javascript - Why can't use JSConext.setObject to UIWebView when js called location.reload() ? -


i had write ios app swift when want use js call swift's object , functions in uiwebview, appear bugs. below codes:

    //a demo class want export uiwebview;     //this object      @objc protocol  navobj : jsexport{             //any code here             static func callme() -> string;     }     @objc class navobj : nsobject, navobj{             class func callme() -> string{                     return "called me";             }     }     class someview : uiviewcontroller , uiwebviewdelegate{              pubic func webview(                     webview : uiwebview,                     shouldstartloadwithrquest request : nsurlrequest,                     navigationtype : uiwebviewnavigationtype             ) -> bool {                  //get jscontext                 var jscontext = webview.valueforkeypath("documentview.webview.mainframe.javascriptcontext") as! jscontext ;                 //and set navobj uiwebview                 jscontext.setobject( navobj.self , "navobj" );             }      } 

in uiwebview code this:

       <script type='text/javascript'>               //get test               var ret = navobj.callme();               console.log(ret);               //when first time run code successed               //but when use js's location.reload refresh page , appear error:                //navobj not defined.                 location.reload();        </script> 

when first time load webpage successed. when use js's location.reload refresh page , appear error: navobj not defined.

it means swift code

    jscontext.setobject( navobj.self , "navobj" ); 

was not worked.

is there way fix error?

this timing issue. when navigate page or reload page jscontext deallocated , new 1 created. jscontext page created while page loading there isn't way know when happens.

i have found reliable approach use webviewdidfinishload method inject things jscontext , trigger dom event when finished. in web page need wait event before using native objects.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -