简体   繁体   English

当JavaScript发出“ window.close”时,关闭UIWebView

[英]Close UIWebView, when javascript issues “window.close”

I have a UIWebView which renders a javascript. 我有一个呈现Javascript的UIWebView。 I want to kill this UIWebView whenever the javascript rendered in the UIWebView is closed. 每当关闭UIWebView中呈现的javascript时,我都想杀死此UIWebView。

In essence, I want to have the UIWebView only for loading the javascript! 本质上,我只想让UIWebView用于加载javascript!

In order to execute javascript you do not need to add uiwebview on current views. 为了执行javascript,您不需要在当前视图上添加uiwebview。 You can execute wathever you want without displaying your uiwebview onto the screen. 您可以执行任何所需的操作,而无需在屏幕上显示uiwebview。

To notify your uiwebview to close itself use javascript. 要通知uiwebview关闭自身,请使用javascript。 First you have to set your class as delegate of your uiwebview: 首先,您必须将类设置为uiwebview的委托:

NSURL *url = [NSURL URLWithString:@"https://myWebWithJavascript.html"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
//if your are not to display webview, frame dimensions does not mind
UIWebView uiwebview = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320, 480)];
[uiwebview setDelegate:self];  //remember that your .h has to implement <UIWebViewDelegate> 
[uiwebview loadRequest:request];

//then you implement notifications:
//this is executed when uiwebview has been loaded
- (void)webViewDidFinishLoad:(UIWebView *)webView
{ 
   //put here code if you wanna do something with uiwebview once has finished loading 
}
//this one is executed if your request returns any error on loading page
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//put here code if you wanna manage html errors
}
//this one it the ONE you will use to receive messages from javascript code:
//this function is executed every time an http request is made
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)req navigationType:(UIWebViewNavigationType)navigationType { 
        //we check every time there is an http request if the request contains
        //an special prefix that indicates it is not a real http request, but 
        //a comunication from javascript code 
        if ([[[req URL] absoluteString] hasPrefix:@"my-special-frame"]) {
             //so thats it- javascript code is indicating me to do something
             //for example: closing uiwebview:
             [webview release];  //probably it would be cleverer not to kill this way your uiwebview...but it is just an example
             return NO; //that is important because avoid uiwebview to load this fake http request 
        }   
 return YES; //that means that it will load http request that skips the if clause   
}

Then in your javascript you just need to make an http request with that special prefix we are expecting on objective-c code: 然后,在您的JavaScript中,您只需要使用我们希望在Objective-C代码上使用的特殊前缀发出http请求:

   var iframe = document.createElement("IFRAME");
   iframe.setAttribute("src", "my-special-frame:uzObjectiveCFunction");
   document.documentElement.appendChild(iframe);

In this example I open a frame with a URL that contains our special prefix. 在此示例中,我打开一个包含URL的框架,该URL包含我们的特殊前缀。 You could also make a simple: 您也可以做一个简单的:

   document.location.href = my-special-frame:uzObjectiveCFunction;  

Hope this help your doubts! 希望这可以帮助您解决疑问! Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM