我正在使用 HTML 视图来显示表单。它可以工作,但唯一的问题是表单在提交时会尝试转到下一页,我不希望这种情况发生。如何防止此表单尝试转到下一页?
<div id="stylized" class="myform">
<form id="form" class="myform" method="post"
action="http://www.mydomain.com/my_page.php?platform=ios" >
<label>Your Name:</label>
<input name="name" id="name" />
<label>Your Email:</label> <input name="email" id="email" />
<button type="submit" value="Submit" />Submit</button>
<div class="spacer"></div>
</form>
</div>
这是它的控制器。我前段时间写了它,现在实际上甚至不完全理解远程调用的位置/方式以将此信息发送到服务器:
@interface PremiumController ()
@end
@implementation PremiumController
@synthesize theWebView;
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:@"http"])
{
stringByEvaluatingJavaScriptFromString:@"escape(document.getElementById(\"phone\").value);"];
[theWebView stringByEvaluatingJavaScriptFromString:@"alert(\"Information sent successfully. Simply ensure that what you had entered was correct.\");"];
}
return YES;
}
- (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.
theWebView.delegate = self;
}
- (void)viewDidUnload
{
[self setTheWebView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewDidAppear:(BOOL)animated
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"premium" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *rq = [NSURLRequest requestWithURL:url];
[theWebView loadRequest:rq];
// EMAIL
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *user_id = [standardUserDefaults objectForKey:@"user_id"];
NSString *user_email = [standardUserDefaults objectForKey:@"email"];
EmailUtil *email_obj = [[EmailUtil alloc] initWithSubject:@"iPremium Loaded" body:[NSString stringWithFormat: @"User id: %@ and email: %@" , user_id , user_email ]];
[email_obj send];
// END EMAIL
}
@end
谢谢!