logologo
售前咨询
点播云
产品简介
购买指南
快速入门
控制台指南
服务端API
SDK文档
播放器SDK
上传SDK
服务端SDK
相关协议
文档中心SDK文档上传SDKiOS端集成

iOS端集成


上传SDK iOS端集成说明

1.使用SLUploaderConfig配置上传实例

@interface SLUploaderConfig : NSObject
/// 上传凭证,由服务交互返回
@property (nonatomic, copy) NSString *authentication;
/// 上传任务id, 推荐使用uuid进行标识
@property (nonatomic, copy) NSString *taskId;
@end

2.SLUploader上传实例

//  SLUploader.h
@interface SLUploader : NSObject
@property (atomic, assign, readonly) BOOL isPaused;
@property (atomic, assign, readonly) BOOL isCancelled;
@property (atomic, assign, readonly) BOOL isCompleted;
@property (nonatomic, readonly) SLUploaderConfig *config;

+ (instancetype)imageUploaderWithImagePath:(NSString *)imagePath
                                    config:(SLUploaderConfig *)config
                               delegate:(id<SLUploaderDelegate>)delegate;

+ (instancetype)videoUploaderWithVideoPath:(NSString *)videoPath
                                    config:(SLUploaderConfig *)config
                               delegate:(id<SLUploaderDelegate>)delegate;

+ (instancetype)videoWithCoverUploaderWithVideoPath:(NSString *)videoPath
                                          coverPath:(NSString *)coverPath
                                             config:(SLUploaderConfig *)config
                                           delegate:(id<SLUploaderDelegate>)delegate;

/// 开始上传
- (void)startUpload;


/// 取消上传
- (void)cancel;


/// 暂停上传
- (BOOL)pause;


/// 继续上传
- (BOOL)resume;

@end

3.SLUploaderDelegate 上传回调协议

//  SLUploader.h
@protocol SLUploaderDelegate <NSObject>
@optional

/// 上传进度回调
/// @param uploader 上传实例
/// @param progress 上传进度
/// @discussion 回调线程在主线程队列
- (void)uploader:(SLUploader *)uploader uploadProgress:(double)progress;

/// 上传完成回调
/// @param uploader 上传实例
/// @param error 上传错误
/// @param mediaToken 上传结果的媒资凭证
/// @discussion 回调线程在主线程队列, 可依据错误是否存在判断上传是否成功
- (void)uploader:(SLUploader *)uploader didFinishWithError:(nullable NSError *)error mediaToken:(nullable NSString *)mediaToken;

@end

4. Example

4.1 和服务交互获取上传凭证 (异步)

NSString *mediaType = @"";
if (self.imagePath) {
    mediaType = self.imagePath.pathExtension;
}
if (self.videoPath) {
    mediaType = self.videoPath.pathExtension;
}

[SLONetworkingManager.sharedManager sendPOSTRequestWithAction:@"ApplyUploadToken"
                                                   bodyParams:@{@"MediaType" : mediaType}
                                            completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable JSONResponse) {
    self.uploadToken = JSONResponse[@"UploadToken"];
    self.taskID = NSUUID.UUID.UUIDString;
}];

4.2 使用上传凭证和 taskId 创建上传实例,进行上传

    self.uploaderConfig = [[SLUploaderConfig alloc] init];
    self.uploaderConfig.authentication = self.uploadToken;
    self.uploaderConfig.taskId = self.taskID;
    
    if (self.imagePath) {
        self.uploader = [SLUploader imageUploaderWithImagePath:self.imagePath
                                                        config:self.uploaderConfig
                                                      delegate:self];
    }
    
    if (self.videoPath) {
        self.uploader = [SLUploader videoUploaderWithVideoPath:self.videoPath
                                                        config:self.uploaderConfig
                                                      delegate:self];
    }
    
    if (self.imagePath && self.videoPath) {
        self.uploader = [SLUploader videoWithCoverUploaderWithVideoPath:self.videoPath
                                                              coverPath:self.imagePath
                                                                 config:self.uploaderConfig
                                                               delegate:self];
    }
    
    [self.uploader startUpload];

4.3 实现上传回调协议方法

- (void)uploader:(SLUploader *)uploader uploadProgress:(double)progress {
    self.progressView.progress = progress;
    self.logLabel.text = @(progress).description;
}

- (void)uploader:(SLUploader *)uploader didFinishWithError:(NSError *)error mediaToken:(NSString *)mediaToken {
    if (error) {
        self.logLabel.text = error.description;
    } else {
        NSLog(@"mediatoken = %@", mediaToken);
        self.logLabel.text = [NSString stringWithFormat:@"upload success: %@", mediaToken];
    }
}
上一篇:Android端集成下一篇:错误码查询
该篇文档内容是否对您有帮助?
有帮助没帮助