使用 UIWebView 调用 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8886443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:55:36  来源:igfitidea点击:

Calling Javascript using UIWebView

javascriptuiwebviewios5rgraph

提问by learner2010

I am trying to call a javascript in a html page using the function -

我正在尝试使用该函数在 html 页面中调用 javascript -

View did load function
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"BasicGraph.html"];
    NSURL *urlStr = [NSURL fileURLWithPath:writablePath];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"BasicGraph" ofType:@"html"];
    [fileManager copyItemAtPath:myPathInfo toPath:writablePath error:NULL];

    [graphView loadRequest:[NSURLRequest requestWithURL:urlStr]];
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"];
}

Here is the javascript on the html page -

这是 html 页面上的 javascript -

<script>
    function methodName()
      {
         // code to draw graph
      }

However, the function methodName()is not getting called but after window.onload = function () everything is working fine..

但是,该函数methodName()没有被调用,但在 window.onload = function () 之后一切正常..

I am trying to integrate RGraphsinto my application and Basic.htmlis the html page in which the javascripts are written.

我正在尝试将RGraphs集成到我的应用程序中,并且Basic.html是编写 javascripts 的 html 页面。

It would be great if someone could help me out with this.

如果有人能帮我解决这个问题,那就太好了。

回答by Bj?rn Kaiser

Simple: You try to execute the JS function from Objective-C before the page even has been loaded.

很简单:您尝试在页面加载之前从 Objective-C 执行 JS 函数。

Implement the UIWebView's delegate methodwebViewDidFinishLoad:in your UIViewController and in there you call [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"];to make sure the function gets called afterthe page has been loaded.

在 UIViewController 中实现UIWebView 的委托方法webViewDidFinishLoad:,并在那里调用[graphView stringByEvaluatingJavaScriptFromString:@"methodName()"];以确保在页面加载调用该函数。

回答by Kalel Wade

To clarify a little bit more.

为了澄清一点。

.h - implement the UIWebViewDelegate

.h - 实现 UIWebViewDelegate

@interface YourViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

.m

.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = @"http://www.google.com";
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
    _webView.delegate = self; //Set the webviews delegate to this
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    //Execute javascript method or pure javascript if needed
    [_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];
}

You could also assign the delegate from storyboard instead of doing it in the code.

您还可以从故事板分配委托,而不是在代码中进行。