admin管理员组

文章数量:1294659

I recently started working on a IOS project that runs mainly on a UIWebView. Now my UIWebViewis a delegate of UIWebViewDelegate and what I have encountered is that my app does not respond to window.close and other window events that JavaScript might call. Now I am able to pick up a new HTTP request shot off through a href in my shouldStartLoadWithRequest delegate method.

My hope is to know if there is a way to listen for window events such as window.close in my IOS app and to retrieve the targets. I want to stray away from injecting any sort of JavaScript into the page directly as much as possible!

Anyone care to explain if or if not this is possible and why?

I recently started working on a IOS project that runs mainly on a UIWebView. Now my UIWebViewis a delegate of UIWebViewDelegate and what I have encountered is that my app does not respond to window.close and other window events that JavaScript might call. Now I am able to pick up a new HTTP request shot off through a href in my shouldStartLoadWithRequest delegate method.

My hope is to know if there is a way to listen for window events such as window.close in my IOS app and to retrieve the targets. I want to stray away from injecting any sort of JavaScript into the page directly as much as possible!

Anyone care to explain if or if not this is possible and why?

Share Improve this question asked Aug 5, 2015 at 21:11 David BigaDavid Biga 2,8018 gold badges40 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Using UIWebView there is no way of achieving this without injecting some JavaScript. Even the newer WKWebView would still need JavaScript injection to make this work.

You would inject JavaScript that would override the window.close function and trigger a location change with something like location.href = "uniquescheme://window.close"; which, as you say, could be handled in shouldStartLoadWithRequest.

Something like:

[self.webView stringByEvaluatingJavaScriptFromString:@"window.close = function () { location.href = 'uniquescheme://window.close'; }"]

For WKWebView there are methods which catch the calls for (implement WKUIDelegate)

window.open : createWebViewWithConfiguration: forNavigationAction: windowFeatures:

and for

window.close : webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0)

mind it, it's only available 9.0 and up.

but not in UIWebView(actually still investigating if it can be done without injection, will update the answer if I find any)

本文标签: iosHandling Windowclose in JavaScript through UIWebViewObj CStack Overflow