How to call JavaScript Function in objective C

后端 未结 5 1694
借酒劲吻你
借酒劲吻你 2020-12-01 04:09

I am new programmer to objective C. I added some html file to Objective C. In this html file contains simple javascript function. I need to call that Function through thw ob

5条回答
  •  盖世英雄少女心
    2020-12-01 04:23

    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"first" ofType:@"html"];
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];
    [webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
    
     NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
     [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
    
    [webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
    

    If all this code called from single place, than it won't work, because page load is asynchronous and page with JavaScript code is not ready at that moment. Try to call this methods [webView stringByEvaluatingJavaScriptFromString:@"myFunction()"]; in UIWebViewDelegate callback method –webViewDidFinishLoad:

提交回复
热议问题