Facebook iOS SDK: Cannot specify an album using FBRequest's requestForUploadPhoto:

眉间皱痕 提交于 2019-12-18 02:55:07

问题


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..


回答1:


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

}


来源:https://stackoverflow.com/questions/12486852/facebook-ios-sdk-cannot-specify-an-album-using-fbrequests-requestforuploadphot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!