ale*_*0xB 19 iphone core-services ios ios6 social-framework
我一直在使用新的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];
}
}
}];
}
}];
}
}
}
Run Code Online (Sandbox Code Playgroud)
_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]);
}];
}
Run Code Online (Sandbox Code Playgroud)
在postRequest上调用setAccount:时,我收到一条消息:"此请求的帐户类型无效",这显然是错误的.我也尝试调试代码,奇怪的是_twitterAccount上的accountType在发送到ACAccount对象之前设置为nil.更奇怪的是,如果我放
NSLog(@"%@",_twitterAccount);
Run Code Online (Sandbox Code Playgroud)
正确的
_twitterAccount = [arrayOfAccounts lastObject]
Run Code Online (Sandbox Code Playgroud)
在代码的第一部分,它没有问题.
我已经查看了我的代码,我认为我没有做错什么,所以我认为它可能是框架上的错误?看起来ACAccountType正在发布它不应该发布,但是我想检查一下你是否有人发现我的代码有什么问题引起了它并且/或找到了解决问题的方法,我无法解决自己解决.
谢谢
UPDATE
似乎其他一些人有同样的问题,我接受其中一个答案,因为它实际上解决了这个问题,但我会期待任何可以找到问题解释的人.
Ste*_*ley 31
在通过以下两个方法检索的帐户上使用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];
Run Code Online (Sandbox Code Playgroud)
帐户上的NSLog已按预期填充所有内容,但类型设置为null.猜猜这是Apple的SDK的一个错误.执行以下操作为我修复了帐户,以便我可以将它们与SLRequest一起使用:
ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
account.accountType = account_type_twitter;
Run Code Online (Sandbox Code Playgroud)
Gin*_*ane 17
您必须保留ACAccountStore:
@property (nonatomic, strong) ACAccountStore *accountStore;
Run Code Online (Sandbox Code Playgroud)
ACAccount文档从未明确指出您必须保留ACAccountStore,但它确实说明了这一点
"要从Accounts数据库创建和检索帐户,您必须创建一个ACAccountStore对象.每个ACAccount对象属于一个ACAccountStore对象."
你打电话的时候:
NSArray *accounts = [accountStore accountsWithAccountType:accountType]
Run Code Online (Sandbox Code Playgroud)
数组中的那些accountStore对象不一定从数据库中获取所有属性.如果需要,仅从Accounts数据库中检索某些属性(如accountType).这导致您在登录帐户时看到的奇怪行为,并且一切都神奇地起作用.记录帐户时,ACAccountStore对象仍在内存中,日志记录导致检索AccountType属性.此时,AccountType与ACAccount一起保留,即使在ACAccountStore发布后,一切都能够工作.
| 归档时间: |
|
| 查看次数: |
5506 次 |
| 最近记录: |