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

时间:2015-10-05 17:06:54

标签: ios swift facebook-graph-api facebook-login facebook-ios-sdk

我最近安装了应用程序" mapstr"当你选择Facebook登录时,你没有通过Facebbok应用程序,只需通过一种alertView接受权限:

enter image description here

用法语写的,它说:" mapstr"想要访问您的公开个人资料和朋友列表。

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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

使用保存在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];

        });
    }];
}
}

请删除额外的代码。随意提出任何问题。