Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close UIWebView using javascript:window.close();

The question sounds weird, but here is my case.

I have a UIWebView in my UIView ( done by [self.view addSubview:webView]; ) which occupies full screen. It intentionally hides the UIView before the UIWebView clicks on an action javascript:window.close();.

How do I associate JavaScript in UIWebView page to UIView's action , e.g. [webView removeFromSuperView]; ?

like image 914
Raptor Avatar asked Jun 01 '12 02:06

Raptor


1 Answers

This is possible:

Set the location to something with your own scheme in the url, i.e.

document.location.href = "myscheme://DoSomething"

This will cause the UIWebViewDelegate method shouldStartLoadWithRequest: to be called with an NSURLRequest parameter whose scheme is"myscheme". So you look for this scheme and extract the DoSomething and then act on the DoSomething, which in your case would be to call remove from superView.

(In the UIWebView dealloc probably need to make sure you call stopLoading)

like image 91
Gruntcakes Avatar answered Sep 19 '22 06:09

Gruntcakes