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