ios Facebook登录没有打开App

时间:2013-09-11 16:22:56

标签: iphone ios facebook facebook-graph-api

我正在使用此代码在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帐户从我的应用程序登录?

1 个答案:

答案 0 :(得分:0)

找到一个解决方案,使用此代码我可以使用仅在设备设置中注册的帐户登录facebook并且无需app:

    - (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");
}];

}