【问题标题】:Get facebook user details in ios 6 sdk?在 ios 6 sdk 中获取 facebook 用户详细信息?
【发布时间】:2013-01-08 11:23:41
【问题描述】:

如何使用 ios 6 内置的 facebook 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} 

【问题讨论】:

  • 什么不起作用?准备好回答这个问题,因为您说您需要一个替代解决方案,我们需要以某种方式设计来克服您的错误和错误——哦,我们不知道它们!
  • 非常感谢,我用更多信息编辑了我的问题。

标签: iphone objective-c ios


【解决方案1】:

不确定这是否能解决问题,但您是否在应用的 AppName-Info.plist 文件中设置了 Facebook 应用 ID?

所需的密钥是FacebookAppID,其类型为String。 尝试在那里填写您的 App ID 并查看它是否有效。

【讨论】:

  • 我加了试试,还是不行。这次的错误是“Error Domain=com.apple.accounts Code=6”操作无法完成。 (com.apple.accounts 错误 6.)”。这部分在我的代码中是否正确。?ACFacebookPermissionsKey:@[@"email"]
  • 啊,好吧,如果用户拒绝访问应用程序,它看起来会抛出错误 7,如果用户没有在设备上设置 Facebook,则会抛出错误 6。您可以做的一件事是检查 if (error.code == 6) { // 向用户发出警报,告诉他们关联他们的帐户 }
  • 我找到了解决方案。我们必须在 facebook 开发者网站上的 facebook 应用程序中设置 bundle id 并启用本机 ios 应用程序。 stackoverflow.com/questions/12671420/…
【解决方案2】:

在 iOS6.0 中,你必须分别询问读写权限。 首先请求读取权限,即电子邮件,然后根据应用要求请求其他权限。

【讨论】:

    【解决方案3】:
    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**
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多