开发者

UIWebView + custom javascript object

开发者 https://www.devze.com 2023-02-07 17:14 出处:网络
I\'m using a UIWebView w开发者_运维知识库ith local jquery and a script that defines a function, like this:

I'm using a UIWebView w开发者_运维知识库ith 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.


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();"];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消