Upload SDK for iOS

Last updated: 2020-01-20 15:40:35

    VOD provides an Upload SDK for iOS for uploading videos on the iOS platform. For more information on the upload process, please see Uploading Videos from Client.

    Downloading Source Code

    1. Click here to download the iOS Upload Demo and source code.
    2. You can see the TXUGCUploadDemo directory after decompressing the downloaded zip package. The upload source code is in the TXUGCUploadDemo/upload directory.

    Integrating Upload Library and Source Code

    1. Copy the upload source code directory TXUGCUploadDemo/upload to your project directory.
    2. Import the dynamic libraries QCloudCore.framework and QCloudCOSXML.framework and the static library libmtasdk.a (in the TXUGCUploadDemo/upload/COSSDK/ directory) into your project and add the following dependency libraries:
       1. CoreTelephony.framework
       2. Foundation.framework
       3. SystemConfiguration.framework
       4. libc++.tbd
    3. Set "Other Linker Flags" in "Build Settings" and add the -ObjC parameter.

    Simple Video Upload

    Initializing an upload object

    TXUGCPublish *_videoPublish = [[TXUGCPublish alloc] initWithUserID:@"upload_video_userid"];

    Setting upload object callback

    _videoPublish.delegate = self;
    #pragma mark - TXVideoPublishListener
    
    - (void)onPublishProgress:(NSInteger)uploadBytes totalBytes:(NSInteger)totalBytes {
        self.progressView.progress = (float)uploadBytes/totalBytes;
        NSLog(@"onPublishProgress [%ld/%ld]", uploadBytes, totalBytes);
    }
    
    - (void)onPublishComplete:(TXPublishResult*)result {
        NSString *string = [NSString stringWithFormat:@"Upload completed; error code: [%d], error message: [%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg];
        [self showErrorMessage:string];
        NSLog(@"onPublishComplete [%d/%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg);
    }

    Constructing upload parameter

    TXPublishParam *publishParam = [[TXPublishParam alloc] init];
    
    publishParam.signature  = @"Signature generated by your business backend";
    publishParam.videoPath  = @"Path to the video file";

    For the rule of signature calculation, please see Signature for Upload from Client.

    Calling upload

    [_videoPublish publishVideo:publishParam];

    The upload method automatically selects simple upload or multipart upload based on the file size, eliminating your need to take care of every step in multipart upload.

    Advanced Features

    Uploading a cover

    Include the cover image in the upload parameter.

    TXPublishParam *publishParam = [[TXPublishParam alloc] init];
    publishParam.signature  = @"Signature generated by your business backend";
    publishParam.coverPath = @"Path to the cover image file";
    publishParam.videoPath  = @"Path to the video file";

    Canceling and resuming upload

    To cancel an upload, call the canclePublish API.

    [_videoPublish canclePublish];

    To resume an upload, call publishVideo of TXUGCPublish again by using the same upload parameter, with the paths to the video and cover unchanged.

    Resumable upload

    During the video upload process, VOD supports resumable upload, that is, when the upload is terminated unexpectedly, you can upload the file again from where it left off, which helps reduce the upload time.

    The effective time for resumption is 1 day, i.e., if the upload of a video is interrupted and then resumed within 1 day, it can be directly resumed; otherwise, the full video will be uploaded again by default.

    enableResume in the upload parameter is the switch for resumable upload, which is enabled by default.

    Image and Media Upload

    // Create object
    TXUGCPublish *_imagePublish = [[TXUGCPublish alloc] initWithUserID:@"upload_image_userid"];
    
    // Set callback
    _imagePublish.mediaDelegate = self;
    
    // Construct upload parameter
    TXMediaPublishParam *publishParam = [[TXMediaPublishParam alloc] init];
    publishParam.signature  = @"Signature generated by your business backend";
    publishParam.mediaPath = @"Path to the image file";
    
    // Upload image or media file
    [_imagePublish publishMedia:publishParam];
    

    Video Upload API Description

    Initialize upload object: TXUGCPublish::initWithUserID

    Parameter Name Description Type Required
    userID User ID used to uniquely identify a user. NSString No

    Start upload: TXUGCPublish.publishVideo

    Parameter Name Description Type Required
    param Publishing parameter. TXPublishParam Yes

    Upload parameter: TXPublishParam

    Parameter Name Description Type Required
    signature Signature for upload from client. NSString* Yes
    videoPath Path to a local video file. NSString* Yes
    coverPath Path to a local cover image, which is optional. NSString* No
    fileName Name of a video file uploaded to Tencent Cloud. If this parameter is left empty, the local filename will be used by default. NSString* No
    enableResume Whether to enable resumable upload, which is enabled by default. BOOL No
    enableHttps Whether to enable HTTPS, which is disabled by default. BOOL No

    Set upload callback: TXUGCPublish.delegate

    Member Variable Name Description Type Required
    delegate Listener on the upload progress and result callback. TXVideoPublishListener Yes

    Upload progress callback: onPublishProgress

    Variable Name Description Type
    uploadBytes Number of uploaded bytes. NSInteger
    totalBytes Total number of bytes. NSInteger

    Upload result callback: onPublishComplete

    Variable Name Description Type
    result Upload result. TXPublishResult

    Upload event callback: onPublishEvent

    Variable Name Description Type
    evt Event for printing debugging information. NSDictionary

    Upload result: TXPublishResult

    Member Variable Name Description Type
    retCode Error code. int
    descMsg Error description of a failing upload. NSString
    videoId VOD video file ID. NSString
    videoURL Video storage address. NSString
    coverURL Cover storage address. NSString

    Pre-upload: TXUGCPublishOptCenter.prepareUpload

    Parameter Name Description Type Required
    signature Signature for upload from client. NSString Yes

    Error codes

    The SDK listens on the video upload status through the TXVideoPublishListener API; therefore, you can check the video publishing status with the retCode in TXPublishResult.

    Error Code Constant in TVCCommon Description
    0 TVC_OK Uploaded successfully.
    1001 TVC_ERR_UGC_REQUEST_FAILED The upload request failed. This is usually because that the signature of the client has expired or is invalid. You need to apply for a signature for the app again.
    1002 TVC_ERR_UGC_PARSE_FAILED Failed to parse the request information.
    1003 TVC_ERR_VIDEO_UPLOAD_FAILED Failed to upload the video.
    1004 TVC_ERR_COVER_UPLOAD_FAILED Failed to upload the cover.
    1005 TVC_ERR_UGC_FINISH_REQ_FAILED The request to end the upload failed.
    1006 TVC_ERR_UGC_FINISH_RSP_FAILED An error occurred when responding to the upload ending request.

    Image and Media Upload API Description

    Initialize upload object: TXUGCPublish::initWithUserID

    Parameter Name Description Type Required
    userID User ID used to uniquely identify a user. NSString No

    Start upload: TXUGCPublish.publishMedia

    Parameter Name Description Type Required
    param Publishing parameter. TXMediaPublishParam Yes

    Upload parameter: TXMediaPublishParam

    Parameter Name Description Type Required
    signature Signature for upload from client. NSString* Yes
    mediaPath Path to a local image/media file. NSString* Yes
    fileName Name of an image/media file uploaded to Tencent Cloud. If this parameter is left empty, the local filename will be used by default. NSString* No
    enableResume Whether to enable resumable upload, which is enabled by default. BOOL No
    enableHttps Whether to enable HTTPS, which is disabled by default. BOOL No

    Set upload callback: TXUGCPublish.TXMediaPublishListener

    Member Variable Name Description Type Required
    mediaDelegate Listener on the upload progress and result callback. TXMediaPublishListener Yes

    Upload progress callback: onMediaPublishProgress

    Variable Name Description Type
    uploadBytes Number of uploaded bytes. NSInteger
    totalBytes Total number of bytes. NSInteger

    Upload result callback: onMediaPublishComplete

    Variable Name Description Type
    result Upload result. TXMediaPublishResult

    Upload event callback: onMediaPublishEvent

    Variable Name Description Type
    evt Event for printing debugging information. NSDictionary

    Upload result: TXMediaPublishResult

    Member Variable Name Description Type
    retCode Error code. int
    descMsg Error description of a failing upload. NSString
    mediaId Image/media file ID. NSString
    mediaURL Image/media storage address. NSString

    Pre-upload: TXUGCPublishOptCenter.prepareUpload

    Parameter Name Description Type Required
    signature Signature for upload from client. NSString Yes

    Error codes

    The SDK listens on the image/media upload status through the TXMediaPublishListener API; therefore, you can check the image/media publishing status with the retCode in TXMediaPublishResult.

    Error Code Constant in TVCCommon Description
    0 MEDIA_PUBLISH_RESULT_OK Uploaded successfully.
    1001 MEDIA_PUBLISH_RESULT_UPLOAD_REQUEST_FAILED The upload request failed. This is usually because that the signature of the client has expired or is invalid. You need to apply for a signature for the app again.
    1002 MEDIA_PUBLISH_RESULT_UPLOAD_RESPONSE_ERROR Failed to parse the request information.
    1003 MEDIA_PUBLISH_RESULT_UPLOAD_VIDEO_FAILED Failed to upload the image/media.
    1005 MEDIA_PUBLISH_RESULT_PUBLISH_REQUEST_FAILED The request of ending the upload failed.
    1006 MEDIA_PUBLISH_RESULT_PUBLISH_RESPONSE_ERROR An error occurred when responding to the upload ending request.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help