[英]Communication between iOS's native app and webpage's javascript
I have a webpage loaded in a UIWebView, and a javascript function of the page needs to data from native iOs app, a NSString. 我在UIWebView中加载了一个网页,该页面的javascript函数需要来自本机iOs应用程序的数据,即NSString。 How can a Js function access the data in native app?
Js函数如何访问本机应用程序中的数据?
Thanks, 谢谢,
lvreiny lvreiny
You can execute JavaScript in your UIWebView from Obj-C. 您可以在Obj-C的UIWebView中执行JavaScript。 Simply call
[webView stringByEvaluatingJavaScriptFromString:@"myJavaScript"];
只需调用
[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 Objective-C的
NSString *myParameter = @"myParameter";
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"callmeFromObjC('%@')", myParameter]];
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之间的通信应用程序之一,反之亦然。
https://github.com/marcuswestin/WebViewJavascriptBridge . https://github.com/marcuswestin/WebViewJavascriptBridge 。
I created an iOS/JS library to help make this easier -- that is, communication in both directions using similar methods. 我创建了一个iOS / JS库来帮助简化这一过程 - 也就是说,使用类似的方法在两个方向上进行通信。 You can check it out here: https://github.com/tcoulter/jockeyjs
你可以在这里查看: https : //github.com/tcoulter/jockeyjs
[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. 我还写了一篇关于如何在iOS中调用和处理不同javascript函数的指南。 http://www.dplusmpage.com/2012/07/20/execute-javascript-on-ios/
http://www.dplusmpage.com/2012/07/20/execute-javascript-on-ios/
Let the javascript load a custom URL, which your app intercepts. 让javascript加载您的应用拦截的自定义网址。 It than can parse it, prepare the data and pass it on to your webpage via
stringByEvaluatingJavaScriptFromString:
. 它可以解析它,准备数据并通过
stringByEvaluatingJavaScriptFromString:
将其传递到您的网页stringByEvaluatingJavaScriptFromString:
。
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.