iOS Facebook native login (without Facebook SDK)

痞子三分冷 提交于 2019-12-06 02:34:17

问题


I am trying to use native (iOS 6-7x) libraries to authorize a user with Facebook from my app. I would like to pass the auth token to my server when login is successful.

The code below works fine except when the user has not set up their Facebook account in the OS. I get the following error in this case:

Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed. (com.apple.accounts error 6.)

-(void) initFacebookLogin
{
    LRAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

    if (appDelegate.accountStore == nil)
        appDelegate.accountStore = [[ACAccountStore alloc] init];

    __block ACAccount *facebookAccount = nil;

    ACAccountType *facebookAccountType = [appDelegate.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSArray * permissions = @[@"publish_stream", @"publish_actions", @"email"];

    NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:FACEBOOK_APP_ID, ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];

    [appDelegate.accountStore requestAccessToAccountsWithType:facebookAccountType
        options: options
        completion: ^(BOOL granted, NSError *error) {

           if ( granted )
           {
               NSArray *accounts = [appDelegate.accountStore accountsWithAccountType:facebookAccountType];

               facebookAccount = [accounts lastObject];

               ACAccountCredential* accountCredential = [facebookAccount credential];

               NSString* token = [accountCredential oauthToken];

               NSLog( @"token=%@", token );
           }
           else {
               // WHAT DO I DO HERE???
               // Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed. (com.apple.accounts error 6.)"
               NSLog(@"%@", [error description]);
           }
       }];
}

Do I still need to use the Facebook SDK to ask the user to log in? Is there another iOS native library I could use to prompt the user to set up Facebook access in iOS?

OR, is there a better way to do streamlined Facebook auth (not asking the user to log in if they have already done so in the OS)?


回答1:


You should take a look at Facebook docs here (there's a simple login example):

https://developers.facebook.com/docs/ios/ios-sdk-tutorial/authenticate/

In my opinion, I would use the Facebook SDK to perform the login, you can do something like this:

/* Constants */
#define K_FACEBOOK_REQUEST_CREDENTIALSURL @"me/?fields=id,location,name,username,bio,picture,gender,website,birthday,email"
#define K_FACEBOOK_PERMISSIONS @[@"user_about_me",@"user_birthday",@"email"]

/* Facebook Keys */
#define K_FACEBOOK_REQUEST_ID @"id"
#define K_FACEBOOK_REQUEST_USERNAME @"username"
#define K_FACEBOOK_REQUEST_LOCATION @"location"
#define K_FACEBOOK_REQUEST_FULLNAME @"name"
#define K_FACEBOOK_REQUEST_BIO @"bio"
#define K_FACEBOOK_REQUEST_WEBSITE @"website"
#define K_FACEBOOK_REQUEST_EMAIL @"email"
#define K_FACEBOOK_REQUEST_BIRTHDAY @"birthday"
#define K_FACEBOOK_REQUEST_GENDER @"gender"

- (void)initWithLoginFacebook
{
  [PFFacebookUtils logInWithPermissions:K_FACEBOOK_PERMISSIONS block:^(PFUser *user, NSError *error) {
    if (!user) {
        if (!error) 
          NSLog("ERROR_CAUSE_AUTH_USERCANCELLED");
        else 
          NSLog("ERROR_CAUSE_AUTH_FAILEDLOGIN");
    } else if (user.isNew) {
        FBRequest *request = [FBRequest requestForMe];
        [request startWithCompletionHandler:^(FBRequestConnection *connection,id result,NSError *error) {
            if (error) 
              NSLog("ERROR_CAUSE_AUTH_FAILEDLOGIN");
            else 
              [self requestLoginFacebook:result];
        }];
    } else
        NSLog("User logged and not new!");
  }];
}

- (void)requestLoginFacebook:(id)result
 {
    NSDictionary *userData = (NSDictionary *)result;
    // Do something with the data... For example
    NSString *facebookId = userData[K_FACEBOOK_REQUEST_ID];
    NSString *name = userData[K_FACEBOOK_REQUEST_FULLNAME];
    NSString *username = [userData[K_FACEBOOK_REQUEST_USERNAME] lowercaseString];
    NSString *bio = userData[K_FACEBOOK_REQUEST_BIO];
    NSString *website = userData[K_FACEBOOK_REQUEST_WEBSITE];
    NSString *location = userData[K_FACEBOOK_REQUEST_LOCATION][@"name"];
    NSString *gender = userData[K_FACEBOOK_REQUEST_GENDER];
    NSString *birthday = userData[K_FACEBOOK_REQUEST_BIRTHDAY];
    NSString *email = userData[K_FACEBOOK_REQUEST_EMAIL];
    NSString *profileImage = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=180&height=180",userData[@"id"]];

}

You have to configure the Facebook SDK in the appDelegate and in the .plist file.

Your code only will work fine if the user has a Facebook account set in the device Settings.

I.



来源:https://stackoverflow.com/questions/19853865/ios-facebook-native-login-without-facebook-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!