アプリなどを開発するブログ

React Native / Swift / Ruby on Railsなどの学習メモ。


SLRequest に setAccountすると "Invalid account type for this request"というエラーが出る場合の対処法

iOSアプリ内にTwitterアカウントを保持しておく仕様のアプリを作っている時に、
アプリを閉じて復旧した際にNSUserDefaultに保存しておいた
ACAccount.identifierからアカウントを復帰させると、
TweetしようとしてSLRequestにsetAccountすると
"Invalid account type for this request"とエラーが出る。

全く原因がわからないのでググるとStack Overflowに回答が。

どうやらバグとのこと。

解決方法

identifierからアカウントを復旧させた後、accountTypeを指定してあげる必要があるようです。

ACAccountStore *account_store = [[ACAccountStore alloc] init];
ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

NSString *account_id = @"アカウントのID";

ACAccount *account = [account_store accountWithIdentifier:account_id];
ACAccountType *account_type_twitter = [account_store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
account.accountType = account_type_twitter;