Why I am telling these info to you is that now we are going to discuss an ios app which you can install in your iphone to tweet easily without visiting twitter site. The types of tweets that we are going to use is limited but the way we do that is interesting.
The following image shows how the design should be done.
what actually happens here is, we are going to have a Picker view with two columns( user can select two items from each column), that helps to form your tweet saying how are you today and how do you feel about that. So, all what the user has to do is to select them and click on Tweet it! button to post a tweet in his/her twitter page. Simple, but interesting.
once you are done with the design, don't forget to set the UIPickerView data source and delegate to the picker view we have here.
OK, I'll jump to the code now.
once you are done with the design, don't forget to set the UIPickerView data source and delegate to the picker view we have here.
OK, I'll jump to the code now.
// InstaTwitViewController.h
#import <UIKit/UIKit.h>
@interface InstaTwitViewController : UIViewController
//we are going to use the UIPickerView datasource and delegate methods
<UIPickerViewDelegate,UIPickerViewDataSource>
{
IBOutlet UIPickerView *tweetPicker; //two objects to hold the controls
IBOutlet UITextView *notesField;
NSArray *activities; //two arrays to hold the two parts of the tweet
NSArray *feelings;
}
@property(nonatomic,retain)UIPickerView *tweetPicker;
@property(nonatomic,retain)UITextView *notesField;
-(IBAction)sendButtonTapped:(id)sender; //method to sent the tweet
-(IBAction)textFieldDoneEditing:(id)sender;
@end
//---------------------------------------------------------------------------------------------------------
Now, we'll see how these can be implemented.
#import "InstaTwitViewController.h"
@implementation InstaTwitViewController
@synthesize tweetPicker,notesField;
- (void)dealloc
{
[activities release];
[feelings release];
[tweetPicker release];
[notesField release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
//this method will be invoked when the app loads for the first time
//we are going to make two NSArrys named activities and feelings and store a collection of related strings
- (void)viewDidLoad
{
activities=[[NSArray alloc]initWithObjects:@"sleeping",@"eating",@"working",@"thinking",@"crying",@"begging",@"leaving",@"shopping",@"hello worlding", nil];
feelings=[[NSArray alloc]initWithObjects:@"awsome",@"sad",@"happy",@"ambivalent",@"nauseous",@"psyched",@"confused",@"hopeful",@"anxious", nil];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
activities=nil;
feelings=nil;
tweetPicker=nil;
notesField=nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//-------------------UIPickerView datasouce methods-----------------------------------------------------
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// returns the # of rows in each component..
//-------------------UIPickerView delegate methods-----------------------------------------------------
//to set values for each cell of the UIPickerView
//we use a switch block here to see to which column we are addressing at the moment
//if it is component 0 then have to pass the rowth element of the activities array
//if component 1, do the same with the feelings array
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component)
{
case 0:
return [activities objectAtIndex:row];
break;
case 1:
return [feelings objectAtIndex:row];
break;
default:
break;
}
return nil;
}
//this method will be called if the user selects items from each of the columns of the picker view
-(IBAction)sendButtonTapped:(id)sender
{
//get each comlumn's selction to variables
NSString *selectedAcivity=[activities objectAtIndex:[tweetPicker selectedRowInComponent:0]];
NSString *selectedFeeling=[feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]];
//format the tweet
NSString *message=[[NSString alloc]initWithFormat:@"%@ I'm %@ and feeling %@ about it.",notesField.text ? notesField.text :@"",selectedAcivity,selectedFeeling];
//display the
NSLog(message);
//TWITTER API starts here to pass our message to twitter
//TWITTER BLACK MAGIC
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[[NSString stringWithFormat:@"status=%@",
message] 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
}
//this method will send the onscreen key board way when clicked on return button in it
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender becomeFirstResponder];
}
@end
Now, we'll see how these can be implemented.
#import "InstaTwitViewController.h"
@implementation InstaTwitViewController
@synthesize tweetPicker,notesField;
- (void)dealloc
{
[activities release];
[feelings release];
[tweetPicker release];
[notesField release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
//this method will be invoked when the app loads for the first time
//we are going to make two NSArrys named activities and feelings and store a collection of related strings
- (void)viewDidLoad
{
activities=[[NSArray alloc]initWithObjects:@"sleeping",@"eating",@"working",@"thinking",@"crying",@"begging",@"leaving",@"shopping",@"hello worlding", nil];
feelings=[[NSArray alloc]initWithObjects:@"awsome",@"sad",@"happy",@"ambivalent",@"nauseous",@"psyched",@"confused",@"hopeful",@"anxious", nil];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
activities=nil;
feelings=nil;
tweetPicker=nil;
notesField=nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//-------------------UIPickerView datasouce methods-----------------------------------------------------
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// returns the # of rows in each component..
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//we need the number of elements in the activities array to be the number of rows in the first column
//(here you call it component)
if(component==0)
return [activities count];
//we need the number of elements in the feelings array to be the number of rows in the first column
if(component==1)
return [feelings count];
}
//-------------------UIPickerView delegate methods-----------------------------------------------------
//to set values for each cell of the UIPickerView
//we use a switch block here to see to which column we are addressing at the moment
//if it is component 0 then have to pass the rowth element of the activities array
//if component 1, do the same with the feelings array
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component)
{
case 0:
return [activities objectAtIndex:row];
break;
case 1:
return [feelings objectAtIndex:row];
break;
default:
break;
}
return nil;
}
//this method will be called if the user selects items from each of the columns of the picker view
-(IBAction)sendButtonTapped:(id)sender
{
//get each comlumn's selction to variables
NSString *selectedAcivity=[activities objectAtIndex:[tweetPicker selectedRowInComponent:0]];
NSString *selectedFeeling=[feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]];
//format the tweet
NSString *message=[[NSString alloc]initWithFormat:@"%@ I'm %@ and feeling %@ about it.",notesField.text ? notesField.text :@"",selectedAcivity,selectedFeeling];
//display the
NSLog(message);
//TWITTER API starts here to pass our message to twitter
//TWITTER BLACK MAGIC
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:yourTwitterUserName.com:yourTwitterPassword@twitter.comstatuses/update.xml"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[theRequest setHTTPBody:[[NSString stringWithFormat:@"status=%@",
message] 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
}
//this method will send the onscreen key board way when clicked on return button in it
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender becomeFirstResponder];
}
@end
//----------------------------------------------------------------------------------------------------
Now you can build and run your app. I also have made available the project source code for you.
No comments:
Post a Comment