Get Facebook uid from ACAccountStore in iOS?

我的梦境 提交于 2019-12-03 13:05:47

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

             }
         }];

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!