0

如何使用 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} 
4

3 回答 3

1

不确定这是否会修复它,但您是否在应用程序的 AppName-Info.plist 文件中设置了 Facebook 应用程序 ID?

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

于 2013-01-09T04:47:16.287 回答
0

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

于 2013-09-09T11:28:48.627 回答
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**
于 2015-01-07T17:05:49.217 回答