Publish picture with caption on application album whether it exists or not using Facebook SDK 3.1 for iOS

耗尽温柔 提交于 2019-12-08 14:52:10

问题


Using toblerpwn answer i manage to publish a picture to the application album as i wanted to. But this works only when the application album has already been created (because i need to specify the album id).

In the other hand the requestForUploadPhoto convenience method creates the application album if it doesn't exists and publishes the picture.

So could someone help me figure out how to merge this two beheviours?


回答1:


Thanks to @deepak-lakshmanan i manage to solve my problem.

The idea is to publish to the application album using /USER_ID/photos insted of /ALBUM_ID/photos; this way the album is automatically created if it doesn't exist.

So the steps to follow are:

  • get the publish permission
  • then retrieve the user id using /me
  • finally publish the picture to the application album with /USER_ID/photos

Here is my code in case someone struggles with this, am using Facebook SDK 3.1 for iOS:

- (void)sendRequestFacebookPublishPermission {
    NSArray *permissions = [[NSArray alloc] initWithObjects:@"publish_stream", nil];
    if(!FBSession.activeSession.isOpen ) {
        [FBSession openActiveSessionWithPublishPermissions:permissions
                                           defaultAudience:FBSessionDefaultAudienceFriends
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                             [self requestFacebookPublishPermissionCompleted:session status:status error:error];
                                         }];
    } else if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
        [FBSession.activeSession reauthorizeWithPublishPermissions:permissions
                                                   defaultAudience:FBSessionDefaultAudienceFriends
                                                 completionHandler:^(FBSession *session, NSError *error) {
                                                     [self requestFacebookPublishPermissionCompleted:session status:FBSessionStateOpen error:error];
                                                 }];
    } else {
        [self sendRequesFacebooktUserInfo];
    }
}

- (void)requestFacebookPublishPermissionCompleted:(FBSession *)session
                                            status:(FBSessionState)status
                                             error:(NSError *)error {
    if (error) {
        DLog(@"%@", error.localizedDescription);
    } else {
        [self sendRequesFacebooktUserInfo];
    }
}

-(void)sendRequesFacebooktUserInfo {
    FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];

     // output the results of the request
    FBRequestHandler handler = ^(FBRequestConnection *connection, id result, NSError *error) {
        [self requestFacebookUserInfoCompleted:connection result:result error:error];
    };

    // create the request object, using the fbid as the graph path
    FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:@"me"];

    // add the request to the connection object
    [newConnection addRequest:request completionHandler:handler];

    // if there's an outstanding connection, just cancel
    [self.requestConnection cancel];

    // keep track of our connection, and start it
    self.requestConnection = newConnection;
    [newConnection start];
}

- (void)requestFacebookUserInfoCompleted:(FBRequestConnection *)connection
                                result:(id)result
                                 error:(NSError *)error {
    // not the completion we were looking for...
    if (self.requestConnection && connection != self.requestConnection) {
        return;
    }

    // clean this up, for posterity
    self.requestConnection = nil;

    if (error) {
        DLog(@"%@", error.localizedDescription);
    } else {
        FBGraphObject *dictionary = (FBGraphObject *)result;
        NSString* userId = (NSString *)[dictionary objectForKey:@"id"];
        [self sendRequestFacebookPublishOnAlbum:userId];
    }
}

- (void)sendRequestFacebookPublishOnAlbum:(NSString*)fbId {
    UIImage *imageToPost = /* get the image you need*/ ;

    /*
    //--- Facebook SDK convenience method : requestForUploadPhoto won't let me post image description ---//
    FBRequestConnection *newConnection = [FBRequestConnection startForUploadPhoto:screenshot completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        [self requestPostCardOnFacebookCompleted:connection result:result error:error];
    }];

    // if there's an outstanding connection, just cancel
    [self.requestConnection cancel];

    // keep track of our connection, and start it
    self.requestConnection = newConnection;
    //-----------------------------------------------------------------------------------------------------//
     */


    //--- http://stackoverflow.com/questions/12486852/facebook-ios-sdk-cannot-specify-an-album-using-fbrequests-requestforuploadphot ---//
    NSString *messagePublie = laCarte.message;
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                screenshot, @"picture",
                                messagePublie , @"name",
                                nil] ;

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

    FBRequestHandler handler = ^(FBRequestConnection *connection, id result, NSError *error) {
        [self requestFacebookPublishOnAlbumCompleted:connection result:result error:error];
    };

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

    FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:graphPath parameters:parameters HTTPMethod:@"POST"];
    [newConnection addRequest:request completionHandler:handler];

    // if there's an outstanding connection, just cancel
    [self.requestConnection cancel];

    // keep track of our connection, and start it
    self.requestConnection = newConnection;
    [newConnection start];
}

- (void)requestFacebookPublishOnAlbumCompleted:(FBRequestConnection *)connection result:(id)result error:(NSError *)error {
    if (self.requestConnection && connection != self.requestConnection) {
        return;
    }

    // clean this up, for posterity
    self.requestConnection = nil;

    if (error) {
        DLog(@"%@", error.localizedDescription);
    } else {
        DLog(@"%@", "Done");
    }
}


来源:https://stackoverflow.com/questions/13565056/publish-picture-with-caption-on-application-album-whether-it-exists-or-not-using

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