简体   繁体   中英

Trying to upload video to facebook using FBConnect for iPhone

I am working on an app which will upload videos to a Facebook users wall, however I have not had much success. I present an extended permissions dialog window, and then use the face.video.upload method call. In the debugger, it seems like each parameter is set correctly, however the ext permission dialog never completely displays, and the video file never uploads.

The video file is stored in the app's documents directory (record and playback work fine) but the upload is broken. I have the video.upload parameters in the dialogDidSucceed: method, and I have modified the FBRequest.m generatePostBody: method to accept video files.

Any help would be tremendous, as I have been banging my head against the wall on this one. Thanks in advance.

Here is the view controller code:

-(IBAction)loginToFacebook
{
  session = [[FBSession sessionForApplication:kAPIKey secret:kAPISecret delegate:self] retain];

  FBLoginDialog *loginDialog = [[[FBLoginDialog alloc] initWithSession:session] autorelease];

  [loginDialog show];
}

-(IBAction)askPermission
{
  //---------------ask permission---------------------/
  FBPermissionDialog *permDialog = [[[FBPermissionDialog alloc]init]autorelease];

  permDialog.delegate = self;

  permDialog.permission = @"video_upload";

  [permDialog show];
}   


-(void)dialogDidSucceed:(FBPermissionDialog *)dialog
{

  //---------------video file path--------------------/
  NSString *path = [NSString stringWithFormat:@"%@/Documents/%@.mov", NSHomeDirectory(), aSelectedQuote.quoteID];

  //---------------video data converter---------------/

  NSData *videoData = [NSData dataWithContentsOfFile:path];

  videoFileName = [NSString stringWithUTF8String:[videoData bytes]];

  //---------------dict for FB upload-----------------/
  NSMutableDictionary *args = [[[NSMutableDictionary alloc] init] autorelease];

  [args setObject:videoFileName forKey:@"video"];
  [args setObject:aSelectedQuote.quoteTitle forKey:@"title"];

  //---------------FBRequest--------------------------/
  FBRequest *uploadVideoRequest = [FBRequest requestWithDelegate:self];

  [uploadVideoRequest call:@"facebook.video.upload" params:args dataParam:videoData];

  //[uploadVideoRequest call:@"facebook.video.upload" params:args];


  NSLog(@"Upload video button pushed.");

}

-(void)dialogDidCancel:(FBDialog *)dialog
{
  NSLog(@"user canceled request");
}



-(void)session:(FBSession *)session didLogin:(FBUID)uid
{
  NSLog(@"user with id %lld logged in.",uid);

  NSString *fql = [NSString stringWithFormat:@"select uid, name from user where uid == %lld", session.uid];

  NSDictionary *params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];

  [[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];

}

/*
  -(void)sessionDidLogout:(FBSession *)session
  {
  }
*/


-(void)request:(FBRequest *)request didLoad:(id)result
{
  if ([result isKindOfClass:[NSArray class]])
    {
      NSArray *users = result;
      NSDictionary *user = [users objectAtIndex:0];
      NSString *name = [user objectForKey:@"name"];

      NSLog(@"FBRequest didLoad: - logged in as %@",name);
    }
}

-(void)dialog:(FBDialog *)dialog didFailWithError:(NSError *)error
{
  NSLog(@"Error (%d) %@", [error code], [error localizedDescription]);
}

Here is the FBRequest.m code:

- (NSMutableData*)generatePostBody {
  NSMutableData* body = [NSMutableData data];
  NSString* endLine = [NSString stringWithFormat:@"\r\n--%@\r\n", kStringBoundary];

  [self utfAppendBody:body data:[NSString stringWithFormat:@"--%@\r\n", kStringBoundary]];

  for (id key in [_params keyEnumerator]) {
    [self utfAppendBody:body
      data:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]];
    [self utfAppendBody:body data:[_params valueForKey:key]];
    [self utfAppendBody:body data:endLine];
  }

  if (_dataParam != nil) {
    if ([_dataParam isKindOfClass:[UIImage class]]) {
      NSData* imageData = UIImagePNGRepresentation((UIImage*)_dataParam);
      [self utfAppendBody:body
        data:[NSString stringWithFormat:@"Content-Disposition: form-data; filename=\"photo\"\r\n"]];
      [self utfAppendBody:body
        data:[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"]];
      [body appendData:imageData];
    } else {
      NSAssert([_dataParam isKindOfClass:[NSData class]], @"dataParam must be a UIImage or NSData");
      /*           
               [self utfAppendBody:body
               data:[NSString stringWithFormat:@"Content-Disposition: form-data; filename=\"data\"\r\n"]];
               [self utfAppendBody:body
               data:[NSString stringWithString:@"Content-Type: content/unknown\r\n\r\n"]];
               [body appendData:(NSData*)_dataParam];
      */
      if ([_method isEqualToString:@"facebook.video.upload"]) {
    [self utfAppendBody:body
          data:[NSString stringWithFormat:@"Content-Disposition: form-data; filename=\"data.mov\"\r\n"]];
    [self utfAppendBody:body
          data:[NSString stringWithString:@"Content-Type: video/quicktime\r\n\r\n"]];
      }
      else {
    [self utfAppendBody:body
          data:[NSString stringWithFormat:@"Content-Disposition: form-data; filename=\"data\"\r\n"]];
    [self utfAppendBody:body
          data:[NSString stringWithString:@"Content-Type: content/unknown\r\n\r\n"]];
      }

    }
    [self utfAppendBody:body data:endLine];
  }

  FBLOG2(@"Sending %s", [body bytes]);
  return body;
}

You're setting videoFileName to the raw bytes of the videofile:

videoFileName = [NSString stringWithUTF8String:[videoData bytes]];
....
[args setObject:videoFileName forKey:@"video"];

I think you meant:

videoFileName = [path lastPathComponent];

I hope this fixes your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM