我正在使用 iOS6 的新社交 API 实现 Facebook 视频上传,一切正常,除了我没有找到知道上传进度的方法……Apple 似乎提供的唯一 API 是如果上传完成或失败,有可能他们是如此--------以至于他们没有提供知道它的方法吗?
我也没有找到关于这个话题的任何问题,不可能只有我对此感兴趣,如果有人知道其他任何事情,请告诉我们。
谢谢 !
我一直在努力解决这个问题,终于让它工作了。我想我会分享我的片段:
-(void)sendTwitter {
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES) {
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0) {
twitterAccount = [arrayOfAccounts lastObject];
NSString *status = @"Secrets";
UIImageView *uploadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 640, 480)];
UIImage *uploadImage = [image resizedImageToFitInSize:uploadImageView.bounds.size scaleIfSmaller:NO];
NSURL *requestURL = [NSURL URLWithString:@"https://upload.twitter.com/1.1/statuses/update_with_media.json"];
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:requestURL parameters:nil];
[postRequest addMultipartData:UIImageJPEGRepresentation(uploadImage, 0.6) withName:@"media[]" type:@"multipart/form-data" filename:nil];
[postRequest addMultipartData:[status dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data" filename:nil];
[postRequest setAccount:twitterAccount];
NSURLRequest *preparedRequest = [postRequest preparedURLRequest];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:preparedRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Twitter upload success");
[self completedSendingPhotos];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Twitter upload error");
}];
[operation start];
}
}
}];
}
用于preparedURLRequest
获取NSURLRequest
对象。通过手动发送请求,NSURLConnection
您可以提供一个可以处理委托方法的委托,例如connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
.
对于那些感兴趣的人来说,我是如何使用 AFNetworking 的,在拥有一个 SLRequest 对象之后,我使用@Jesper 的方式来获取一个进度监听器。
NSURLRequest* preparedRequest = [uploadRequest preparedURLRequest];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:preparedRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"%lld bytes out of %d sent.", totalBytesWritten, fileSize);
float progress = totalBytesWritten/(float)fileSize;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Facebook upload success");
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Facebook upload error");
});
}];
[operation start];
希望有帮助!