2012-11-18 59 views
0

我正在使用HTML視圖來顯示錶單。它有效,但唯一的問題是表單在提交時會嘗試進入下一頁,而我不希望發生這種情況。如何防止此表單試圖進入下一頁?ios - 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 

謝謝!

回答

0

如果您查找到UIWebViewDelegate Protocol第一個委託方法webView:shouldStartLoadWithRequest:navigationType:是您需要返回BOOL

返回值是,如果web視圖應該開始加載內容; 否則,否。

這意味着如果您返回YES,webView將繼續加載請求,如果否則webView將不會加載請求。希望這會有所幫助。