简体   繁体   中英

facebook login using SDK

I am able to get the user profile permissions if user has set facebook account in his iPhone settings. For this I have used following code:

if(!_accountStore)
    _accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                       options:@{ACFacebookAppIdKey: @"app_id", ACFacebookPermissionsKey: @[@"email"]}
                                    completion:^(BOOL granted, NSError *error) {
                                        if(granted){
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
                                            _facebookAccount = [accounts lastObject];
                                            NSLog(@"Success");

                                            [self me];
                                        }else{
                                           NSLog(@"go to iphone settings");

                                        }
                                    }];






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

    }];   
}

If user hasn't set his facebook account then control will move to else part ie go to iphone settings. In this else part I want to open the facebook login page so that user will be able to login in his facebook account without going to iPhone settings. SO how could we open the facebook login via SDK. Please help me out from this issue.

Thanks in advance.

In this case, if you're not using the user account (in case he/she doesn't have it set up on the iOS Settings), you need to implement the Facebook SDK ( https://developers.facebook.com/ios/ ) on your application. It's easy, just follow these steps ( https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/ ).

After that, you can use FBSession's method +openActiveSessionWithReadPermissions:allowLoginUI:completionHandler: to perform the user login.

To be true, in my application, I don't even try to login the user with Apple's requestAccessToAccountsWithType:options:completion:; with the newest Facebook SDK, the default behavior is:

  1. Tries to login with the user account setup on the iOS Settings;
  2. If no account is found, tries to open the Facebook application to request login / permissions;
  3. If the Facebook app is not installed on the device, it opens Facebook on Safari, as a fallback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM