Use the button on the HTML page to invoke xcode IBAction

That seems weird. I'm not even sure that this is possible!

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

I want to click a button and then call IBAction in Xcode.

How can i do this? Or can you do it?

Thanks guys,

Stephen.

+3
source share
3 answers

You can do this using your own protocol. In your html file you can refer to something like myProtocol://callSomeAction.

Then on your UIWebViewDelegate(possibly yours UIViewController) you should implement a method called:

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

(Docs )

, request. myProtocol, IBAction NO. - , - YES.

:

- (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;
    }
}
+8
  • HTML URL-, myapp://buttonclick.

  • - webView:shouldStartLoadWithRequest:navigationType:. , URL-, , Obj-C, .

+3

, imageclick, href 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;
    }
}
0

Source: https://habr.com/ru/post/1773608/


All Articles