2013-01-08 123 views
0

如何使用ios 6 inbuilt facebook sdk檢索facebook用戶詳細信息?我嘗試了幾個例子,但無法工作。在ios 6 sdk中獲取facebook用戶詳細信息?

- (void) getFBDetails { 

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

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

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

              [self me]; 

             }else{ 
              // ouch 
              NSLog(@"Fail"); 
              NSLog(@"Error: %@", error); 
             } 
            }]; 


} 


- (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獲取數據。我的應用程序ID是正確的。

錯誤消息我

Error: Error Domain=com.apple.accounts Code=7 "The Facebook server could not fulfill this access request: no stored remote_app_id for app" UserInfo=0x1d879c90 {NSLocalizedDescription=The Facebook server could not fulfill this access request: no stored remote_app_id for app} 
+1

什麼不起作用?準備回答這個問題,因爲你說你需要一種替代的解決方案,我們需要以某種方式克服你的錯誤和錯誤---當我們不知道他們時! – MCKapur

+0

非常感謝,我用更多的信息編輯了我的問題。 – sajaz

回答

1

不知道這是否會解決它或沒有,但你在你的應用程序中的AppName的-Info.plist文件設置了Facebook應用程序ID?

所需的關鍵是FacebookAppID,這是類型String。 嘗試填寫您的App ID,看看它是否有效。

+0

我添加了它並嘗試,但不工作。這次的錯誤是「錯誤域= com.apple.accounts代碼= 6」操作無法完成。 (com.apple.accounts error 6.)「。這部分在我的代碼中是否正確。?ACFacebookPermissionsKey:@ [@」email「] – sajaz

+0

好吧,它看起來像拋出錯誤7,如果用戶拒絕訪問應用程序,如果用戶沒有在設備上設置Facebook,則拋出錯誤6.您可以做的一件事是檢查if(error.code == 6){//向用戶發出警告,告訴用戶將其帳戶關聯起來} – ryanwils

+0

我找到了解決方案,我們必須在臉書開發者網站上的facebook應用程序中設置包ID,並啓用本機ios應用程序http://stackoverflow.com/questions/12671420/ios-6-facebook-posting-procedure-ends-上與遠程的應用ID此結果不匹配,STOR – sajaz

0

在iOS6.0中,您必須單獨詢問讀寫權限。 首先要求讀取權限是電子郵件,然後根據應用程序要求要求其他權限。

0
in .h file 

#import <Accounts/Accounts.h> 
#import <Social/Social.h> 

@property (nonatomic, strong) ACAccountStore *accountStore; 
@property (nonatomic, strong) ACAccount *facebookAccount; 

in .m file 

- (void) getuserdetails 
{ 

    self.accountStore = [[ACAccountStore alloc]init]; 
    ACAccountType *FBaccountType= nil; 
    //[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    if (! FBaccountType) { 
     FBaccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 
    } 
    NSString *key =kFBAppId; 
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil]; 


    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion: 
    ^(BOOL granted, NSError *e) 
    { 
     if (granted) 
     { 
      NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType]; 
      self.facebookAccount = [accounts lastObject]; 
      NSLog(@"facebook account =%@",self.facebookAccount); 
      [self get]; 
     } 
     else 
     { 
      NSLog(@"fb error %@",e.description); 

      dispatch_async(dispatch_get_main_queue(),^
          { 
           [self performSelectorOnMainThread:@selector(hideLoader) withObject:nil waitUntilDone:YES]; 
           NSLog(@"%@",e.description); 
           if([e code]== ACErrorAccountNotFound) 
           { 
            UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Account not found" 
                        message:msgSetUpFBAccount delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
            [alt show]; 
           } 
           else 
           { 
            UIAlertView* alt = [[UIAlertView alloc] initWithTitle:msgFBAccessDenied 
                        message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
            [alt show]; 
           } 

          }); 
      NSLog(@"error getting permission %@",e); 

     } 
    }]; 
} 

-(void)get 
{ 

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

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
              requestMethod:SLRequestMethodGET 
                 URL:requestURL 
               parameters:nil]; 
    request.account = self.facebookAccount; 
    [request performRequestWithHandler:^(NSData *data, 
             NSHTTPURLResponse *response, 
             NSError *error) 
    { 

     if(!error) 
     { 
      list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSLog(@"Dictionary contains: %@", list); 


      if([list objectForKey:@"error"]!=nil) 
      { 
       [self attemptRenewCredentials]; 
      } 
      dispatch_async(dispatch_get_main_queue(),^{ 
      }); 

     } 
     else 
     { 
      [self performSelectorOnMainThread:@selector(hideLoader) withObject:nil waitUntilDone:YES]; 
      NSLog(@"error from get%@",error); 
     } 

    }]; 


} 



-(void)attemptRenewCredentials{ 
    [self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){ 
     if(!error) 
     { 
      switch (renewResult) { 
       case ACAccountCredentialRenewResultRenewed: 
        NSLog(@"Good to go"); 

        [self get]; 
        break; 
       case ACAccountCredentialRenewResultRejected: 
       { 
        NSLog(@"User declined permission"); 
        UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied" 
                    message:@"You declined permission" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
        [alt show]; 
        break; 
       } 
       case ACAccountCredentialRenewResultFailed: 
       { 
        NSLog(@"non-user-initiated cancel, you may attempt to retry"); 
        UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied" 
                    message:@"non-user-initiated cancel, you may attempt to retry" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
        [alt show]; 
        break; 
       } 
       default: 
        break; 
      } 

     } 
     else{ 
      //handle error gracefully 
      NSLog(@"error from renew credentials%@",error); 
     } 
    }]; 


} 

T**o get this code work the bundle Identifier with which you have registered your application with facebook and bundle identifier in application plist file should be same** 
相關問題