简体   繁体   中英

Get facebook user details in ios 6 sdk?

How to retrieve facebook user details with ios 6 inbuilt facebook sdk? I tried few examples, but couldn't get work.

- (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);

}];

}

But this fails to grab data from facebook. My app id is correct.

This the error message I got

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} 

Not sure if this will fix it or not, but have you set the Facebook App ID in your AppName-Info.plist file of your app?

The key required is FacebookAppID , which is of type String . Try filling in your App ID there as well and see if it works.

In iOS6.0 ,you have to ask read and write permissions separately. First ask for read permissions that is email then ask for other permissions according to the app requirement.

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**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM