获取的Facebook从ACAccountStore的uid iOS中?(Get Facebook

2019-08-17 01:46发布

嗨,我想从ACAccountStore了Facebook用户的UID在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有UID,但我无法得到它...

Answer 1:

我想获得的UID不使用Facebook的API。 它可以通过以下方法来完成

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

             }
         }];


Answer 2:

你需要调用Facebook的图形API ; 使用SLRequest如下:

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

应该这样做!



文章来源: Get Facebook uid from ACAccountStore in iOS?