How to check if a Facebook account exists?

I am trying to follow the Facebook posting procedure. Can you help me understand how to check if an account exists on the iPhone (iOS6 function). I saw a WWDC session where they used code like this:

if (self.accountStore == nil) { self.accountStore = [[ACAccountStore alloc] init]; } ACAccountType * facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; //Now we can obtain some extra permissions NSArray * permissions = @[@"publish_stream"]; //NSArray * permissions = @[@"user_about_me"]; NSDictionary * dict = @{ACFacebookAppIdKey : FB_APP_ID, ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceEveryone}; [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) { __block NSString * statusText = nil; if (granted) { statusText = @"Logged in"; NSArray * accounts = [self.accountStore accountsWithAccountType:facebookAccountType]; self.facebookAccount = [accounts lastObject]; NSLog(@"account is: %@", self.facebookAccount); self.statusLabel.text = statusText; [self postToFeed]; } else { self.statusLabel.text = @"Login failed"; NSLog(@"error is: %@", error); } }]; 

But if the account does not exist, I get the error message: Error Domain=com.apple.accounts Code=6 "The operation couldn't be completed Maybe I missed something ... but I don’t quite understand that I’m doing it. It will also be good if you post some guides or links on how to use these new iOS 6 features because facebook tutorials are not so clear.

+4
source share
2 answers

There are no built-in functions to check for an account. You must access the account as usual, intercept and process the error in the else branch. The type of error is ACErrorCode, and the possible values ​​are:

 typedef enum ACErrorCode { ACErrorUnknown = 1, ACErrorAccountMissingRequiredProperty, ACErrorAccountAuthenticationFailed, ACErrorAccountTypeInvalid, ACErrorAccountAlreadyExists, ACErrorAccountNotFound, ACErrorPermissionDenied, ACErrorAccessInfoInvalid } ACErrorCode; 

So your code might look like this:

 -(void)requestBasicPermissionsForFacebookAccount { ACAccountType * facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSArray * permissions = @[@"email"]; NSDictionary * options = @{ACFacebookAppIdKey : kFacebookAppId, ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceEveryone}; FacebookAccountManager * fbMgr = [[FacebookAccountManager alloc] init]; [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) { if (granted) { NSArray * accounts = [self.accountStore accountsWithAccountType:facebookAccountType]; fbMgr.account = [accounts lastObject]; fbMgr.isBasicPermissionsGranted = YES; [self.accountManagers addObject:fbMgr]; NSLog(@"granted!"); } else { fbMgr.account = nil; fbMgr.isBasicPermissionsGranted = NO; switch ([error code]) { case 1: [self showErrorAlertWithMessage:@"Unknown error occured, try again later!"]; break; case 3: [self showErrorAlertWithMessage:@"Authentication failed, try again later!"]; break; case 6: [self showErrorAlertWithMessage:@"Facebook account does not exists. Please create it in Settings and come back!"]; break; case 7: [self showErrorAlertWithMessage:@"Permission request failed. You won't be able to share information to Facebook"]; break; default: break; } NSLog(@"error is: %@", error); } }]; } - (void)showErrorAlertWithMessage:(NSString *)message { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil]; [alertView show]; }); } 
+7
source

Use the graphical API to extract information from this account name and view the response, if this does not exist, it will return an error, for more information, please check the facebook developer's site facebook facebook

Hello

+1
source

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


All Articles