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.
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:
So your code may look like this: