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