1

在我的新 iOS 应用程序中,我必须登录 Facebook 并获取用户详细信息。我在这里阅读了教程 - http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/authenticate/并研究了 Scrumptious 和其他示例。但我无法理解流程。

我创建了一个示例项目来进行 Facebook 登录(下面的代码) - 但有以下主要问题:

  • 为什么当我再次单击登录按钮时应用程序要求授权 - 在我在 Facebook 上单击一次“确定”后

下面是我的代码 -

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // See if the app has a valid token for the current state.
    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
    {
        // To-do, show logged in view --- LOGGED IN : Already Login
        // Yes, so just open the session (this won't display any UX).
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        ViewController *rootViewController = (ViewController *)window.rootViewController;
        [rootViewController openSession];        
    }

    return YES;
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [FBSession.activeSession handleOpenURL:url];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBSession.activeSession handleDidBecomeActive];
}


@implementation ViewController

@synthesize lblStatus;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    lblStatus.text = @"Login Now";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender
{
    [self openSession];
}

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
        {
            // Connection is Open
            lblStatus.text = @"FBSessionStateOpen";
        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
        {
            [FBSession.activeSession closeAndClearTokenInformation];

            // Connection is Closed / Login Failed
            lblStatus.text = @"FBSessionStateClosed";
        }
            break;
        default:
            break;
    }
}

- (void)openSession
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error)
     {
         [self sessionStateChanged:session state:state error:error];
     }];
}

答案---->

感谢 nerowolfe 的建议。

以下是我连接到 Facebook 的完整解决方案。我的下一个任务是现在获取用户数据。

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [FBSession.activeSession handleOpenURL:url];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBSession.activeSession handleDidBecomeActive];
}

--------------------------

@implementation ViewController

@synthesize lblStatus;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    lblStatus.text = @"Login Now";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender
{
    [self openSession];
}

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
        {
            // Connection is Open
            lblStatus.text = @"FBSessionStateOpen";
        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
        {
            [FBSession.activeSession closeAndClearTokenInformation];

            // Connection is Closed / Login Failed
            lblStatus.text = @"FBSessionStateClosed";
        }
            break;
        default:
            break;
    }
}

- (void)openSession
{
    if(FBSession.activeSession.isOpen)
    {
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:NO
                                      completionHandler:
         ^(FBSession *session,
           FBSessionState state, NSError *error)
         {
             [self sessionStateChanged:session state:state error:error];
         }];
    }
    else
    {
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:YES         
                                      completionHandler:
         ^(FBSession *session,
           FBSessionState state, NSError *error)
         {
             [self sessionStateChanged:session state:state error:error];
         }];
    }
}

- (IBAction)logout:(id)sender
{
    [FBSession.activeSession closeAndClearTokenInformation];

    if(FBSession.activeSession.isOpen)
        lblStatus.text = @"Open Still";
    else
        lblStatus.text = @"Close";

}
4

1 回答 1

1

只是不显示 LoginUI。

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

       [FBSession openActiveSessionWithReadPermissions:nil
                                   allowLoginUI:NO
                              completionHandler:
                     ^(FBSession *session,
                         FBSessionState state, NSError *error)
       {
        [self sessionStateChanged:session state:state error:error];
       }];
    }

 }
于 2013-07-23T12:19:00.540 回答