【发布时间】:2014-08-06 13:22:11
【问题描述】:
我有dashboard.html 页面并且DashboardController.js 与dashboard.html 相关联。我正在 UIWebview 中加载页面,现在我想从 uiwebview 的 DashboardController.js 调用 angularjs 函数。它不会被调用。
下面是我在 iOS 中尝试的代码
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSLog(@"URL:%@",url);
if (![Utility isInternetAvailable]) {
return FALSE;
}
NSString *jsCallBack = [NSString stringWithFormat:@"DashboardController.test()"];
NSString *response = [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
NSLog(@"%@",response);
return YES; // Return YES to make sure regular navigation works as expected.
}
在确保在 uiwebview 中加载了dashboard.html 后,我还尝试了不使用控制器引用。 NSString *jsCallBack = [NSString stringWithFormat:@"test()"];
虽然 test() 是我的 DashboardController.js 范围内的 js 函数。
function test (){
alert('sendCredentialsToNativeApp');
}
提前致谢。
【问题讨论】:
-
您确定需要 DashboardController。 ??这是我的应用程序中的一个函数 - (void) sendJsonDataString:(NSString *)jsonData{ // Création de la commance JS à envoyer. self.jsonActionToSend = [NSString stringWithFormat:@"sendData(%@);", jsonData]; if (contentLoaded){ NSLog(@"Contenu de la webView déjà chargé, le JS est execute."); [self.webView stringByEvaluatingJavaScriptFromString:self.jsonActionToSend]; self.jsonActionToSend = nil; } }
-
我调用的函数是远程 DashboardController.js 文件,该文件与我在 uiwebview 中加载的网页链接。所以我想从objective-c调用那个函数。
标签: javascript ios angularjs