繁体   English   中英

ios Facebook登录无需打开应用程序

[英]ios Facebook login without open App

我正在使用此代码通过 facebook 在我的应用程序中登录用户。

AppDelegate *appDelegate = (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 openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
            // we recurse here, in order to update buttons and labels

        }];
    }
}

// get the app delegate so that we can access the session property
AppDelegate *appDelegate = (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 openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
        // and here we make sure to update our UX according to the new session state

    }];
}

此代码工作正常,但它始终打开 facebook 应用程序(如果我尚未安装该应用程序,则打开 safari facebook 登录)进行登录。 有没有办法只使用手机设置中注册的 fb 帐户从我的应用程序登录?

找到了一个解决方案,使用此代码,我可以仅在设备设置中注册一个帐户且无需应用程序即可登录 Facebook:

    - (void) performPublishAction:(void (^)(void)) action {

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
    //clear the token cache
    [[FBSession activeSession] closeAndClearTokenInformation];
    [[FBSession activeSession] close];
    [FBSession setActiveSession:nil];


} 

// we defer request for permission to post to the moment of post, then we check for the permission

if ([[FBSession activeSession]isOpen]) {

    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
        // if we don't already have the permission, then we request it now
        [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
                                              defaultAudience:FBSessionDefaultAudienceFriends
                                            completionHandler:^(FBSession *session, NSError *error) {
                                                if (!error) {

                                                    action();
                                                }
                                                //For this example, ignore errors (such as if user cancels).
                                            }];
    } else {

        action();
    }

}
else{
    [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                       defaultAudience:FBSessionDefaultAudienceFriends
                                          allowLoginUI:YES
                                     completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                         if (!error && status == FBSessionStateOpen) {
                                             action();
                                         }else{
                                             NSLog(@"error");
                                         }
                                     }];
}




}
-(void)Login{


 [self performPublishAction:^{

     nslog("LOGGED");
}];

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM