トップ «前の日記(2012-05-01) 最新 次の日記(2012-05-03)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-05-02 [iOS]Tweeting(アカウント管理)

独自にOAuth/xAuthに対応する場合は、アプリケーションはアカウントに対応したアクセストークンを取得して、これを使ってアクセスすることになる。

iOS5から用意されたTwitter/Accounts frameworkを利用する場合、そもそも管理されているTwitterアカウントが複数あり、ユーザがそれのどれを選択したのか管理する必要がある。

それが、ACAccountのプロパティidentifierだ。

- (IBAction)tweet2:(id)sender
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
	
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
	
    [accountStore requestAccessToAccountsWithType:accountType
            withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
			
            for (NSUInteger i = 0; i < [accountsArray count]; i++) {
                ACAccount *twitterAccount = [accountsArray objectAtIndex:i];
                NSLog(@"account: %@", twitterAccount);
				
                TWRequest *postRequest = [[TWRequest alloc]
                    initWithURL:
                    [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
                    parameters:[NSDictionary dictionaryWithObject:@"hello, world" forKey:@"status"]
                    requestMethod:TWRequestMethodPOST];
				
                [postRequest setAccount:twitterAccount];
                
                NSLog(@"credential: %@", twitterAccount.credential);
                NSLog(@"identifier: %@", twitterAccount.identifier);
				
                [postRequest performRequestWithHandler:
                        ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %i",
                        [urlResponse statusCode]];
                    NSLog(@"%@", output);
                    [self performSelectorOnMainThread:@selector(displayText:)
                        withObject:output waitUntilDone:NO];
                }];
            }
        }
	}];
}

ACAccountのプロパティidentifierを保存しておけば、ACAccountStoreクラスの - (ACAccount *)accountWithIdentifier:(NSString *)identifier メソッドを使えば、ACAcountクラスのインスタンスを得られる。

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/Tweets - GitHub

_ 関連情報

iOS Twitter framework
Twitter Developersサイトの情報。
Tweeting
Developerサイトのサンプル・コード

トップ «前の日記(2012-05-01) 最新 次の日記(2012-05-03)» 編集