3

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.

StefanHanotin
  • 191
  • 5
  • 17

3 Answers3

8

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;
    }
}
pgb
  • 24,813
  • 12
  • 83
  • 113
  • Can you maybe show me how this is done. Because I can't quite figure it out. Thanks. – StefanHanotin Nov 08 '10 at 14:50
  • You my friend are a HERO! Thanks a million! – StefanHanotin Nov 08 '10 at 15:25
  • Thax for helping, It helps alot :) – Umair_uas Apr 26 '12 at 15:32
  • If anyone's has a problem getting this to working, check if you have set the `UIWebView`'s delegate to `self`. I spent quite a time beating around the bush because I forgot to do that. And @pgb Thanks for the solution. – Isuru Jun 26 '13 at 07:27
  • 1
    @Isuru you are right in that you need to set the delegate. However, setting it to self works in your context (where self is probably the view controller) but might fail for others. I would state it as: set the web view's delegate to the class that implemented the methods above. – pgb Jun 26 '13 at 12:03
3
  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.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Can you maybe show me where I am going wrong? I have tried the above but it doesn't seem to work. Here is my HTML.

    Here is my Objective-C. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (navigationType == UIWebViewNavigationTypeLinkClicked) { [self video1]; return NO; } return YES; }

    – StefanHanotin Nov 08 '10 at 14:25
0

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;
    }
}
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97