IOS to login to Facebook (without Facebook SDK)

I am trying to use my own (iOS 6-7x) libraries to authorize a user with Facebook from my application. I would like to transfer the authorization token to my server when the login is successful.

The code below works fine if the user has not set up a Facebook account on the OS. In this case, I get the following error:

Domain error = com.apple.accounts Code = 6 "Operation could not be completed (com.apple.accounts 6. error)

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

Should I use the Facebook SDK to ask the user to login? Is there any other native iOS library that I could use to encourage a user to configure access to Facebook on iOS?

OR, is there a better way to do simplified Facebook authentication (without asking the user about the login if they have already done so on the OS)?

+6
source share
1 answer

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

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

In my opinion, I would use the SDK to register 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 must configure the SDK for Facebook in appDelegate and in the .plist file.

Your code will only work if the user has a Facebook account set in the device’s settings.

I am.

0
source

Source: https://habr.com/ru/post/957654/


All Articles