1

我最近安装的应用程序“mapstr”,当你选择Facebook登录,你不通过Facebbok应用程序走,你只是通过一种alertView的接受权限:如何Facebook登录而无需打开Facebook应用程序?

enter image description here

这是写在法语,它说:“mapstr”想要访问你的公共个人资料和你的朋友列表。

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

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

回答

3

我Mapstr创始人) 它在Facebook的LoginManager(在iOS版SDK)一loginBehavior选项,这是“FBSDKLoginBehaviorSystemAccount”(你也有应用程序选项和网络选项) 如果用户配置自己的脸boo帐户在其iPhone上,SDK将使用它,如果没有,它会尝试应用程序,然后在网络上。 唯一的缺点是,如果系统帐户存在但配置错误失败... Sebastien

+0

答案不能来自更好的来源! 祝贺你成立的创业者!我们可能很快会见;) – Cherif

+0

@Sebastien,尼斯:) –

1

它使用Facebook的凭据保存在您的iPhone设置,通过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]; 

     }); 
    }]; 
} 
} 

请删除额外的代码。随意问任何疑问。

相关问题