我一直在尝试使用新的 Social.Framework,尤其是 SLRequest,它们都可在 iOS 6 及更高版本上使用。问题是,我对在尝试发布此类请求时遇到的崩溃感到非常惊讶。
我的 Facebook 和 Twitter 帐户都遇到过崩溃,所以我知道这与其中任何一个的任何特定问题无关。它必须与我以这种方式获得的 ACAccount 对象相关:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
//iOS 6
if (_twitterEnabled) {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
//Set up account
[accountStore requestAccessToAccountsWithType:accountTypeTwitter options:nil completion:^(BOOL granted, NSError *error) {
if (granted && !error) {
//Get account and get ready to post.
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountTypeTwitter];
if ([arrayOfAccounts count] > 0) {
_twitterAccount = [arrayOfAccounts lastObject];
}
}
}];
}
}
if (!_facebookEnabled) {
ACAccountType *accountTypeFacebook = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSArray *permissions = @[@"user_about_me",@"user_likes",@"email"];
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:kFacebookAppIDString, ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[accountStore requestAccessToAccountsWithType:accountTypeFacebook options:options completion:^(BOOL granted, NSError *error) {
if (granted && !error) {
[options setObject:@[@"publish_stream",@"publish_actions"] forKey:ACFacebookPermissionsKey];
[accountStore requestAccessToAccountsWithType:accountTypeFacebook options:options completion:^(BOOL granted, NSError *error) {
if (granted && !error) {
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountTypeFacebook];
if ([arrayOfAccounts count] > 0) {
_fbAccount = [arrayOfAccounts lastObject];
}
}
}];
}
}];
}
}
}
_twitterAccount 和 _fbAccount 都是 ACAccount 对象,我在其中存储从帐户存储中检索到的相关帐户。
后来我尝试使用这些对象时出现了问题(为了简洁起见,我只发布 twitter 方法):
NSDictionary *twitterMsg = @{@"status" : message};
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:kTwitterUpdateStatusURL parameters:twitterMsg];
[postRequest setAccount:_twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter HTTP response: %d", [urlResponse statusCode]);
}];
}
当在 postRequest 上调用 setAccount: 时,我收到一条异常消息:“此请求的帐户类型无效”,这显然是错误的。我还尝试调试代码,但奇怪的是,_twitterAccount 上的 accountType 在发送到 ACAccount 对象之前被设置为 nil。更奇怪的是,如果我把
NSLog(@"%@",_twitterAccount);
就在
_twitterAccount = [arrayOfAccounts lastObject]
在第一段代码中,它没有问题。
我检查了我的代码,我不认为我做错了什么,所以我认为它可能是框架上的错误?看起来 ACAccountType 在不应该发布的时候被发布了,但我想检查你们中的任何人是否可以看到我的代码有什么问题是引发它和/或找到问题的解释,我无法自己解决。
谢谢
更新
似乎其他人也有同样的问题,我接受其中一个答案,因为它实际上解决了问题,但我期待任何人能找到问题的解释。
最佳答案
在通过以下两种方式检索的帐户上使用 SLRequest 时,我也收到“此请求的无效帐户类型”
ACAccountStore *account_store = [[ACAccountStore alloc] init];
ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// A.
NSArray *accounts = [account_store accountsWithAccountType:account_type_twitter];
// B.
NSString *account_id = @"id from doing account.identifier on one of the above";
ACAccount *account = [account_store accountWithIdentifier:account_id];
帐户上的 NSLog 已按预期填充所有内容,但类型设置为空。猜测这是苹果SDK的一个bug。执行以下操作为我修复了帐户,以便我可以将它们与 SLRequest 一起使用:
ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
account.accountType = account_type_twitter;
关于iphone - 尝试使用 Twitter ACAccount 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13349187/