お久しぶりです。ジャマです。
今回はios5から新設されたTwitter.frameworkを使ったので紹介します。
Twitter.frameworkを使うと簡単にiphoneアプリからツイッターに
ポストしたりデータを取得したりできます。
今までxauth認証を使って大変な思いでツイッターと連携してたのが
嘘のようです。
私のリサーチによるとTwitter.frameworkでの連携は2種類の方法あります。
・TWTweetComposeViewController
・TWRequest
TWTweetComposeViewController
これはios標準のダイアログを出してそこからツイッターに投稿します。
流れとして
ボタン押す(自作ボタン)→ダイアログが出る→ツイッターに投稿
となります。
簡単に実装できるのですがダイアログから投稿しないといけないので、
自分の好きなタイミングで投稿することができません。
ボタン押す(自作ボタン)→ツイッターに投稿
といった流れで投稿したい場合は
TWRequest
を使います。
TWTweetComposeViewControllerに関しては
この辺を参考にして下さい。
http://araking56.blog134.fc2.com/blog-entry-172.html
TWRequestは直接apiを叩いて投稿します。
自分がネットで探して切り貼りして作った写真と文字を同時に投稿するソースをアップしてみます。
写真と文字を投稿する時のポイントとしては
①apiを叩くためのurlの指定
https://upload.twitter.com/1/statuses/update_with_media.json
②画像送る処理、文字を送る処理を行う
画像
[request addMultiPartData:imageData withName:@"media[]"type:@"multipart/form-data"];
文字
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding] withName:@"status"type:@"multipart/form-data"];
③画像サイズは3MBまで
こんな感じで自作のボタンのアクションに投稿処理を書いておくと
ダイアログを経由せずにツイッターに投稿することができます。
ソースです。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
ACAccountStore *account = [[ACAccountStorealloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
NSURL *url =
[NSURLURLWithString:
@"https://upload.twitter.com/1/statuses/update_with_media.json"];
TWRequest *request =
[[TWRequestalloc] initWithURL:url parameters:nil
requestMethod:TWRequestMethodPOST];
[request setAccount:acct];
UIImage *image = [UIImage imageNamed:@"test.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[request addMultiPartData:imageData
withName:@"media[]"type:@"multipart/form-data"];
NSString *status = @"投稿テスト";
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
withName:@"status"type:@"multipart/form-data"];
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *dict =
(NSDictionary *)[NSJSONSerialization
JSONObjectWithData:responseData options:0error:nil];
NSLog(@"%@", dict);
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
}];
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
参考サイト
http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-1.html
http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-2.html
https://dev.twitter.com/docs/ios/posting-images-using-twrequest
今回はios5から新設されたTwitter.frameworkを使ったので紹介します。
Twitter.frameworkを使うと簡単にiphoneアプリからツイッターに
ポストしたりデータを取得したりできます。
今までxauth認証を使って大変な思いでツイッターと連携してたのが
嘘のようです。
私のリサーチによるとTwitter.frameworkでの連携は2種類の方法あります。
・TWTweetComposeViewController
・TWRequest
TWTweetComposeViewController
これはios標準のダイアログを出してそこからツイッターに投稿します。
流れとして
ボタン押す(自作ボタン)→ダイアログが出る→ツイッターに投稿
となります。
簡単に実装できるのですがダイアログから投稿しないといけないので、
自分の好きなタイミングで投稿することができません。
ボタン押す(自作ボタン)→ツイッターに投稿
といった流れで投稿したい場合は
TWRequest
を使います。
TWTweetComposeViewControllerに関しては
この辺を参考にして下さい。
http://araking56.blog134.fc2.com/blog-entry-172.html
TWRequestは直接apiを叩いて投稿します。
自分がネットで探して切り貼りして作った写真と文字を同時に投稿するソースをアップしてみます。
写真と文字を投稿する時のポイントとしては
①apiを叩くためのurlの指定
https://upload.twitter.com/1/statuses/update_with_media.json
②画像送る処理、文字を送る処理を行う
画像
[request addMultiPartData:imageData withName:@"media[]"type:@"multipart/form-data"];
文字
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding] withName:@"status"type:@"multipart/form-data"];
③画像サイズは3MBまで
こんな感じで自作のボタンのアクションに投稿処理を書いておくと
ダイアログを経由せずにツイッターに投稿することができます。
ソースです。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
ACAccountStore *account = [[ACAccountStorealloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
NSURL *url =
[NSURLURLWithString:
@"https://upload.twitter.com/1/statuses/update_with_media.json"];
TWRequest *request =
[[TWRequestalloc] initWithURL:url parameters:nil
requestMethod:TWRequestMethodPOST];
[request setAccount:acct];
UIImage *image = [UIImage imageNamed:@"test.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[request addMultiPartData:imageData
withName:@"media[]"type:@"multipart/form-data"];
NSString *status = @"投稿テスト";
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
withName:@"status"type:@"multipart/form-data"];
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *dict =
(NSDictionary *)[NSJSONSerialization
JSONObjectWithData:responseData options:0error:nil];
NSLog(@"%@", dict);
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
}];
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
参考サイト
http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-1.html
http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-2.html
https://dev.twitter.com/docs/ios/posting-images-using-twrequest