1

I've never actually worked with a UIWebView before, so I'm not really sure where to begin. Effectively I'm working with a developer who's building this web platform for the app I'm currently working on. The user is supposed to fill in a form in the WebView. One of the fields is for a destination for a ticket they want to purchase. In the app, we already have a ViewController which is nicely laid out, allowing users to either enter a custom destination or choose from a predefined list. Would it be possible to display this ViewController when the text field in the WebView is clicked? The field then fills with the selected destination when the user has finished. Is there a better way of doing this (without putting load on the web Developer) and I'm just being stupid?

Like I said, I've never really worked with a UIWebView in this way before as I normally like to make my apps 100% native, but here is a go at how I imagine it may work:

-(void)someMethodThatsCalledWhenTextFieldIsPressed {
    AttachDestinationViewController *vc = [[AttachDestinationViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

And then in the AttachDestinationViewController we would do something like:

-(void)didFinish {
    CustomUIWebViewClass *parentViewController = (CustomUIWebViewClass *)self.parentViewController;
    parentViewController.propertyForTextField.text = self.locationEntered.text;
    [self dismissViewControllerAnimated:YES completion:nil];
}

That's how I would envision it if it were a normal ViewController, however, I have absolutely no idea how we would call -(void)someMethodThatsCalledWhenTextFieldIsPressed, nor how we would create propertyForTextField for the textField in the UIWebView.

Any help would be greatly appreciated,
Regards,
Mike

4

1 回答 1

2

Firstly you need to add a button to your UIWebView

Something like:

<a href="#" class="buttonStyle">Click me!</a>

Execute custom code by clicking on a link(button) in html

Then implement shouldStartLoadWithRequest.

You button links should look like this:

<a href="button://dosomething" class="buttonStyle">Click me!</a>

Now implement shouldStartLoadWithRequest like this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
    // only do something if a link has been clicked...
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {     

            // check if the url requests starts with our custom protocol:
        if ([[[request URL] absoluteString] hasPrefix:@"button://"]) 
        {
            [self CallYourMethodButton];
            return NO;
        } 
    }

    return YES;
}

This method will detect the href button you clicked according to the title and here you can call the method for your button.

EDIT :

Well Mike then its just like entering data to and from a form right ? Then you need to get the element name or the element id for the textfield. Please go through the following links :-

Finding and auto-filling HTML login forms in a UIWebView using JavaScript

ios - Injecting text into a UIWebView textField

于 2013-08-06T12:07:35.713 回答