我在控制器中有以下内容,并且不明白它为什么不在页面中调用该函数(它不执行任何操作)。任何帮助表示赞赏 - 第一次使用它:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = @"http://local.com/js-more.html"; // this does laod
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[arcWebView loadRequest:requestObj];
NSString * param = @"foo";
NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
[arcWebView stringByEvaluatingJavaScriptFromString:jsCallBack];
在网页上我有:
<script>
function myFunc(this_str){![enter image description here][1]
alert('here is val: ' + this_str);
}
myFunc('here is in string');
</script>
并在浏览器或嵌入式UIWebView中正确收到警告。
事先提前编辑1
答案 0 :(得分:1)
我将javascript放在webviewdidfinishload委托方法中。由于问题可能是您的网页尚未加载,即使您已调用加载请求。
设置webviews委托,将代码放在那里,它应该可以工作。
EG
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://local.com/js-more.html"; // this does laod
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[arcWebView setDelegate:self];
[arcWebView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString * param = @"foo";
NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}