首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SLRequest进度回调( iOS 6)

SLRequest进度回调( iOS 6)
EN

Stack Overflow用户
提问于 2012-12-14 16:45:33
回答 3查看 1.2K关注 0票数 0

我正在使用iOS6的新社交API实现一个Facebook视频上传,一切都很好,除了我没有找到一种方法来知道上传的进展情况……Apple似乎提供的唯一API是,如果上传完成或失败,他们可能是如此-他们没有提供一种方法来了解它?

我也没有发现任何关于这个话题的问题,我不可能是唯一对这个话题感兴趣的人,如果有人知道任何其他事情,请让我们知道。

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-14 16:50:18

使用preparedURLRequest获取NSURLRequest对象。通过使用NSURLConnection手动发送请求,您可以提供一个可以处理委托方法的委托,比如connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:

票数 2
EN

Stack Overflow用户

发布于 2013-02-05 03:23:01

我一直在努力解决这个问题,并最终让它工作起来。我想我应该分享我的代码片段:

代码语言:javascript
复制
-(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];
        }
    }
}];

}

票数 5
EN

Stack Overflow用户

发布于 2012-12-15 01:54:11

对于那些对这里感兴趣的人来说,我是如何使用AFNetworking的,在拥有一个SLRequest对象之后,我使用@Jesper的方式来获得一个进度监听器。

代码语言:javascript
复制
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];

希望这能有所帮助!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13875236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档