I have a pretty basic internal iOS app that I want to send a POST to a web-server. You basically have a bunch of buttons, and depending on what button you press, it sends a pre-defined "message" to the web-server.
Once the buttons are pressed it takes them to another view and says Thanks for selecting the button that you pressed. I also want a pre-defined message to be sent to a web-server, formatted and then emailed to a static email address.
This is what I have so far.. I'm just trying to figure out the best way to send the POST. Is the way I have setup the best way to do it? Or is there smarter way to do this?
Sorry if it's messy and un-organized.. I'm very new to iOS development.
@interface DrinkViewController () {
NSString *buttonlabel;
NSString *urlEncodeUsingEncoding;
}
@end
@implementation DrinkViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(IBAction)selectButton:(id)sender{
NSLog(@"Button Pressed:%d",((UIButton *)sender).tag);
NSString *dvalue;
switch (((UIButton *)sender).tag) {
case 1:
dvalue = @"Button 1";
break;
case 2:
dvalue = @"Button 2";
break;
case 3:
dvalue = @"Button 3";
break;
case 4:
dvalue = @"Button 4";
break;
case 5:
dvalue = @"Button 5";
break;
case 6:
dvalue = @"Button 6";
break;
case 7:
dvalue = @"Button 7";
break;
}
buttonlabel = dvalue;
[self performSegueWithIdentifier:@"selectionTransition" sender:sender];
NSData *postData = [buttonlabel dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest alloc];
[request setURL:[NSURL URLWithString:@"http://www.abc.com/form.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSLog(@"POST: %@ sent to %@", buttonlabel, request);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"selectionTransition"]) {
NSLog(@"%@", buttonlabel);
// Get destination view
DetailViewController *vc = [segue destinationViewController];
// Pass the information to your destination view
[vc setLabelValue:buttonlabel];
}
}
@end