4

我正在尝试实施 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 教程不是很清楚。

4

2 回答 2

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];
    });
}
于 2012-11-22T09:27:16.990 回答
1

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

问候

于 2012-11-19T13:15:14.967 回答