0

我在我的一个应用程序中使用 FacebookSDK 登录到应用程序并分享一些东西。登录已完成,我现在可以使用此 SDK 登录到应用程序。下面是我用于登录的代码。

-(void)didSelectLoginWithFB
{
    if (FBSession.activeSession.isOpen) {
        [appDelegate closeSession];
    }
    else {
        [appDelegate openSessionWithAllowLoginUI:YES];
    }

    if(FBSession.activeSession.accessToken ){

        [self performSelector:@selector(FBLoginAction) withObject:nil];
    }

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(FBLoginDidFinish:)
                                                 name:@"FBLoginDidFinish"
                                               object:appDelegate];

}

-(void)FBLoginDidFinish:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self performSelector:@selector(FBLoginAction) withObject:nil];

    }

-(void)FBLoginAction
{
    if (FBSession.activeSession.isOpen) {

    [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,

           NSError *error) {
             if (!error) {

                 NSDictionary *userInfo = (NSDictionary *)user;
                 NSLog(@"User dic %@",userInfo);
//                 fbUserId = user.id;
//                 NSLog(@"User dic %@",fbUserId);

                 userMail = [userInfo objectForKey:@"email"];
                 NSLog(@"User E Mail --- %@", userMail);
                  loginFlag = 200;
                 [self getDataFromServer:nil];
             }

             else{

                 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Facebook Connection Error !!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ];
                 [alert show];
             }

         }];



    }

}

现在,我想在用户的 Facebook 墙上(时间线)发布一些内容。我怎样才能使用这个 SDK 做到这一点?我没有从 developer.facebook.com 获得有关此 sdk 的任何信息。请帮忙 !

4

1 回答 1

3

我知道这已经很晚了,但我希望它会有所帮助..

您可以使用以下代码在用户的墙上/时间轴上发布-

- (void)postToFacebook{

    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    if (appDelegate.session.state != FBSessionStateCreated || !appDelegate.session.isOpen){

        // Create a new, logged out session.
        NSArray *permissions = [NSArray arrayWithObject:@"publish_actions, publish_stream"];
        appDelegate.session = [[FBSession alloc] initWithPermissions:permissions];

        // if the session isn't open, let's open it now and present the login UX to the user
        [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
            if (appDelegate.session.state!=258 && appDelegate.session.state!=257) {

                [self postOpenGraphAction];
            }
        }];
    }else{
        if (appDelegate.session.isOpen) {
            [self postOpenGraphAction];
        }
    }
}

- (void)postOpenGraphAction {

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"", @"picture", @"", @"name", @"", @"link", @"", @"caption", @"", @"description", @"Your message to post", @"message", nil];

    AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
    [FBSession setActiveSession:appDelegate.session];

    [FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (error) {
                                  NSLog(@"Error on publishing story: %@", error);
                              } else {
                                                          NSLog(@"Published on wall successfully");
                              }
                          }];
}

我使用了在 AppDelegate 中创建的会话对象,而不是 FBSession activeSession。您可以根据您的代码使用。

于 2013-06-24T11:38:06.917 回答