javascript iOS原生应用与网页javascript之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8307620/
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
Communication between iOS's native app and webpage's javascript
提问by HymanOdE
I have a webpage loaded in a UIWebView, and a javascript function of the page needs to data from native iOs app, a NSString. How can a Js function access the data in native app?
我在 UIWebView 中加载了一个网页,并且该页面的 javascript 函数需要来自本机 iOs 应用程序的数据,即 NSString。Js 函数如何访问原生应用程序中的数据?
Thanks,
谢谢,
lvreiny
吕雷尼
回答by Bj?rn Kaiser
You can execute JavaScript in your UIWebView from Obj-C. Simply call [webView stringByEvaluatingJavaScriptFromString:@"myJavaScript"];
.
您可以在 Obj-C 的 UIWebView 中执行 JavaScript。只需调用[webView stringByEvaluatingJavaScriptFromString:@"myJavaScript"];
.
I could imagine a setup like this:
我可以想象这样的设置:
Webpage
网页
<html>
<head>
<script type="text/javascript">
function callmeFromObjC(para1) {
// do something
alert(para1);
}
</script>
</head>
<body>
</body>
</html>
Objective-C
目标-C
NSString *myParameter = @"myParameter";
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"callmeFromObjC('%@')", myParameter]];
回答by Murali
With WebViewJavaScriptBridge you can achieve two way communication between javaScript and iOS.
使用 WebViewJavaScriptBridge,您可以实现 javaScript 和 iOS 之间的两种通信方式。
Check this link below for WebViewJavaScriptBridge .
检查下面的链接以获取 WebViewJavaScriptBridge 。
I used this bridge for one of my application for communication between iOS and JS and also vice versa.
我将此桥用于我的一个应用程序,用于在 iOS 和 JS 之间进行通信,反之亦然。
回答by Tim Coulter
I created an iOS/JS library to help make this easier -- that is, communication in both directions using similar methods. You can check it out here: https://github.com/tcoulter/jockeyjs
我创建了一个 iOS/JS 库来帮助简化这件事——也就是说,使用类似的方法进行双向通信。你可以在这里查看:https: //github.com/tcoulter/jockeyjs
回答by Johan Kool
Let the javascript load a custom URL, which your app intercepts. It than can parse it, prepare the data and pass it on to your webpage via stringByEvaluatingJavaScriptFromString:
.
让 javascript 加载一个自定义 URL,您的应用程序会拦截该 URL。它可以解析它,准备数据并通过stringByEvaluatingJavaScriptFromString:
.
回答by dplusm
[webView loadHTMLString:@"<script src=\"filename.js\"></script>"
baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"function(parameter)"];
Give feedback to iOS
向 iOS 反馈
window.location = customprefix://function/parameter=value
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[URL scheme] isEqualToString:@"customprefix"]) {
// handle function name and paramters
}
}
I also wrote a guide on how to call and handle different javascript functions from within iOS. http://www.dplusmpage.com/2012/07/20/execute-javascript-on-ios/
我还写了一篇关于如何在 iOS 中调用和处理不同 javascript 函数的指南。 http://www.dplusmpage.com/2012/07/20/execute-javascript-on-ios/
回答by Pravin
Sample code for this is available here,you can check it....very usefull
示例代码可在此处获得,您可以查看它....非常有用
http://ramkulkarni.com/blog/framework-for-interacting-with-embedded-webview-in-ios-application/
http://ramkulkarni.com/blog/framework-for-interacting-with-embedded-webview-in-ios-application/