使用SLRequest将多个图像上传到Facebook

时间:2016-04-12 18:48:37

标签: ios objective-c facebook image-uploading slrequest

我有两种方式将图像分享到Facebook。

SDK Way(完美的工作):

FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];

    NSMutableArray *uploadImages = [[NSMutableArray alloc] init];
    for (UIImage *image in images) {
        FBSDKSharePhoto *sharePhoto = [[FBSDKSharePhoto alloc] init];
        sharePhoto.caption = title;
        sharePhoto.image = image;

        [uploadImages addObject:sharePhoto];
    }
    content.photos = [NSArray arrayWithArray:uploadImages];

    [FBSDKShareAPI shareWithContent:content delegate:self];

和SLRequest方式(仅发送1张图片):

+ (void)uploadPhotoToFacebook:(NSMutableArray *)imagesData imageTitle:(NSString *)title account:(ACAccount*)account completionBlock:(shareSocialResponse)completion
{
    SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                    requestMethod:SLRequestMethodPOST
                                                              URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
                                                       parameters:@{@"message": title}];

    for (NSData *imageData in imagesData) {
        [facebookRequest addMultipartData:imageData
                                 withName:@"source"
                                     type:@"multipart/form-data"
                                 filename:@"photo!"];
    }
    NSLog(@"facebookRequest %@",facebookRequest);
    facebookRequest.account = account;
    [facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
    {
        if (error) {
            NSLog(@"%@",error.description);

        } else {
            NSDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
            NSLog(@"returnedData: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);

            if ([urlResponse statusCode] != 200) {

                dispatch_async(dispatch_get_main_queue(), ^{
                    NSString *errorMessage = @"We could not process your request now";
                    if ([returnedData valueForKeyPath:@"error.message"] != nil) {
                        errorMessage = [returnedData valueForKeyPath:@"error.message"];
                    }
                    completion(NO, errorMessage);
                });
            } else {
                completion(YES, @"Your clip was posted!");
            }
        }
    }];
}
  

我相信如果在多部分中发送的每个数据都可以工作,但是没有。

任何人都知道该怎么做?感谢。

1 个答案:

答案 0 :(得分:1)

facebook文档中有一些文章可能会对您有所帮助。

https://developers.facebook.com/docs/graph-api/making-multiple-requests

因此,在您的情况下,您需要在请求中发送一个名为batch的字段,并且每个文件必须附加一个唯一的名称,以便其他人不会重写。

batch字段的内容将是一个json,您将在其中指定请求的端点,如下所示(缩小btw):

[
  {
    "method": "POST",
    "relative_url": "me/photos",
    "body": "message=labrys",
    "attached_files": "file1"
  },
  {
    "method": "POST",
    "relative_url": "me/photos",
    "body": "message=circle",
    "attached_files": "file2"
  }
]

我用postman尝试过,该请求的设置应如下所示:

image

很抱歉没有发布代码,我不熟悉目标C,但我希望你能理解这一点。