2013-04-25 123 views
3

如果用戶在他的iPhone設置中設置了Facebook帳戶,我可以獲得用戶個人資料權限。爲此,我使用了下面的代碼:使用SDK登錄Facebook

if(!_accountStore) 
    _accountStore = [[ACAccountStore alloc] init]; 

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount 
             options:@{ACFacebookAppIdKey: @"app_id", ACFacebookPermissionsKey: @[@"email"]} 
            completion:^(BOOL granted, NSError *error) { 
             if(granted){ 
              NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount]; 
              _facebookAccount = [accounts lastObject]; 
              NSLog(@"Success"); 

              [self me]; 
             }else{ 
              NSLog(@"go to iphone settings"); 

             } 
            }]; 






-(void)me 
    { 

    NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

    SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook 
               requestMethod:SLRequestMethodGET 
                 URL:meurl 
               parameters:nil]; 

    merequest.account = _facebookAccount; 

    [merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

     NSLog(@"%@", meDataString); 

    }]; 
} 

如果用戶沒有設置自己的Facebook帳戶,然後控制就會轉移到別的部分即去iphone設置。在這部分內容中,我想打開Facebook登錄頁面,以便用戶能夠登錄他的Facebook帳戶,而無需進行iPhone設置。那麼我們怎麼能通過SDK打開Facebook登錄。請幫我解決這個問題。

在此先感謝。

回答

1

在這種情況下,如果您沒有使用用戶帳戶(如果他/她沒有在iOS設置中設置),則需要在應用程序上實現Facebook SDK(https://developers.facebook.com/ios/)。這很簡單,只需按照以下步驟操作即可(https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/)。

之後,您可以使用FBSession's方法+openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:執行用戶登錄。

是真實的,在我的申請,我甚至不嘗試登錄蘋果的requestAccessToAccountsWithType:options:completion:;與最新的Facebook SDK的用戶,默認行爲是:

  1. 嘗試與用戶帳戶設置密碼在iOS設置上;
  2. 如果未找到帳戶,則嘗試打開Facebook應用程序以請求登錄/權限;
  3. 如果Facebook應用程序未安裝在設備上,它會在Safari上打開Facebook,作爲後備。
+0

我試圖與Facebook登錄的時候,如果一個Facebook帳戶在手機設置中配置了「FBSessionStateClosedLoginFailed」。我不知道爲什麼這是發生。 – 2014-07-11 05:00:52