我有一个非常基本的内部 iOS 应用程序,我想将 POST 发送到网络服务器。你基本上有一堆按钮,根据你按下的按钮,它会向网络服务器发送一个预定义的“消息”。
按下按钮后,它会将它们带到另一个视图并表示感谢您选择您按下的按钮。我还希望将预定义的消息发送到网络服务器,格式化,然后通过电子邮件发送到静态电子邮件地址。
这就是我到目前为止所拥有的......我只是想找出发送 POST 的最佳方式。我设置的方式是最好的方式吗?还是有更聪明的方法来做到这一点?
对不起,如果它是混乱和无组织的。我对 iOS 开发很陌生。
@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