Question

This seems like a strange one. Not even sure if it is possible!!

I have a UIWebView that loads a local html page. On this html page I have a button.

I want to click on the button and then call an IBAction in Xcode.

How would I go about this? Or can you even do this???

Thanks guys,

Stefan.

Was it helpful?

Solution

You can do it by using a custom protocol. In you html file, you can link to something like myProtocol://callSomeAction.

Then on your UIWebViewDelegate (probably your UIViewController) you have to implement the method called:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

(Docs here)

The idea is that on that code, you detect the protocol based on the data in the request parameter. If it is myProtocol, you can call your IBAction and return NO. If it's something else, you fallback to have the web view load the page, and just return YES.

The code would look something like this:

- (BOOL)webView:(UIWebView *)webView
    shouldStartLoadWithRequest:(NSURLRequest *)request 
    navigationType:(UIWebViewNavigationType)navigationType {

    NSString* scheme = [[request URL] scheme];
    if ([@"myProtocol" isEqual:scheme]) {
        // Call your method
        return NO;
    } else {
        return YES;
    }
}

OTHER TIPS

  1. Have the button on the HTML page open a custom url such as myapp://buttonclick.

  2. In your web view delegate, implement webView:shouldStartLoadWithRequest:navigationType:. Check whether the request includes your custom URL and if it does, call any Obj-C method you want.

I have similar kind of situation, but its an imageclick , href and handling this method of webview,

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener{

    NSString *host = [[request URL] host];
    //if (host!=nil) 
    {




    WebNavigationType eActionType = (WebNavigationType)[[actionInformation valueForKey:WebActionNavigationTypeKey] intValue];
    NSURL *pOrignalURL;
    if(eActionType == WebNavigationTypeLinkClicked)// == [actionInformation valueForKey:WebActionNavigationTypeKey])
    {
        /* we will handle it */
        pOrignalURL = [actionInformation valueForKey:WebActionOriginalURLKey];
        NSString *pElementName = [actionInformation valueForKey:WebActionElementKey];

        if([[pOrignalURL absoluteString] hasPrefix:@"app:"]){

            [listener ignore];
            return;
        }
    }
    //[[NSWorkspace sharedWorkspace] openURL:pOrignalURL];
    NSArray* urls = [ NSArray arrayWithObject:
                     [ NSURL URLWithString:[pOrignalURL absoluteString]]];

    [[ NSWorkspace sharedWorkspace ]
     openURLs:urls
     withAppBundleIdentifier:nil
     /* use default system bindings */
     options:NSWorkspaceLaunchWithoutActivation
     additionalEventParamDescriptor:nil
     launchIdentifiers:nil ];


     /* default behavior */
[listener download];




    return;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top