Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
iPhone Development How-To
目標:寫一個可以選擇心情狀態的 app,然後按下按鈕後自動發送訊息至
twitter
Step1. 打開 Xcode,選擇 View-Based Applicaiton,取名
為 InstaT...
Step3. 修改 InstatwitViewController.h
#import <UIKit/UIKit.h>

@interface InstatwitViewController :
UIViewController <UIPick...
(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:
(...
@"sleeping",@"eating",@"working",@"thinking",@"crying"
,@"begging",@"leaving",@"shopping",@"hello
worlding",nil];

    fee...
step7. 修改 InstatwitViewController.m將記憶體釋放
- (void)dealloc {
    [tweetPicker release];
    [activities release];
    [feel...
[theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[[NSString
stringWithFormat:@"status=%@",themessage]
dataU...
Nächste SlideShare
Wird geladen in …5
×

iPhone App How-To:Send message To Twitter

2.247 Aufrufe

Veröffentlicht am

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

iPhone App How-To:Send message To Twitter

  1. 1. iPhone Development How-To 目標:寫一個可以選擇心情狀態的 app,然後按下按鈕後自動發送訊息至 twitter Step1. 打開 Xcode,選擇 View-Based Applicaiton,取名 為 InstaTwit Step2. 在 Resources 資料夾的 InstatwitViewControllers.xib 上面按兩下,在 View 中加入 Label, Picker View, Round Rect Button 並修改文字,畫 出以下界面:
  2. 2. Step3. 修改 InstatwitViewController.h #import <UIKit/UIKit.h> @interface InstatwitViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{ IBOutlet UIPickerView *tweetPicker; NSArray* activities; //存放可選擇的活動狀態 NSArray* feelings; //存放可選擇的感覺狀態 } @property(nonatomic,retain)UIPickerView *tweetPicker; -(IBAction)sendButtonTapped: (id)sender; @end step4.修改 InstatwitViewController.m 加入 DataSource 的方法,決定 Picker View 有兩列,一列放活動狀態,一列放感 覺狀態 //UIPickerViewDataSource Protocol's methods //1.numberOfComponentsInPickerView:pickerView //2.pickerView:numberOfRowsInComponent -(NSInteger)numberOfComponentsInPickerView:
  3. 3. (UIPickerView *)pickerView { return 2; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component { if (component==0) { return [activities count]; } else { return [feelings count]; } } step5.修改 InstatwitViewController.m 加入 Delegate 的 方法,當使用者轉到該狀態時,呈現對應值 //UIPickerViewDelegate Protocol's method //pickerView:titleForRow -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component { switch (component) { case 0: return [activities objectAtIndex:row]; case 1: return [feelings objectAtIndex:row]; } return nil; } step6. 修改 InstatwitViewController.m加入活動狀態與感 覺狀態的內容 - (void)viewDidLoad { [super viewDidLoad]; activities=[[NSArray alloc] initWithObjects:
  4. 4. @"sleeping",@"eating",@"working",@"thinking",@"crying" ,@"begging",@"leaving",@"shopping",@"hello worlding",nil]; feelings=[[NSArray alloc] initWithObjects: @"awesome",@"sad",@"happy",@"ambivalent",@"nause ous",@"psyched",@"confused",@"hopeful",@"anxious",n il]; }
  5. 5. step7. 修改 InstatwitViewController.m將記憶體釋放 - (void)dealloc { [tweetPicker release]; [activities release]; [feelings release]; [super dealloc]; } step8. 修改 InstatwitViewController.m @implementation InstatwitViewController @synthesize tweetPicker; step9. 修改 InstatwitViewController.m,讓按鈕按下時可 以送出訊息至twitter,注意其中的網址須輸入自己的Twitter帳號 與密碼 -(IBAction)sendButtonTapped: (id)sender { NSString* themessage=[NSString stringWithFormat:@"I'm %@ and feeling %@ about it.", [activities objectAtIndex: [tweetPicker selectedRowInComponent:0]], [feelings objectAtIndex: [tweetPicker selectedRowInComponent:1]]]; NSLog(themessage); //Twitter Black Magic NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://YourTwitterUsername:YourT witterPassword@twitter.com/statuses/update.xml"] cachePolicy:NSURLRequestUseProtocolCachePolic y timeoutInterval:60.0];
  6. 6. [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody:[[NSString stringWithFormat:@"status=%@",themessage] dataUsingEncoding:NSASCIIStringEncoding]]; NSURLResponse* response; NSError* error; NSData* result=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; NSLog(@"%@",[[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]); //End Twitter Black Magic } step10.再到 Interface Builder,在 Picker View 的圖上按 右鍵,將 datasource, delegate, tweetPicker 右方的圓圈 拖曳至 File’s Owner step11.在 Tweet it!按鈕上按右鍵,將 Touch Up Inside 拉至 File’s Owner,並選擇 sendButtonTapped,存檔
  7. 7. step12.Build & Run

×