This topic describes how to use the iOS software development kit (SDK) to upload local media files to 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 you integrate the SDK, open your project and modify the configuration as follows.
In the menu bar, click Build Setting > Linking > Other Linker Flags.
Add -Objc.
Basic settings
Initialize the upload instance
First, understand the overall upload process for client-side uploads. Then, deploy an authorization service based on your chosen authorization 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.
Initializing the upload instance involves two steps: declaring the initialization callback and initializing the instance.
Declare the
VODUploadClientproperty. It cannot be a local variable.Initialize the upload instance.
Upload URL and credential method
NoteTo 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
NoteTo 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 to receive messages about key events during the upload process.
Set the
VODUploadListenerobject. This object is a callback class for the upload status. You need to set 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];After this method is called, the
OnUploadStartedListenercallback is triggered. If you use an upload URL and credential, you must set the upload URL and credential in this callback method. The following code provides an example:[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, a videoId is returned. Use this videoId to obtain a playback URL. For more information, see Obtain playback URLs.
After an image is uploaded, an imageUrl is returned. If you enable URL signing, the imageUrl has a time-to-live (TTL). For more information, see Configure URL signing.
Queue management
The VODUploadClient supports adding multiple files for sequential upload and provides the following methods to manage the upload queue:
Although VODUploadClient supports uploading multiple files, you must set the upload credential and URL for each file individually if you use that method. Due to the complexity of the code for multi-file uploads, we recommend that you add and upload only 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 you stop an upload, you can call
resumeFileto resume uploading the file, or clear the queue and add the file again to restart the upload.Pause the upload.
- (BOOL)pause;Resume the upload.
- (BOOL)resume;
Callback handling
Upload failure
If an upload fails, the
OnUploadFailedListenercallback is triggered. In this callback method, you can view the cause of the failure from thecodeandmessageparameters and display a notification on the page. For more information about error codes, see Error codes and OSS error codes.Expired credential handling
If an upload credential or STS token expires, the
OnUploadTokenExpiredListenercallback is triggered. In this callback method, request a new upload credential or STS token from your app server and call one of the following methods to resume the upload.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
If an upload times out, the
OnUploadRertyListenercallback is triggered, and the upload is automatically retried. In this callback method, you can display a notification on the page or call thestopmethod to stop the upload. You can also set themaxRetryCountproperty to specify the maximum number of retries. If the upload can be resumed after a timeout retry, theOnUploadRertyResumeListenercallback is triggered, and the upload resumes.
Advanced settings
The VODUploadClient supports advanced settings, such as upload acceleration, upload transcoding, timeout retries, setting the cache folder location, resumable uploads, multipart uploads, and setting the VOD service region. The following sections provide examples.
Upload acceleration
When you need to upload large files (in gigabytes or terabytes) or perform cross-region uploads, such as uploading a video from the Chinese mainland to a storage address in the Singapore storage region, you can enable the upload acceleration feature. For more information, see how to enable the feature. After you enable the feature, you must add the required key-value pair to the UserData string in the vodInfo configuration of the upload instance. This string must be in JSON format. The following example shows the 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;