19

私は新しい Social.Framework と、特に iOS 6 以降で利用可能な SLRequest を少し使ってみました。実は、そのようなリクエストを投稿しようとしたときに発生していたクラッシュに本当に驚きました.

Facebook と Twitter の両方のアカウントでクラッシュが発生していたので、そのうちの 1 つの特定の問題とは関係がないことがわかったのです。それは、私がこの方法で取得している 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: を呼び出すと、「このリクエストには無効なアカウント タイプです」というメッセージで例外が発生しましたが、これは明らかに誤りでした。また、コードをデバッグしようとしましたが、不思議なことに、ACAccount オブジェクトに送信される直前に _twitterAccount の accountType が nil に設定されていました。もっと奇妙なことに、私が置くと

NSLog(@"%@",_twitterAccount);

真下

_twitterAccount = [arrayOfAccounts lastObject]

コードの最初のセクションでは、問題なく動作します。

私は自分のコードを見直しましたが、何か間違ったことをしているとは思わないので、フレームワークのバグであると思いますか? ACAccountType がリリースされるべきではないときにリリースされているようですが、それを引き起こしている私のコードに何か問題があるかどうか、および/または問題の説明を見つけられるかどうかを確認したかったのですが、私にはわかりません自分で解決。

ありがとうございました

アップデート

他の人も同じ問題を抱えているようです。実際に問題を解決するので、回答の1つを受け入れていますが、問題の説明を見つけることができる人を楽しみにしています.

4

4 に答える 4

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];

アカウントの NSLog にはすべてが期待どおりに入力されていましたが、タイプは null に設定されていました。これは Apple の SDK のバグだと思います。以下を実行すると、アカウントが修正され、SLRequest で使用できるようになりました。

ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
account.accountType = account_type_twitter;
于 2012-11-22T07:16:01.737 に答える
17

ACAccountStore を保持する必要があります。

@property (nonatomic, strong) ACAccountStore *accountStore;

ACAccount のドキュメントには、ACAccountStore を保持する必要があるとは特に記載されていませんが、次のように記載されています。

「アカウント データベースからアカウントを作成および取得するには、ACAccountStore オブジェクトを作成する必要があります。各 ACAccount オブジェクトは、単一の ACAccountStore オブジェクトに属します。」

電話すると:

NSArray *accounts = [accountStore accountsWithAccountType:accountType]

配列内のそれらの accountStore オブジェクトは、必ずしもデータベースからフェッチされたすべてのプロパティを持っているわけではありません。一部のプロパティ (accountType など) は、必要な場合にのみ Accounts データベースから取得されます。これにより、アカウントをログインしたときに見た奇妙な動作が発生し、すべてが魔法のように機能しました. アカウントをログに記録したとき、ACAccountStore オブジェクトはまだメモリ内にあり、ログによって AccountType プロパティが取得されました。その時点で、AccountType は ACAccount で保持され、ACAccountStore がリリースされた後でもすべてが機能するようになりました。

于 2013-01-11T20:15:16.577 に答える
5

arraryOfAccounts をフェッチするために使用した ACAccountStore を保持していますか?

于 2012-11-17T06:47:05.890 に答える
1

このコードは問題なく使用できます。(ツイッターのみ)

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
    if (!granted) {
        NSLog(@"Access denied.");
    }
    else {
        NSArray *accounts = [accountStore accountsWithAccountType:accountType];
        if (accounts.count > 0) {
            twitterAccount = [accounts objectAtIndex:0];
        }
    }
}];

accountStoreの初期化の問題について何か考えていますか?

Twitterの公式リファレンスはこちらです。 Twitter: TWRequest を使用した API リクエスト

于 2012-11-14T20:42:30.860 に答える