Facebook 3.1 SDK with iOS 4.3




我正在尝试让Facebook 3.1 SDK与iOS 4.3 +配合使用,并在应用程序从身份验证返回后显示一个对话框,但是我在身份验证和显示用于旧版Facebook SDK的对话框时遇到问题。我已经通读了Facebook文档,但有些不是很清楚,有些东西被折旧了,而另一些则不起作用。任何正确方向的帮助将不胜感激。
谢谢

如果您使用最新的SDK,请您尝试以下代码。

[FBSession openActiveSessionWithReadPermissions:[NSArray arrayWithObjects:@"read_stream", nil] allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {
                                  if (session.isOpen) {
                                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"You logged in" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                                      [alertView show];
                                  }
                                  else if(status == FBSessionStateClosedLoginFailed) {
                                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Loggin failed" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                                      [alertView show];
                                  }
                              }];

1.在您的系统中安装最新的 Facebook SDK,并在您的应用程序中安装 SDK 框架。

2.在应用委托中添加以下代码。

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }
    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}
/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            @"user_likes",
                            nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}
- (void)openSession
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}

3.在应用委托中添加以下代码。

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

4.最后在您需要时致电此代表

navAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSessionWithAllowLoginUI:YES]; 

最新更新