iOS 5 Beta の Twitter.frameworkを試してみた

2011年06月09日 14時11分31秒 | iPhone

XCode 4.2 Betaをインストールすると iOS 5用に新しいframework が幾つか追加されます。

この中の「Twitter.framework」を使って見たところ、簡単に自作アプリへの組込ができるので紹介致します。

XCode 4.2 Betaを起動し新しいプロジェクトを作成するか既存のプロジェクトを開きます。

そこに 「Twitter.framework」を追加します。


私はテスト用にUITextViewとUIBottonを配置しました。


ヘッダーに下記の様に記述します。

 


 

#import

@interface TwTestViewController : UIViewController {

    // テキスト入力のOutlet 定義

    UITextView *strTweet;

}

 

// テキスト入力のOutlet 定義

@property (nonatomic, strong) IBOutlet UITextView *strTweet;

 

// ボタンのアクション定義

- (IBAction)Send:(id)sender;


 

上記定義と画面デザインを関連つけます。

strTweet -> outlet -> UITextView
Send -> Action -> UIBotton

- (IBAction)Send モジュールを下記の様に記述します。

 


// Twitter 送信ボタンの処理

- (IBAction)Send:(id)sender {

    // iOS5 Twitter Class初期

    TWTweetComposeViewController *twclass = [[TWTweetComposeViewController alloc] init];

 

    // 送信文字列セット

    [twclass setInitialText:strTweet.text];

 

    // [CANCEL]ボタンなどのイベントハンドラ定義

    twclass.completionHandler = ^(TWTweetComposeViewControllerResult res) {

        [self dismissModalViewControllerAnimated:YES];

    };

 

    // 送信View表示

    [self presentModalViewController:twclass animated:YES];

}


これだけで完了です。

動作画面は下記の様になります。



テキストを入力して[Send]ボタンを押します。

iOS 5のTwitter送信Viewが表示され、文字数などを確認して[Send]ボタンを押すと送信されます。





イメージやURLを添付したい場合には、上記コードの //送信文字列セット 付近に

[twclass addImage:(UIImage *)image];



[twclass addURL:(NSURL *)url];

で添付ができます。

 


最新の画像もっと見る

コメントを投稿