Search code examples
iphoneobjective-cfacebookios6

Facebook iOS 6 - get user info


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.


Solution

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