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];

TOSGetObjectInput *get = [TOSGetObjectInput new];
get.tosBucket = @"bucket-name";
get.tosKey = @"object-name";

// 自定义下载进度回调函数
get.tosDownloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};

TOSTask *task = [client getObject:get];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Get object success.");
        TOSGetObjectOutput *output = task.result;
        NSData *date = output.tosContent;
    } else {
        NSLog(@"Get object failed, error: %@" ,task.error);
    }
    return nil;
}];