2012-01-05 40 views
14

Tôi đang gặp một số sự cố khi cố gắng tải SDK iOS của Facebook lên hàng loạt ảnh tải lên. Hiện tại tôi có thể tải lên từng cái một, nhưng tôi muốn gửi hàng loạt các yêu cầu nếu có thể. Tôi đã đọc số post over and over này cùng với fb batch docs. Đây là những gì tôi có cho đến nay.Tải lên hàng loạt Facebook Ảnh iOS

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

Tôi ánh xạ imageData vào khóa mà nó đang tìm kiếm, nhưng tôi nhận được phản hồi này mọi lúc.

{ 
     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"; 
      } 
     ); 
    } 
) 

Bất kỳ sự giúp đỡ rất nhiều đánh giá cao.

Trả lời

1

Với new Facebook SDK (3.0) thử một cái gì đó như thế này:

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

bạn đã thử sử dụng khuôn khổ xã hội dành cho iOS 6 để chia sẻ hình ảnh? Nó cho phép bạn thêm tất cả các bức ảnh và chia sẻ chúng.

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

mỗi hình ảnh được chia sẻ như bài riêng biệt. có thể chia sẻ dưới dạng một bài đăng với nhiều hình ảnh không? – venky

0

Đây là chức năng tôi đã sử dụng trong một trong những dự án của chúng tôi bằng 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]; 
             }]; 


     }]; 
    } 
} 

}

2

Hãy thử với điều này.

Xin lưu ý rằng Facebook hạn chế số lượng yêu cầu mà có thể là trong một lô để 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]; 
} 
+0

bắt buộc phải vượt qua albumID ??? –

+0

Cảm ơn nó hoạt động hoàn hảo .. –

+0

@Madhuri ý bạn là gì "albumId" ?. Tôi chỉ có hình ảnh đường dẫn cục bộ. Không phải từ bất kỳ máy chủ cục bộ nào. –

Các vấn đề liên quan