首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用Twitter ACAccount时的奇怪行为

尝试使用Twitter ACAccount时的奇怪行为
EN

Stack Overflow用户
提问于 2012-11-12 18:16:16
回答 4查看 5.5K关注 0票数 19

我一直在使用新的Social.Framework,特别是SLRequest,这两个版本都可以在iOS 6和更高版本上使用。问题是,当我试图发布这样的请求时,我对一次撞车事件感到非常惊讶。

我在Facebook和Twitter账户上都遭遇了崩溃,这就是为什么我知道这与其中一个账户的任何特定问题无关。它必须与ACAccount对象相关,我正在以这样的方式获得该对象:

代码语言:javascript
复制
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对象,当从account Store检索时,我在其中存储相关的帐户。

当我后来尝试使用这样的对象时,问题就出现了(为了简洁起见,我将只发布twitter方法):

代码语言:javascript
复制
        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]);
            }];
        }

在调用setAccount: on postRequest时,我得到了一个异常消息:“这个请求的帐户类型无效”,这显然是假的。我还尝试调试代码,奇怪的是,在发送到accountType对象之前,_twitterAccount上的ACAccount设置为0。更奇怪的是,如果我

代码语言:javascript
复制
NSLog(@"%@",_twitterAccount);

就在下面

代码语言:javascript
复制
_twitterAccount = [arrayOfAccounts lastObject]

在代码的第一部分,它的工作没有问题。

我已经检查了我的代码,我不认为我做错了什么,所以我认为它可能是框架上的一个bug?看起来ACAccountType是在不应该发布的时候发布的,但是我想检查一下,你们中是否有人能看到我的代码有什么问题引起了它和/或找到了这个问题的解释,这是我自己解决不了的。

谢谢

更新

似乎其他人也有同样的问题,我接受其中的一个答案,因为它实际上解决了这个问题,但我期待着任何能为这个问题找到解释的人。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-11-22 07:16:01

在通过以下两种方法检索的帐户上使用SLRequest时,我还得到了“此请求的无效帐户类型”

代码语言:javascript
复制
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按预期填充了所有内容,但类型被设置为null。猜测这是苹果SDK的一个bug。执行以下操作为我修复了帐户,这样我就可以在SLRequest中使用它们了:

代码语言:javascript
复制
ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
account.accountType = account_type_twitter;
票数 31
EN

Stack Overflow用户

发布于 2013-01-11 20:15:16

您必须保留ACAccountStore:

代码语言:javascript
复制
@property (nonatomic, strong) ACAccountStore *accountStore;

ACAccount文档从未明确规定您必须保留ACAccountStore,但它确实声明

要从帐户数据库中创建和检索帐户,必须创建一个ACAccountStore对象。每个ACAccount对象都属于单个ACAccountStore对象。

当你打电话:

代码语言:javascript
复制
NSArray *accounts = [accountStore accountsWithAccountType:accountType]

数组中的那些accountStore对象不一定都从数据库中获取它们的所有属性。有些属性(如accountType)仅在需要时才从Account数据库中检索。这导致了您在登录帐户时所看到的奇怪行为,所有的事情都神奇地正常工作。记录帐户时,ACAccountStore对象仍在内存中,日志记录导致检索AccountType属性。此时,AccountType被保留在ACAccount中,即使在ACAccountStore发布之后,一切都能够工作。

票数 17
EN

Stack Overflow用户

发布于 2012-11-17 06:47:05

您是否保留了用于获取ACAccountStore的arraryOfAccounts?

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13349187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档