1

我已经使用 FBSession 对我的应用程序进行本地身份验证,这工作正常,我也得到了访问令牌。

但是我的问题是在那之后,当我通过传递特定的 URl 在 webView 上加载客户端 facebook 页面时,它成功地显示了页面但它没有登录。我仍然可以在顶部看到加入和登录按钮。

既然我已经登录了,那为什么要再次登录。

朋友给我一些提示如何解决这个问题。

这是代码。

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

    [self updateView];

    SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    if (!appDelegate.session.isOpen) {
        // create a fresh session object
        appDelegate.session = [[FBSession alloc] init];

        // if we don't have a cached token, a call to open here would cause UX for login to
        // occur; we don't want that to happen unless the user clicks the login button, and so
        // we check here to make sure we have a token before calling open
        if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
            // even though we had a cached token, we need to login to make the session usable



            [appDelegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView
                    completionHandler:^(FBSession *session,
                                        FBSessionState status,
                                        NSError *error) {
                        // this handler is called back whether the login succeeds or fails; in the
                        // success case it will also be called back upon each state transition between
                        // session-open and session-close

                        [self updateView];


                    }];

            NSArray *permissions1 = @[@"email"];
            [appDelegate.session requestNewReadPermissions:permissions1
                                         completionHandler:^(FBSession *session,
                                                             NSError *error)
             {
                 // Handle new permissions callback
             }];

        }
    }
}



// FBSample logic
// main helper method to update the UI to reflect the current state of the session.
- (void)updateView {
    // get the app delegate, so that we can reference the session property
    SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    if (appDelegate.session.isOpen) {        
        // valid account UI is shown whenever the session is open
        [self.buttonLoginLogout setTitle:@"Log out" forState:UIControlStateNormal];        
        [self.textNoteOrLink setText:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",
                                 appDelegate.session.accessTokenData.accessToken]];

        NSLog(@"Print %@",self.textNoteOrLink.text);

       // NSLog(@"Print the user name is %@",appDelegate.session.accessTokenData)
    } else {        
        // login-needed account UI is shown whenever the session is closed
        [self.buttonLoginLogout setTitle:@"Log in" forState:UIControlStateNormal];        
        [self.textNoteOrLink setText:@"Login to create a link to fetch account data"];        
    }
}

// FBSample logic
// handler for button click, logs sessions in or out
- (IBAction)buttonClickHandler:(id)sender {
    // get the app delegate so that we can access the session property
    SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

    // this button's job is to flip-flop the session from open to closed
    if (appDelegate.session.isOpen) {
        // if a user logs out explicitly, we delete any cached token information, and next
        // time they run the applicaiton they will be presented with log in UX again; most
        // users will simply close the app or switch away, without logging out; this will
        // cause the implicit cached-token login to occur on next launch of the application
        [appDelegate.session closeAndClearTokenInformation];

    } else {
        if (appDelegate.session.state != FBSessionStateCreated) {
            // Create a new, logged out session.
            appDelegate.session = [[FBSession alloc] init];


        }

        // if the session isn't open, let's open it now and present the login UX to the user
        [appDelegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView
                            completionHandler:^(FBSession *session,
                                                FBSessionState status,
                                                NSError *error) {
                                // this handler is called back whether the login succeeds or fails; in the
                                // success case it will also be called back upon each state transition between
                                // session-open and session-close
                                [self updateView];

                            }];


    }
}

在另一个视图控制器中,我正在打开客户端页面,如下所示:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.facebook.com/abc"]]];
4

0 回答 0