如何检查Facebook帐户是否存在?

时间:2012-11-19 11:00:33

标签: iphone objective-c facebook

我正在尝试实施Facebook发布程序。你能帮我理解如何检查iPhone中是否存在该帐户(iOS6功能)。我看到他们使用这样的代码的WWDC会话:

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

但如果帐户不存在我收到错误: Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed 可能是我错过了一些东西..但我并不完全明白我应该怎么做。如果你发布一些如何使用这个新的iOS 6功能的教程或链接也会很好,因为facebook教程不是那么清楚。

2 个答案:

答案 0 :(得分:7)

没有内置功能来检查帐户是否存在。您应该像往常一样访问帐户并拦截并处理else分支中的错误。错误类型是ACErrorCode,可能的值是:

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

所以你的代码可能如下所示:

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

答案 1 :(得分:1)

使用图形API从该帐户名称中获取详细信息并查看响应,如果不存在则会返回错误,有关详细信息,请查看Facebook开发人员网站 facebook

此致