我使用以下代码(在WWDC 2012视频中显示):
self.accountStore = [[ACAccountStore alloc] init]; ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; [self.accountStore requestAccessToAccountsWithType:facebookAccountType withCompletionHandler:^(BOOL granted,NSError *e) { if (granted) { NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType]; self.facebookAccount = [accounts lastObject]; } else { // Fail gracefully... } }];
我还在我的.plist文件中添加了NSDictionary:
所以,我的问题是我收到以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: 'Access options are required for this account type.'
我尝试过这个ACAccountTypeIdentifierTwitter和ACAccountTypeIdentifierSinaWeibo.我没有收到任何例外,虽然他们总是返回授予==否
解决方法
好吧,WWDC 2012显示了一件事,但
documentation显示了另一个……他们正在使用的方法现在已被弃用:
– requestAccessToAccountsWithType:withCompletionHandler: Deprecated in iOS 6.0
你应该做什么:
ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *options = @{ @"ACFacebookAppIdKey" : @"123456789",@"ACFacebookPermissionsKey" : @[@"publish_stream"],@"ACFacebookAudienceKey" : ACFacebookAudienceEveryone}; // Needed only when write permissions are requested [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted,NSError *error) { if (granted) { NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType]; self.facebookAccount = [accounts lastObject]; } else { NSLog(@"%@",error); // Fail gracefully... } }];