0

我正在使用 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

谢谢!

4

1 回答 1

0

如果您查找UIWebViewDelegate 协议,第一个委托方法webView:shouldStartLoadWithRequest:navigationType:是您需要返回一个BOOL.

如果 Web 视图应该开始加载内容,则返回值YES;否则,否。

这意味着如果您返回 YES,webView 将继续加载请求,如果返回 NO,则 webView 将不会加载请求。希望这会有所帮助。

于 2012-11-19T04:45:46.397 回答