0

使用 FacebookSDK 登录 facebook 后,我想使用 UIWebView 显示页面。首次登录时显示正确。但是我关闭并重新启动应用程序,即使 facebook 会话仍然打开,我也无法登录我的 UIWebView。

当我重新运行该应用程序以获取 Facebook 会话信息时,您对此有何建议?

这是我的代码示例。

@interface FBLogin2ViewController ()

@end

@implementation FBLogin2ViewController

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

    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        // To-do, show logged in view

        NSString *appID = [FBSession defaultAppID];

        NSLog(@"Session token exists %@", appID);
    }
}

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

- (IBAction)onLogin:(id)sender {

    NSArray *permissions = nil;

    if (![FBSession activeSession]) {
        FBSession *session = [[FBSession alloc] initWithAppID:@"000000000000" permissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:@"fb" tokenCacheStrategy:nil];

        [FBSession setActiveSession:session];
    }
    else if( [FBSession activeSession].isOpen )
    {
        [[FBSession activeSession] closeAndClearTokenInformation];
        FBSession.activeSession = nil;
    }

    [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                          [self sessionStateChanged:session state:state error:error];
                                      }];

}

- (IBAction)onLogout:(id)sender {

    [FBSession.activeSession closeAndClearTokenInformation];
    FBSession.activeSession = nil;
}

- (IBAction)onGoWeb:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.facebook.com/xxxxx?fref=xx"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    _webView.delegate = self;
}


- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen: {

        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            // Once the user has logged in, we want them to
            // be looking at the root view.


            break;
        default:
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
       [alertView show];
    }
    }

    @end
4

1 回答 1

0

1) 在 didFinishLaunchingWithOptions 方法中调用 openFBSessionIfAvaliable

//in appdelegate
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            [self openFBSessionIfAvaliable];
        }

//in appdelegate
    - (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];
    }
//in appdelegate
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        [[FBSession activeSession] handleDidBecomeActive];
    }

//in appdelegate
    - (void)applicationWillTerminate:(UIApplication *)application
    {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        [[FBSession activeSession] close];
    }

-(void)openFBSessionIfAvaliable
{
    [FBSession setActiveSession:[FBSession new]];
    if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded)
    {
        [self openFBSessionWithAllowLoginUI:NO];
    }
}

- (BOOL)openFBSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error)
            {

                [self sessionStateChanged:session
                                    state:state
                                    error:error];
            }];
}

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state)
    {
        case FBSessionStateOpen:
            if (!error)
            {
                NSString  *_facebookToken = [[FBSession activeSession] accessToken];
                NSString  *_facebookEmail = [user objectForKey:@"email"];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    if (error)
    {
        [FBSession.activeSession closeAndClearTokenInformation];
        /*UIAlertView *alertView = [[UIAlertView alloc]
         initWithTitle:@"Error"
         message:@"Please try Again"
         delegate:nil
         cancelButtonTitle:@"OK"
         otherButtonTitles:nil];
         [alertView show];*/

    }

}

2)在按钮点击“通过facebook登录”调用下面的方法

[self openFBSessionWithAllowLoginUI:YES];

3)现在每次启动应用程序时,它都会检查 Facebook 会话,如果找到则继续进行,否则显示按钮“通过 Facebook 登录”.session

我希望它能解决你的问题。

于 2013-04-19T09:44:02.633 回答