2013-06-18 36 views
5

我已經使用facebook SDK與Facebook進行身份驗證。當在設置中啓用然後通過點擊按鈕的應用程序沒有Facebook賬戶進入Safari瀏覽器進行身份驗證,即ACAccountType始終顯示0個帳戶

if (appDelegate.session.state != FBSessionStateCreated) 
    { 
     appDelegate.session = [[FBSession alloc] init]; 
    } 

    [appDelegate.session openWithCompletionHandler:^(FBSession *session, 
                FBSessionState status, 
                NSError *error) { 
     // and here we make sure to update our UX according to the new session state 
     [self updateView]; 
    }]; 

否則,如果Facebook帳戶啓用然後

NSArray *fbAccounts=nil; 
    ACAccountType *accountTypeFB; 
    if ((_accountStore = [[ACAccountStore alloc] init]) && 
     (accountTypeFB = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook])){ 
     fbAccounts = [_accountStore accountsWithAccountType:accountTypeFB]; 
     NSLog(@" %d fbAccounts",[fbAccounts count]); 
    } 
    if ([fbAccounts count]!=0) { 

     [FBSession openActiveSessionWithAllowLoginUI:YES]; 
     NSArray *permissions = [[NSArray alloc] initWithObjects: 
           @"email", 
           nil]; 

     [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { 
      if (error) { 
       NSLog(@"Failure"); 
       NSLog(@"error %@",error); 
      } 
      else 
      { 
       NSLog(@" active session opened "); 
       if (self.gotUserDetails) 
       { 
        return; 
       } 
       [self fetchuserinfo]; 
      } 
     }]; 
    } 

enter image description here}

這顯示提醒以訪問用戶信息的圖像。它在模擬器中工作正常。但在設備中,它總是去Safari瀏覽器或Facebook應用程序進行身份驗證,即使在設置中啓用了Facebook帳戶。請幫我找出這個問題。

感謝所有。

+0

你是否調用過'requestAccessToAccountsWithType:options:completion:'? – Wain

+0

@我應該在哪裏調用它? –

回答

1

您不能直接致電accountTypeWithAccountTypeIdentifier訪問FB賬戶,您需要提供屬性。這需要使用requestAccessToAccountsWithType:options:completion:完成,並具有異步響應。

將基於備份的Web身份驗證轉換爲其他方法,然後檢查是否可以按原樣創建帳戶存儲。如果沒有,請調用Web方法。如果可以的話,嘗試訪問與requestAccessToAccountsWithType:options:completion:的FB賬戶。在完成塊中,您將被授予訪問權限,否則您將調用Web方法。

這段代碼應該都在主線程上運行。可以在不同的線程上調用異步響應,以便切換回主線程以完成處理。


填入...部分。

- (void)webAuthorise {...} 

- (void)authorise { 
if(!_accountStore) 
    _accountStore = [[ACAccountStore alloc] init]; 

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

if (facebookTypeAccount == nil) { 
    NSLog(@"Failed, No account"); 
    [self webAuthorise]; 
    return; 
} 

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

              [self ...]; 
             } else { 
              NSLog(@"Failed, Error: %@", error); 

              [self webAuthorise]; 
             } 
             }); 
            }]; 
} 
+0

你能否提供一些示例代碼@Wain –

+0

您的問題是否已解決,或者您仍然需要示例代碼? – Wain

+0

未解決@Wain –