简体   繁体   中英

Post Photo Album to timeline using Facebook iOS SDK

I want to send a some photos from my iPhone App to my Facebook wall:

for (int i=0; i<_pageImages.count; i++) {   
    UIImage *img = [self.pageImages objectAtIndex:i];
    NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
    [params setObject:@"my custom message" forKey:@"message"];
    [params setObject:img forKey:@"picture"];

    [self performPublishAction:^{
        [FBRequestConnection startWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST"
            completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                [self showAlert:@"Photo med text Post" result:result error:error];
        }];
    }];
}

The code completely works but as you see in the code Alert'll be displayed _pageImages.count time. I can easily remove that.

I think it could be a better way to post the list of photos. could you help me?

If your intention is to only show the alert once, you could initialize a global count of the images you are uploading and instead of calling the showAlert:result:error method, you could call a new method. The new method would process the results and increment a local count of processed images. When the local count reached the global count you could display an alert.

You could also think about batching your requests, see https://developers.facebook.com/docs/howtos/batch-requests-ios-sdk/

You could perhaps change the loop logic to:

// Before the loop
FBRequestConnection *connection = [[FBRequestConnection alloc] init];

// Loop through images, set up the request
for (int i=0; i<_pageImages.count; i++) { 
    ....
    FBRequest *request = [FBRequest requestWithGraphPath:@"me/photos"
                     parameters:params
                     HTTPMethod:@"POST"];
    [connection addRequest:request
         completionHandler:
             ^(FBRequestConnection *connection, id result, NSError *error) {
                 // Call your method to check results and keep count
                 // of the callbacks before displaying the final output
    }];
}

// After the loop
[connection start];

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