6

I have a local photo in my iPhone app, and I want to upload it to a specific album on Facebook dedicated to my app.

Problem: My app name ends with the word 'Photos', so the default Facebook album is APP_NAME Photos Photos - obviously a problem.

Goal: I want to create a new album, store its ID in NSUserDefaults, and (after some validation at runtime) use it for all future photos being uploaded from the app.

Where I'm at: I can make the new album via Open Graph, using my desired name and description, and I can retrieve its ID. I can also upload photos easily enough. However, what I CANNOT figure out is how to specify where that uploaded photo ends up!

Code: Here's my photo upload code with some results commented in. I'm assuming this is where my problem is, since album creation, etc, is working perfectly.

- (void)postPhotosToFacebook:(NSArray *)photos withAlbumID:(NSString *)albumID {

    // Just testing with one image for now; I'll get in to multiple later..
    UIImage *image = [photos lastObject];

    FBRequest *imageUploadRequest = [FBRequest requestForUploadPhoto:image];

    // CALL OUT: I'm guessing my problem is here - but I just don't know!
    [[imageUploadRequest parameters] setValue:albumID
                                       forKey:@"album"];

    DebugLog(@"imageUploadRequest parameters: %@",[imageUploadRequest parameters]);

    // Results of DebugLog:
    // imageUploadRequest parameters: {
    //     album = 10151057144449632;
    //     picture = "<UIImage: 0xc6b6920>";
    // }
    // The album data returns correctly using FB's Graph API explorer tool,
    // so I know it's a legit album.
    // https://developers.facebook.com/tools/explorer

    FBRequestConnection *connection = [[FBRequestConnection alloc] init];

    [connection addRequest:imageUploadRequest
         completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

             if (!error) {

                 DebugLog(@"Photo uploaded successfuly! %@",result);

                 // Results of DebugLog:
                 // 
                 // Photo uploaded successfuly! {
                 //     id = 10151057147399632;
                 //     "post_id" = "511304631_10151057094374632";
                 // }
                 // Again, the photo at that ID shows up correctly, etc -
                 // BUT it's in the wrong album! It shows up in the default app album.

             } else {

                 DebugLog(@"Photo uploaded failed :( %@",error.userInfo);
             }

         }];

    [connection start];

}

Guesses: I'm guessing my problem is somewhere in how I'm assigning my album ID parameter to the image request.. but I have no clue.

You can see at this URL that this should be possible..

shawnwall
  • 4,549
  • 1
  • 27
  • 38
toblerpwn
  • 5,415
  • 8
  • 38
  • 46

1 Answers1

6

Ahh SWEET. I figured this one out with some good old-fashioned, straight-up tinkering.

Given that there is no real instruction on how to apply the 'album' value to an FBRequest, I hesitate to call it a bug, but either way I needed a different method - in this case, simply creating a generic Open Graph HTTP request and filling in all the image info.

There is no real downside to this approach; except that you can't use the requestForUploadPhoto: convenience method.

Here is my implementation of the correct method (removed code is commented out):

- (void)postPhotosToFacebook:(NSArray *)photos withAlbumID:(NSString *)albumID {

    UIImage *image = [photos lastObject];

//    FBRequest *imageUploadRequest = [FBRequest requestForUploadPhoto:image];
//    
//    [[imageUploadRequest parameters] setValue:albumID
//                                       forKey:@"album"];
//    
//    DebugLog(@"imageUploadRequest parameters: %@",[imageUploadRequest parameters]);

    NSString *graphPath = [NSString stringWithFormat:@"%@/photos",albumID];

    DebugLog(@"graphPath = %@",graphPath);

    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                image,@"picture",
                                nil];

    FBRequest *request = [FBRequest requestWithGraphPath:graphPath
                                              parameters:parameters
                                              HTTPMethod:@"POST"];

    FBRequestConnection *connection = [[FBRequestConnection alloc] init];

    [connection addRequest:request
         completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

             if (!error) {

                 DebugLog(@"Photo uploaded successfuly! %@",result);

             } else {

                 DebugLog(@"Photo uploaded failed :( %@",error.userInfo);
             }

         }];

    [connection start];

}
toblerpwn
  • 5,415
  • 8
  • 38
  • 46
  • Thx i was struggling with this when migrating to SDK 3.1. because i was trying to make it work with `requestForUploadPhoto` – Kalem Nov 26 '12 at 10:10
  • God, this saved me so much time. I do not understand why Facebook didn't include this in their API tutorials. What you did is so straightforward and worked very well. Thanks! – n.evermind Nov 26 '13 at 20:45
  • I couldn't agree more with the above comment. I spent all day yesterday setting up a complicated opengraph and image staging setup for SDK 3.13 and then came across your code. Twitter took 10 minutes, Facebook took an entire day. That should tell you something about their programmers. – user3344977 Mar 21 '14 at 19:37