如何在不打开 Facebook 应用程序的情况下登录 Facebook?

How to Facebook login without opening Facebook app?

我最近安装了该应用程序 "mapstr",当您选择 Facebook 登录时,您不需要通过 Facebbok 应用程序,您只需通过一种 alertView 接受权限即可:

它是用法语写的,上面写着:"mapstr" 想要访问您的 public 个人资料和您的好友列表。

当我点击确定时,我只是使用 Facebook 登录:没有应用程序切换!

他们是怎么做到的? (我在 Swift 2.0 中开发)

它使用保存在您的 iPhone 设置中的 facebook 凭据通过 facebook 登录。您将需要为此使用帐户框架。下面是相同的示例代码。

-(void)facebook
{
ACAccountStore  *accountStore;
accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:kFACEBOOK_APPID,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];
[accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
 ^(BOOL granted, NSError *e) {
     if (granted)
     {
         [self afterPermissionGranted:accountStore accountType:FBaccountType];
     }
     else
     {
         //Fail gracefully...
         NSLog(@"error getting permission %@",e);
         dispatch_async(dispatch_get_main_queue(), ^{
             [self openSessionWithAllowLoginUI:YES];
         });
     }
 }];
}
-(void)afterPermissionGranted:(ACAccountStore  *)accountStore accountType:(ACAccountType *)FBaccountType{

NSArray *accounts = [accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
if ([accounts count] == 0) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"No Other Facebook Account Found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

}
else{
    ACAccount *facebookAccount;
    facebookAccount = [accounts lastObject];
    ACAccountCredential *facebookCredential = [facebookAccount credential];
    NSString *accessToken = [facebookCredential oauthToken];
    NSLog(@"FAT: %@", accessToken);
    NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
    request.account = facebookAccount;


    [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
        if(!error)
        {
            NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            NSDictionary *errorPart = [list objectForKey:@"error"];
            NSString *errorsubCode =[NSString stringWithFormat:@"%@",[errorPart valueForKey:@"error_subcode"]];
            if (![errorsubCode isEqualToString:@"(null)"]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self openSessionWithAllowLoginUI:YES];
                });
            }
            else{
                NSString *globalmailID   = [NSString stringWithFormat:@"%@",[list objectForKey:@"email"]];
                NSLog(@"global mail : %@",globalmailID);


                NSString *fbname = [NSString stringWithFormat:@"%@",[list objectForKey:@"name"]];
                NSLog(@"fname %@",fbname);
                ACAccountCredential *fbCredential = [facebookAccount credential];
                NSString *accessToken = [fbCredential oauthToken];

                [self saveDataFromFacebook:error user:list accessToken:(NSString *)accessToken];
            }
        }
        else
        {
            //handle error gracefully
            NSLog(@"error from get%@",error);
            //attempt to revalidate credentials
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self endProgressBar];

        });
    }];
}
}

请删除多余的代码。如有任何疑问,请随时提出。

我是 Mapstr 创始人 ;) 这是 Facebook LoginManager(在 iOS SDK 中)中的一个 loginBehavior 选项,它是 "FBSDKLoginBehaviorSystemAccount"(您还有应用程序选项和网络选项) 如果用户在其 iPhone 上配置了他的面部 boo 帐户,SDK 将使用它,如果没有,它将尝试应用程序,然后是网络。 唯一的缺点是,如果系统帐户存在但配置错误,它会失败...... 塞巴斯蒂安