You need to enable JavaScript to run this app.
导航

上传进度条(iOS SDK)

最近更新时间2024.02.04 18:31:10

首次发布时间2024.01.05 14:12:13

iOS SDK 支持进度条功能,可以传入自定义函数(block)来监听上传进度相关事件,并实现自定义的业务逻辑。

示例代码

以下代码展示如何在上传过程中使用进度条。

// 从STS服务获取的临时访问密钥和安全令牌(AccessKey、SecretKey、SecurityToken)
TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey" securityToken:@"securityToken"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

TOSPutObjectInput *put = [TOSPutObjectInput new];
put.tosBucket = @"bucket-name";
put.tosKey = @"object-name";
put.tosContent = your_data; // NSData

// 自定义上传进度回调函数
put.tosUploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
    NSLog(@"%lld, %lld, %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
};

TOSTask *task = [client putObject:put];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Put object success.");
        TOSPutObjectOutput *output = task.result;
    } else {
        NSLog(@"Put object failed, error: %@" ,task.error);
    }
    return nil;
}];