iphone - Close UIWebView, when javascript issues "window.close" -
i have uiwebview renders javascript. want kill uiwebview whenever javascript rendered in uiwebview closed.
in essence, want have uiwebview loading javascript!
in order execute javascript not need add uiwebview on current views. can execute wathever want without displaying uiwebview onto screen.
to notify uiwebview close use javascript. first have set class delegate of uiwebview:
nsurl *url = [nsurl urlwithstring:@"https://mywebwithjavascript.html"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; //if not display webview, frame dimensions not mind uiwebview uiwebview = [[uiwebview alloc] initwithframe:cgrectmake(0,0,320, 480)]; [uiwebview setdelegate:self]; //remember .h has implement <uiwebviewdelegate> [uiwebview loadrequest:request]; //then implement notifications: //this executed when uiwebview has been loaded - (void)webviewdidfinishload:(uiwebview *)webview { //put here code if wanna uiwebview once has finished loading } //this 1 executed if request returns error on loading page - (void)webview:(uiwebview *)webview didfailloadwitherror:(nserror *)error { //put here code if wanna manage html errors } //this 1 one use receive messages javascript code: //this function executed every time http request made - (bool)webview:(uiwebview*)webview shouldstartloadwithrequest: (nsurlrequest*)req navigationtype:(uiwebviewnavigationtype)navigationtype { //we check every time there http request if request contains //an special prefix indicates not real http request, //a comunication javascript code if ([[[req url] absolutestring] hasprefix:@"my-special-frame"]) { //so thats it- javascript code indicating me //for example: closing uiwebview: [webview release]; //probably cleverer not kill way uiwebview...but example return no; //that important because avoid uiwebview load fake http request } return yes; //that means load http request skips if clause }
then in javascript need make http request special prefix expecting on objective-c code:
var iframe = document.createelement("iframe"); iframe.setattribute("src", "my-special-frame:uzobjectivecfunction"); document.documentelement.appendchild(iframe);
in example open frame url contains our special prefix. make simple:
document.location.href = my-special-frame:uzobjectivecfunction;
hope doubts! luck!
Comments
Post a Comment