0


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

谢谢

4

2 回答 2

2

如果您使用最新的 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];
                                  }
                              }];
于 2012-11-28T07:10:24.727 回答
1

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

2.在您的应用程序delegate.m中添加以下代码

- (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.在您的应用程序delegate.h中添加以下代码

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

4.last 在需要时调用此委托

navAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSessionWithAllowLoginUI:YES]; 
于 2012-11-28T07:06:17.040 回答