Question

I'm implementing a Facebook video upload using the new Social APIs for iOS6, everything is working fine, except that I didn't find a way to know how the upload is progressing ... the only API that Apple seems to provide is that if the upload is completed or if it failed, it's possible that they are so ------- that they don't provide a method to know it?

I also didn't find ANY question regarding this topic, it's not possible that I'm the only one interested in it, if someone knows anything else, let us know.

Thanks !

Was it helpful?

Solution 2

Use preparedURLRequest to get the NSURLRequest object. Sending the request by hand with NSURLConnection lets you provide a delegate that can handle delegate methods like, say, connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:.

OTHER TIPS

I was struggling with this and finally got it working. I thought I'd share my snippet:

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

}

For those interested here is how I did with AFNetworking, after having a SLRequest object, I used @Jesper way to get a progress listener.

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

Hope that helps !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top