【问题标题】:Facebook Batch Photo Upload iOSFacebook 批量照片上传 iOS
【发布时间】:2012-02-03 17:04:49
【问题描述】:

我在尝试让 Facebook iOS SDK 批量上传照片时遇到了一些问题。目前我可以一一上传,但如果可能的话,我想批量处理请求。我已阅读此post over and over 以及fb batch docs。这是我到目前为止所拥有的。

 Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook]; 

        NSData *imageData = UIImagePNGRepresentation([imgs objectAtIndex:0]);
   NSString *jsonRequest1 = [NSString stringWithFormat:@"{ \"method\": \"POST\",    \"relative_url\": \"me/photos\", \"attached_files\": \"file1\" }"];
        NSString *jsonRequest2 = [NSString stringWithFormat:@"{ \"method\": \"POST\", \"relative_url\": \"me/photos\", \"attached_files\": \"file2\" }"];
        NSString *jsonRequestsArray = [NSString stringWithFormat:@"[ %@, %@ ]", jsonRequest1, jsonRequest2];


    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonRequestsArray,@"batch",imageData,@"file1",imageData,@"file2" nil];

我正在将 imageData 映射到它正在寻找的密钥,但我每次都得到这个响应。

  {
        body = "{\"error\":{\"message\":\"File batch has not been attached\",\"type\":\"GraphBatchException\"}}";
        code = 400;
        headers =         (
                        {
                name = "WWW-Authenticate";
                value = "OAuth \"Facebook Platform\" \"invalid_request\" \"File batch has not been attached\"";
            },
                        {
                name = "HTTP/1.1";
                value = "400 Bad Request";
            },
                        {
                name = "Cache-Control";
                value = "no-store";
            },
                        {
                name = "Content-Type";
                value = "text/javascript; charset=UTF-8";
            }
        );
    },
        {
        body = "{\"error\":{\"message\":\"File file2 has not been attached\",\"type\":\"GraphBatchException\"}}";
        code = 400;
        headers =         (
                        {
                name = "WWW-Authenticate";
                value = "OAuth \"Facebook Platform\" \"invalid_request\" \"File file2 has not been attached\"";
            },
                        {
                name = "HTTP/1.1";
                value = "400 Bad Request";
            },
                        {
                name = "Cache-Control";
                value = "no-store";
            },
                        {
                name = "Content-Type";
                value = "text/javascript; charset=UTF-8";
            }
        );
    }
)

非常感谢任何帮助。

【问题讨论】:

    标签: ios facebook facebook-graph-api facebook-ios-sdk


    【解决方案1】:

    试试这个。

    请注意,Facebook 将批量请求的数量限制为 50 个。

    -(void)uploadBulkPhotosToAlbum:(NSArray *)photoArray albumId:(NSString *)albumId
    
        FBRequestConnection *connection = [[FBRequestConnection alloc] init];
        NSString *graphPath = [NSString stringWithFormat:@"%@/photos",albumId];
    
        NSMutableString *jsonFormat = [[NSMutableString alloc] init];
        [jsonFormat setString:@"["];
    
        for (int i = 0; i < photoArray.count; i++)
        {
            if (i != 0)
            {
                [jsonFormat appendString:@","];
            }
    
            NSString *fileName = [NSString stringWithFormat:@"file%d",i];
            NSString *jsonRequest = [NSString stringWithFormat:@"{ \"method\": \"POST\",    \"relative_url\": \"%@\", \"attached_files\": \"%@\" }",graphPath,fileName];
            [jsonFormat appendString:[NSString stringWithFormat:@" %@",jsonRequest]];
        }
    
        [jsonFormat appendString:@" ]"];
    
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonFormat,@"batch",nil];
    
    
        for (int i = 0; i < photoArray.count; i++)
        {
            NSString *fileName = [NSString stringWithFormat:@"file%d",i];
    
            NSData *data = UIImagePNGRepresentation([photoArray objectAtIndex:i]);
            [params setObject:data forKey:fileName];
        }
    
        FBRequest *request = [FBRequest requestWithGraphPath:@"me"  parameters:params HTTPMethod:@"POST"];
        [connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (!error)
            {
                [self resetViewAfterUpload:result];
            }
            else
            {
                [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
                NSLog(@"Photo uploaded failed :( %@",error.userInfo);
                [[[UIAlertView alloc] initWithTitle:@"Error occurred!!" message:@"Some error occured while uploading." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]  show];
            }
        }];
        [connection start];
    }
    

    【讨论】:

    • @Madhuri “albumId”是什么意思?我只有本地路径图像。不是来自任何本地服务器。
    【解决方案2】:

    您是否尝试过使用 iOS 6 的社交框架来分享照片?它允许您添加所有照片并分享它们。

    - (IBAction)postToFacebook:(id)sender {
        if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    
            SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    
            [controller setInitialText:@"First post from my iPhone app"];
            [controller addURL:[NSURL URLWithString:@"http://www.jinibot.com"]];
            [controller addImage:[UIImage imageNamed:@"socialsharing-facebook-image.jpg"]];
    
            //add as many images as you want
            [controller addImage:[UIImage imageNamed:@"socialsharing-facebook-image.jpg"]];
            [controller addImage:[UIImage imageNamed:@"socialsharing-facebook-image.jpg"]];
            [controller addImage:[UIImage imageNamed:@"socialsharing-facebook-image.jpg"]];
    
            [self presentViewController:controller animated:YES completion:Nil];
        }
    }
    

    【讨论】:

    • 每张图片都作为单独的帖子分享。是否可以将单个帖子与多个图像共享?
    【解决方案3】:

    这是我在 Facebook SDK 3.x 的一个项目中使用的功能

    -(void)postOnFacebookWall : (id )response{
    
        NSString *strPost = [NSString stringWithFormat:@"Group Name:%@",[[response valueForKey:@"GroupDetail"] valueForKey:@"group_name"]];
        NSString *strUrl = [NSString stringWithFormat:@"%@",[[response valueForKey:@"GroupDetail"] valueForKey:@"group_picture_path"]];
        NSURL *urlToShare = [NSURL URLWithString:strUrl];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:urlToShare]];
    
        FBAppCall *appCall = [FBDialogs presentShareDialogWithLink:urlToShare
                                                          name:@"Heook"
                                                       caption:@"string"
                                                   description:@"The 'Has."
                                                       picture:urlToShare
                                                   clientState:nil
                                                       handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                           if (error) {
                                                               NSLog(@"Error: %@", error.description);
                                                           } else {
                                                               NSLog(@"Success!");
                                                           }
                                                       }];
    
        if (!appCall) {
            // Next try to post using Facebook's iOS6 integration
            BOOL displayedNativeDialog = [FBDialogs presentOSIntegratedShareDialogModallyFrom:self
                                                                              initialText:strPost
                                                                                    image:image
                                                                                      url:nil
                                                                                  handler:nil];
    
            if (!displayedNativeDialog) {
                [self performPublishAction:^{
                //              NSString *message = [NSString stringWithFormat:@"Group Name:%@\nGroup Image:%@\n",[[response valueForKey:@"GroupDetail"] valueForKey:@"group_name"],[[response valueForKey:@"GroupDetail"] valueForKey:@"group_picture_path"]];
    
                [FBRequestConnection startForPostStatusUpdate:strPost
                                            completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    
                                                [self showAlert:@"Group shared on facebook" result:result error:error];
                                            }];
    
    
            }];
        }
    }
    

    }

    【讨论】:

      【解决方案4】:

      使用NSOperationQueue 将所有上传请求排队怎么样?

      【讨论】:

        【解决方案5】:

        使用new Facebook SDK (3.0) 尝试如下操作:

        FBRequestConnection *connection = [[FBRequestConnection alloc] init];
        
        FBRequest *request1 = [FBRequest requestForUploadPhoto:image1];
        [connection addRequest:request1
             completionHandler:
         ^(FBRequestConnection *connection, id result, NSError *error) {
             //handle error/success
         }
         ];
        
        FBRequest *request2 = [FBRequest requestForUploadPhoto:image2];
        [connection addRequest:request2
             completionHandler:
         ^(FBRequestConnection *connection, id result, NSError *error) {
             //handle error/success
         }
         ];
        
        [connection start];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 2011-11-08
          • 2012-03-07
          • 2014-07-23
          • 1970-01-01
          相关资源
          最近更新 更多