15

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.

Daniel
  • 23,129
  • 12
  • 109
  • 154
Buyin Brian
  • 2,781
  • 2
  • 28
  • 48

2 Answers2

31

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

    }];

}
Daniel
  • 23,129
  • 12
  • 109
  • 154
  • I used upper code the code first time works fine for me. But next time when I unlinked the fb account and then again linked then getting below errors { error = { code = 190; message = "The access token was invalidated on the device."; type = OAuthException; }; } – NSCry Dec 30 '12 at 04:15
  • @NSIllusion did you solve the problem of the access token here? – user717452 Jan 09 '13 at 03:04
  • 1
    @user717452 Almost solved but did not finding any good answer looking wired to me. My problem solved by resetting simulator content and settings. – NSCry Jan 09 '13 at 05:50
  • Working well. If not then follow my answer on this post: http://stackoverflow.com/questions/12539549/retrieve-facebook-account-information-in-ios-6/15946067#15946067 – Vaibhav Saran Apr 11 '13 at 10:13
  • 1
    try the correct answer of this link. it worked for me. http://stackoverflow.com/questions/12601191/facebook-sdk-3-1-error-validating-access-token – Vibhooti Nov 27 '13 at 09:53
  • how do ou get the user picture using SLRequest? URL = https://graph.facebook.com/me/picture?height=512&width=512 is not working – João Nunes Oct 31 '14 at 14:04
0

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];
        }
    });
João Nunes
  • 3,751
  • 31
  • 32