2012-12-14 112 views
0

我正在实施Facebook的视频上传使用iOS6的新的社交API,一切工作正常,除了我没有找到一种方法来知道如何上传正在进展... Apple似乎提供的唯一API就是如果上传完成或者失败了,那么他们可能是如此-------以至于他们没有提供知道它的方法?SLRequest进度回调(iOS 6)

我也没有发现关于这个话题的任何问题,我不是唯一一个对此感兴趣的人,如果有人知道别的东西,请告诉我们。

谢谢!

回答

2

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

+0

嗨加斯帕,谢谢你,用这个方法帮助了,但是我不得不使用AFNetworking来处理HTTP操作,为什么在苹果---- couldn在SLRequest操作中不包含进度块? – MindTrip

+0

一个很好的问题。也许是因为如果你需要这样的反馈,你也可能对其他委托方法感兴趣。现在有一条非常简单的路径,可以通过NSURLRequest来减少对代码和路径的控制。如果只有一条路径,则必须能够在块API(或其他)中复制所有这些委托方法。但你说得对,简单的道路本身可能更强大一些。 – Jesper

2

对于这里感兴趣的是我如何处理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]; 

希望帮助!

5

我一直在努力,终于搞定了。我想我会分享我的代码片段:

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

}