Reputation:
I'm trying to implement Facebook posting procedure. Can you help me to understand how do I check if the account exists in the iPhone(iOS6 feature). I saw WWDC session where they use such code:
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 account does not exists I'm getting an error:
Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed
May be I missed something..but I do not exactly understand what should I do. Also it will be good if you post some tutorials or links of how to use this new iOS 6 features because facebook tutorials are not so clear.
Upvotes: 4
Views: 3689
Reputation: 7398
Use graph API to fetch details from that account name and see the response, if that doesnot exist it will return error, for further information please check facebook developer website facebook
Regards
Upvotes: 0
Reputation: 9935
There is no built in functionality to check the account existence. You should access account as usual and intercept and handle the error in else branch. The error type is ACErrorCode and the possible values are:
typedef enum ACErrorCode {
ACErrorUnknown = 1,
ACErrorAccountMissingRequiredProperty,
ACErrorAccountAuthenticationFailed,
ACErrorAccountTypeInvalid,
ACErrorAccountAlreadyExists,
ACErrorAccountNotFound,
ACErrorPermissionDenied,
ACErrorAccessInfoInvalid
} ACErrorCode;
So your code may 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];
});
}
Upvotes: 7