how to intercept Button click inside UIWebview on IOS?

后端 未结 3 2019
忘了有多久
忘了有多久 2020-11-30 20:07

I have a HTML form that has certain fields which i am opening inside a UIWebview. On click of a particular button i want to do a in app action.

The approach i\'m usi

相关标签:
3条回答
  • 2020-11-30 20:19

    This code:

    <a class="yourButton" href="inapp://capture">Restart</a>
    

    doesn't work for me.

    I've added "inapp://capture" in my javascript code for event function

    KeyboardInputManager.prototype.restart = function (event) {
      window.location.href = "inapp://capture";
      //other code
    };
    

    Also I use the UIWebViewDelegate and It works for iOS 8, 9.

    0 讨论(0)
  • 2020-11-30 20:25

    For swift 4.2

    Button code in HTML side:

    <a class="catch-iOS" href="inapp://home">Продолжить покупки</a>
    

    UIWebViewDelegate method:

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        if request.url?.scheme == "inapp" {
            if request.url?.host == "home" {
                // do something
            }
        }
        return true
    }
    
    0 讨论(0)
  • 2020-11-30 20:35

    You can do the following:

    In your HTML

    <a class="yourButton" href="inapp://capture">Button Text</a>
    

    In your UIWebViewDelegate

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
       if ([request.URL.scheme isEqualToString:@"inapp"]) {
          if ([request.URL.host isEqualToString:@"capture"]) {
             // do capture action
          }  
          return NO;
       }  
       return YES;
    }
    

    I added "capture" to your button's URL so that you could distinguish between several buttons that should trigger different actions (if you want to):

    0 讨论(0)
提交回复
热议问题