Facebook iOS 6 - get user info

2019-01-13 11:35发布

问题:

Is it possible to use the built-in iOS 6 Facebook integration to get the user's basic info (email address, birthday, etc)? All of the documentation/examples I have seen use the iOS 6 integration to simply open an SLComposeViewController.

Thank you for your time.

回答1:

Please check out my sample project. It allows you to upload video to Facebook, but it also includes a method to get your info, you should look at the file ViewController.m, the one noted "Native" in the tab controller.

https://bitbucket.org/danielphillips/fb-video-upload

You will need to import the Social and Accounts frameworks to do what you want. You request access to the users Facebook account from the ACAccountStore, if you are granted access, then you use this account to create an SLRequest with the parameters you want, here you want the graph object "/me".

Properties:

@property (nonatomic, retain) ACAccountStore *accountStore;
@property (nonatomic, retain) ACAccount *facebookAccount;

Authenticate:

- (IBAction)getMeButtonTapped:(id)sender {
    if(!_accountStore)
        _accountStore = [[ACAccountStore alloc] init];

    ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    [_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                           options:@{ACFacebookAppIdKey: @"483616868329082", 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);
                                            }
                                        }];
}

Get "me":

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

    }];

}


回答2:

https://github.com/jonasman/JNFacebookDownload

 self.facebookDownload = [JNFacebookDownload new];
 self.facebookDownload.appID = @"380637545425915";

[self.facebookDownload downloadInformation:^(NSDictionary *userInfo, NSError *error) {

    dispatch_async(dispatch_get_main_queue(), ^{

        if (error)
        {
            if (error.code == JNFacebookDownloadNoAccount)
                NSLog(@"No account");
            else if (error.code == JNFacebookDownloadNoPermissions)
                NSLog(@"No permissions");
            else if (error.code == JNFacebookDownloadNoAPPID)
                NSLog(@"No APP ID Configured");
        }
        else
        {
            self.textView.text = [userInfo description];
        }
    });