UIWebView + custom javascript object

落爺英雄遲暮 提交于 2019-12-13 02:17:32

问题


I'm using a UIWebView with local jquery and a script that defines a function, like this:

function myFunc() {
    var myVar;

    function info() {
        alert('info');
    }
}

What I cannot accomplish is such a simple task:

var myfunc = new myFunc();
myfunc.info();

in the UIWebView. How can I instantiate a new object and call a function of that object by using:

[webViewObject stringByEvaluatingJavaScriptFromString:@"myfunc.info();"];

I'm struggling about this.


回答1:


Just solved. Is sufficient to implement the protocol in the viewcontroller you are using it, then declare:

webView.delegate = self;

and in:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidFinishLoad");
    [webView stringByEvaluatingJavaScriptFromString:@"var obj = new myObject();"];
}

later:

-(void)myAwesomeAction
{
    [webView stringByEvaluatingJavaScriptFromString:@"obj.func();"];
}


来源:https://stackoverflow.com/questions/4862059/uiwebview-custom-javascript-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!