Unable to upload large videos to Facebook from iOS

2019-02-16 03:33发布

I am trying to upload large files video files to Facebook, but regardless of the approach taken, the result is always the same. The process uploads between 5-35mb worth of data then times out. This happens on WiFi.

I have tried with the Facebook SDK 3.1.1, iOS Social Library (i.e. SLRequest) and AFNetworking.

The social library and afnetworking give time out errors, whereas the Facebook SDK just returns a Code 5, Operation Could Not Be Completed, HTML Error 200 but if I watch the network activity via instruments it has the same signature, that being a certain amount of megabytes being uploaded before it stalls.

Note that I am able to upload smaller videos without any issue, using any of the three methods.

Has anyone encountered this issue and found any solutions or reasons for it?

p.s. I believe its a Facebook bug and I've logged an issue there if anyone else wants to subscribe to it to encourage them to investigate it (https://developers.facebook.com/bugs/265409976924087).

Facebook SDK Code

NSData *videoData = [NSData dataWithContentsOfFile:videoUrlStr options:NSDataReadingMappedAlways error:&lError];
NSString *description = self.streamToShare.videoDescription;

    if (description == nil){
        description = @"";
    }
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:videoData, self.streamToShare.publishedStoryFileName,
                                       @"video/quicktime", @"contentType",
                                       self.streamToShare.name, @"title",
                                       description,@"description",
                                       nil];
[FBRequestConnection startWithGraphPath:@"me/videos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (error) {
                self.errorMessage = [NSString stringWithFormat:@"error: domain = %@, code = %d, description = %@", error.domain, error.code, error.localizedDescription];
            } 
}

iOS Native Library and AFNetworking Code

[accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                              options:@{ACFacebookAppIdKey: appID,ACFacebookPermissionsKey: @[@"publish_stream"],ACFacebookAudienceKey:ACFacebookAudienceFriends} completion:^(BOOL granted, NSError *error) {
  if(granted){
    NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
    facebookAccount = [accounts lastObject];
    NSLog(@"Facebook Login Success");
    NSURL *videourl = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];
    NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:self.streamToShare.publishedStoryURL isDirectory:NO];
    NSDictionary *params = @{
      @"title": self.streamToShare.name,
      @"description": description
    };
    SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:videourl parameters:params];
    [uploadRequest addMultipartData:videoData withName:@"source" type:@"video/quicktime" filename:[pathURL absoluteString]];
    uploadRequest.account = facebookAccount;
    NSURLRequest *urlRequest = [uploadRequest preparedURLRequest];
    NSMutableURLRequest *mutableUrlRequest = [urlRequest mutableCopy];
    [mutableUrlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    [mutableUrlRequest setTimeoutInterval:60]; // adjusting this does not fix the issue

    // AF Networking Code                                                
    NSInputStream *stream = [[NSInputStream alloc] initWithData:videoData];
    [mutableUrlRequest setHTTPBodyStream:stream];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:mutableUrlRequest];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
       NSLog(@"%lld bytes out of %lld sent", totalBytesWritten, totalBytesExpectedToWrite, progress);
     }];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@"Facebook upload success");
      }   failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Facebook upload error %@",error.localizedDescription);
      }
     }];
     [operation start];

     // iOS Native Library Upload - Commented out so AFNetworking could be tested                                               
     //NSURLResponse *urlResponse = nil;
     //NSError *urlRequestError = nil;
     /*[NSURLConnection sendSynchronousRequest:mutableUrlRequest returningResponse:&urlResponse error:&urlRequestError];
    if (urlResponse == nil) {
      // Check for problems
      if (urlRequestError != nil) {
         NSLog(@"Error %@", urlRequestError.localizedDescription);
      }
     }
     else {
       // Data was received.. continue processing
       NSLog(@"Worked!");
     }*/


  }else{
    // ouch
    NSLog(@"Permission not granted. Error: %@", error);
  }
}];

2条回答
虎瘦雄心在
2楼-- · 2019-02-16 04:05

graph.facebook.com seems only to accept small videos. Try posting to graph-video.facebook.com/me/videos instead

NSURL *videourl = [NSURL URLWithString:@"https://graph-video.facebook.com/me/videos"];
查看更多
唯我独甜
3楼-- · 2019-02-16 04:27

This has been resolved in the latest SDK. It now works without issue.

查看更多
登录 后发表回答