加载html正确的方法来获得stringByEvaluatingJavaScriptFromString工作

时间:2014-09-11 08:41:04

标签: javascript html ios webview uiwebview

我试图让stringByEvaluatingJavaScriptFromString起作用。 我确定我加载了错误的html文件,我只是看不出我做错了什么。

-(void)viewDidLoad
{   
// Load the index.html file as a string from the file system
  NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
  NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// Load/pass it in to webView
  [webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
  [super viewDidLoad];
}

我将javascript事件传递给index.html:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"displayName('name');"];
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是我的功能代码。

NSString *fullURL = @"url";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

FinishLoad

-(void)webViewDidFinishLoad:(UIWebView *)webViews {
NSString *testString = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"[ID]\").innerHTML;"];
    [webView loadHTMLString:testString baseURL:nil];
}