Get Facebook uid from ACAccountStore in iOS?
Asked Answered
A

2

9

Hi i want to get the Facebook user's UID from ACAccountStore in iOS 6

    self.accountStore = [[ACAccountStore alloc]init];

    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSString *key = @"01234567890123";
    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];

                 //it will always be the last object with single sign on
                 self.facebookAccount = [accounts lastObject];

                 //i Want to get the Facebook UID and log it here

                 NSLog(@"facebook account =%@",self.facebookAccount);

             } else {
                 //Fail gracefully...
              NSLog(@"error getting permission %@",e);

             }
         }];

self.facebookAccount has UID, but i am unable to get it...

Architecture answered 19/2, 2013 at 7:25 Comment(0)
A
20

I wanted to get the UID without using the Facebook API. It can done by using the following method

    self.accountStore = [[ACAccountStore alloc]init];

    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSString *key = @"01234567890123";
    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];

                 //it will always be the last object with single sign on
                 self.facebookAccount = [accounts lastObject];

                 //i  got the Facebook UID and logged it here (ANSWER)

                  NSLog(@"facebook account =%@",[self.facebookAccount valueForKeyPath:@"properties.uid"]);

             } else {
                 //Fail gracefully...
              NSLog(@"error getting permission %@",e);

             }
         }];
Architecture answered 20/2, 2013 at 18:33 Comment(3)
Please note that the UID found in properties.uid can be different than the one returned in the /me graph call.Oralle
@Oralle I know this is an old thread but can you elaborate on why this is? Is it mentioned in the Apple docs somewhere?Jelks
It's a Facebook thing. They're moving towards app-specific user ids, which will be different than your canonical user id.Oralle
L
5

You need to call the Facebook Graph API; use the SLRequest as follows:

NSUrl requestURL = NSUrl.FromString("https://graph.facebook.com/me"); 
SLRequest sl = SLRequest.Create(SLServiceKind.Facebook, SLRequestMethod.Get, requestURL, null);

sl.Account = // your ACAccount object here

sl.PerformRequest((data, response, error) => {
    if (error == null) {
        // success! parse response
    }
    else {
        // handle failure
    }
});

That should do it!

Logbook answered 19/2, 2013 at 20:14 Comment(1)
hey thanks for the answer but i would like to get it without calling the Facebook Graph API.Architecture

© 2022 - 2024 — McMap. All rights reserved.