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 Protocolを参照する場合、最初のデリゲート メソッドwebView:shouldStartLoadWithRequest:navigationType:BOOL.

戻り値Web ビューがコンテンツのロードを開始する必要がある場合は YES。そうでなければ、いいえ。

つまり、YES を返すと、webView はリクエストをロードし続けます。NO を返すと、webView はリクエストをロードしません。これが役立つことを願っています。

于 2012-11-19T04:45:46.397 に答える