response UIWebView click action to call up native code page

烈酒焚心 提交于 2019-12-10 17:12:52

问题


I'm looking for a way to listen for an action in a UIWebView. For example: When I tap a link or button inside a UIWebView, I want to call up a new native component, like a comment page. Or. take some other action like change a navigationBar item.


回答1:


Yes you can do this. Implement

– webView:shouldStartLoadWithRequest:navigationType:

This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button or hyperlink on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity or in your case bring up comments page or change nav bar etc.

Example - here you are trying to intercept any links clicked on your webpage & pass it through myMethodAction first.

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{ 
    if(navigationType == UIWebViewNavigationTypeLinkClicked)
    {
         if(overrideLinksSwitch.on == TRUE)
         {
             [self myMethodAction];
             [myWebView stopLoading];
             return YES;
         }
        else
        {
            return YES;
        }
    }
    return YES;
}

Hope this helps...



来源:https://stackoverflow.com/questions/8425604/response-uiwebview-click-action-to-call-up-native-code-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!