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]; 

     }); 
    }]; 
} 
} 

請刪除額外的代碼。隨意問任何疑問。

相關問題