Use the iOS SDK to upload local media files to ApsaraVideo VOD storage.
Prerequisites
-
You are using iOS 8.0 or later.
-
The iOS upload SDK does not support Swift.
Integrate the SDK
Integration methods
Integrate the SDK using CocoaPods (Recommended)
-
Run the
pod 'VODUpload'command to add the VOD upload SDK as a dependency in your Podfile. -
Run the
pod repo updatecommand to update the pod repository. -
Run the
pod installcommand to install the VOD upload SDK.
Integrate the SDK manually
SDK version: 1.6.5
Update time: 2022-01-24
Package MD5 hash: f3551634b53cd1264013db4762f79a14
Download address: V1.6.5 SDK
-
In Xcode, drag VODUpload.framework and AliyunOSSiOS.framework to your project target. In the dialog box that appears, select Copy items if needed.
-
Add the following system dependency libraries.
-
AVFoundation.framework
-
CoreMedia.framework
-
SystemConfiguration.framework
-
MobileCoreServices.framework
-
libresolv.9.tbd
-
Project configuration
After integration, configure your Xcode project:
-
In the menu bar, click Build Setting > Linking > Other Linker Flags.
-
Add -Objc.
Basic settings
Initialize the upload instance
Understand the client-side upload process, then deploy an authorization service for your chosen method:
-
If you use an upload URL and credential, obtain the upload URL and credential from your authorization service.
-
If you use a Security Token Service (STS) token, obtain an STS token from your authorization service.
Initialize the upload instance in two steps:
-
Declare the
VODUploadClientproperty. It cannot be a local variable. -
Initialize the upload instance.
Upload URL and credential method
Note-
To use an upload URL and credential, call the
initmethod for initialization. -
Do not set the upload URL and credential during initialization. After the upload starts, the
OnUploadStartedListenercallback is triggered. Call thesetUploadAuthAndAddress:uploadAuth:uploadAddress:method in this callback to set the URL and credential. -
When the credential expires, the
OnUploadTokenExpiredListenercallback is triggered. Call theresumeWithAuthmethod and provide a new upload credential to resume the upload.
STS token method
Note-
To initialize using an STS token, call the
initmethod and pass the temporary STS credentials using thesetKeyId:accessKeySecret:secretToken:expireTime:listener:method. -
When the token expires, the
OnUploadTokenExpiredListenercallback is triggered. Call theresumeWithToken:accessKeySecret:secretToken:expireTime:method and provide a new STS token to resume the upload.
-
-
Set callbacks for key upload events.
Set the
VODUploadListenerobject with the following callback methods: -
Construct the upload parameters based on the file type, such as audio, video, or image.
NoteThe upload parameters for audio or video files and image files are slightly different. The client does not support uploading auxiliary media assets.
Audio and video file parameters
Construct an upload request function to add an audio or video file to the upload list.
VodInfo description
After you add a file, the SDK encapsulates the file into an
UploadFileInfoobject. The structure is as follows:NoteTo upload a video from a photo album, use the absolute path of the selected video as the file path for the upload.
Image file parameters
-
Start the upload.
-
Call
startto begin the upload.[self.uploader start];This triggers the
OnUploadStartedListenercallback. If you use an upload URL and credential, set them in this callback:[weakSelf.uploader setUploadAuthAndAddress:fileInfo uploadAuth:weakSelf.uploadAuth uploadAddress:weakSelf.uploadAddress]; -
After the file starts uploading, the
OnUploadProgressListenercallback starts to synchronize the upload progress.The callback parameters include the size of the uploaded part (
uploadedSize) and the total file size (totalSize). -
After the file is successfully uploaded, the
OnUploadFinishedListenercallback returns the uploaded file information (UploadFileInfo) and the upload result (VodUploadResult).The
VodUploadResultobject contains the following properties:@property (nonatomic, copy) NSString* videoId; @property (nonatomic, copy) NSString* imageUrl;NoteThe
videoIdproperty is returned only after a video is successfully uploaded using an STS token. TheimageUrlproperty is returned only after an image is successfully uploaded using an STS token. If you use an upload URL and credential,videoIdandimageUrlare not returned. You can obtain these values when you request the upload URL and credential.
-
Execution results
-
After a video is uploaded, use the returned videoId to obtain playback URLs.
-
After an image is uploaded, an imageUrl is returned. If URL signing is enabled, the imageUrl has a TTL. Configure URL signing.
Queue management
VODUploadClient supports adding multiple files for sequential upload. Use the following methods to manage the upload queue:
VODUploadClient supports multi-file uploads, but each file requires its own credential and URL. For simplicity, upload one file at a time.
-
Delete a file from the upload queue. If the file is being uploaded, the upload is canceled, and the next file in the queue starts uploading automatically.
- (BOOL)deleteFile:(int) index; -
Clear the upload queue. If a file is being uploaded, the upload is canceled.
- (BOOL)clearFiles; -
Obtain the list of files in the upload queue.
- (NSMutableArray<UploadFileInfo *> *)listFiles; -
Mark a file as canceled. The file remains in the upload list. If the file is being uploaded, the upload is canceled, and the next file in the queue starts uploading automatically.
- (BOOL)cancelFile:(int)index; -
Resume the upload of a canceled file. The upload starts automatically.
- (BOOL)resumeFile:(int)index;
Upload control
-
Stop the upload. If a file is being uploaded, the upload is canceled.
- (BOOL)stop;NoteAfter stopping, call
resumeFileto resume, or clear the queue and re-add the file. -
Pause the upload.
- (BOOL)pause; -
Resume the upload.
- (BOOL)resume;
Callback handling
-
Upload failure
If an upload fails, the
OnUploadFailedListenercallback is triggered. Check thecodeandmessageparameters for the failure cause, and display a notification on the page. Error code references: Error codes and OSS error codes. -
Expired credential handling
When an upload credential or STS token expires, the
OnUploadTokenExpiredListenercallback is triggered. Request a new credential from your app server and call one of the following methods to resume:-
Reset the upload credential
- (BOOL)resumeWithAuth:(NSString *)uploadAuth; -
Reset the STS token
- (BOOL)resumeWithToken:(NSString *)accessKeyId accessKeySecret:(NSString *)accessKeySecret secretToken:(NSString *)secretToken expireTime:(NSString *)expireTime;
-
-
Timeout handling
On timeout, the
OnUploadRertyListenercallback is triggered and the upload is retried automatically. You can display a notification on the page or callstopto cancel, or setmaxRetryCountto limit retries. When the upload resumes after a retry, theOnUploadRertyResumeListenercallback is triggered.
Advanced settings
VODUploadClient supports the following advanced settings:
Upload acceleration
For large files (GB/TB) or cross-region uploads (for example, from the Chinese mainland to the Singapore storage region), enable upload acceleration. After enabling, add the required key-value pair to the UserData JSON string in the vodInfo configuration:
/**
Set custom data in VodInfo.
*/
@property (nonatomic, copy) NSString *UserData;
vodInfo.UserData = "{\"Type\":\"oss\",\"Domain\":\"oss-accelerate.aliyuncs.com\"}";
Parameter descriptions
|
Name |
Type |
Description |
|
Type |
string |
The type of upload acceleration to enable. Only `oss` is supported. |
|
Domain |
string |
The accelerated domain name for the user's bucket. The default protocol is HTTPS. Note
Use an accelerated domain name that is assigned after you enable the feature, such as vod-*******.oss-accelerate.aliyuncs.com. |
Upload transcoding
/**
Specifies whether to transcode the file on the server after upload. The default value is YES. To specify the output format for video transcoding, see Video transcoding.
*/
@property (nonatomic, assign) BOOL transcode;
Timeout and retry
/**
The maximum number of retries. The default value is INT_MAX.
*/
@property (nonatomic, assign) uint32_t maxRetryCount;
/**
The timeout period.
*/
@property (nonatomic, assign) NSTimeInterval timeoutIntervalForRequest;
Set the cache folder location
/**
The location of the cache folder.
*/
@property (nonatomic, copy) NSString * recordDirectoryPath;
Resumable upload
/**
Specifies whether to record the upload progress for resumable uploads. The default value is YES.
*/
@property (nonatomic, assign) BOOL recordUploadProgress;
Multipart upload
/**
The part size. The default value is 1024 * 1024.
*/
@property (nonatomic, assign) NSInteger uploadPartSize;
Set the VOD service region
/**
The VOD region. The default value is "cn-shanghai".
*/
@property (nonatomic, copy) NSString *region;