twitterフレームワークで投稿してみる
twitterフレームワークを試してみます。
それで、twitterフレームワークですが、ios5から存在するもの?だったと思いますので、
ios4とかでは、自分で書かないといけないかもです。
それで、tweetのアイコンとかボタンとかに処理を紐付ける感じで作ろうと思います。
Storyboardのところは、省略してます...。
あ!それとですね、事前にiphoneの設定で、twitterのアカウントとパスワードを登録する必要があります。
あと、作るアプリがその設定したアカウント情報へアクセスできるようにしておく必要もあります。
まずは、ViewControllerインターフェイスにtweetする文字列を格納する変数と
tweet画面を呼び出すメソッドを定義します。
@interface ViewController : UIViewController { @protected UITextView *tweet; } @property (nonatomic, strong) IBOutlet UITextView *tweet; - (IBAction)tweet; @end
それで、実際の実装ですが、
twitter利用可能になってるか?twitter投稿画面表示とか、キャンセル時のイベントハンドラとか
書いて行きます。
twitter利用可能になっているかの確認方法ですが、
TWTweetComposeViewControllerのcanSendTweetを使えば分かります。
if ([TWTweetComposeViewController canSendTweet]) { }
次に、twitter投稿画面表示ですが、下記に記載したコードで表示できます。
if ([TWTweetComposeViewController class]) { TWTweetComposeViewController *controller = [[TWTweetComposeViewController alloc] init]; [self presentModalViewController:controller animated:YES];
それと、上のコードと組み合わせて、tweetする文章とか、キャンセルボタンとかのイベントハンドを
書いていく感じです。
最終的には、下のような感じです。
// Twitter利用可能チェック if ([TWTweetComposeViewController canSendTweet]) { // Twitter投稿画面表示 if ([TWTweetComposeViewController class]) { TWTweetComposeViewController *controller = [[TWTweetComposeViewController alloc] init]; // 投稿メッセージ初期設定 [controller setInitialText:tweet.text]; // イベントハンドラ controller.completionHandler = ^(TWTweetComposeViewControllerResult res) { if (res == TWTweetComposeViewControllerResultCancelled) { // なんらかの処理 } else if (res == TWTweetComposeViewControllerResultDone) { // なんらかの処理 } [self dismissModalViewControllerAnimated:YES]; }; [self presentModalViewController:controller animated:YES]; } }
これで、tweetすることができます。