Search code examples
iphonexcodefacebookios6social-framework

Get Facebook uid from ACAccountStore in iOS?


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


Solution

  • 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);
    
                 }
             }];