UIWebView可以与应用程序交互(沟通)吗? [英] Can a UIWebView interact (communicate) with the app?
问题描述
我去使用UIWebView来显示动态内容,而不是使用UI元素。是否有可能触发本机应用程序功能从简单地击中UIWebView内的链接?示例:点击链接,然后切换当前视图?
I went to use UIWebView to display dynamic content, instead of doing it natively using UI-elements. Is it possible to trigger native app functions from simply hitting links inside the UIWebView? Example: hitting a link which then switches current View?
推荐答案
是的,这是可能的。在你的html中,你写一个JS加载一个假冒方案的URL,如
Yes, it's possible. In your html, you write a JS to load a URL with a fake scheme such as
window.location = "request_for_action://anything/that/is/a/valid/url/can/go/here";
然后,在您的iOS代码中,为您的webView分配一个委托, / p>
Then, in your iOS code, assign a delegate to your webView, and in your delegate, handle
webView:shouldLoadWithRequest:navigationType
与
if( [request.URL.scheme isEqualToString: @"request_for_action"] )
{
// parse your custom URL to extract parameter, use URL parts or query string as you like
return NO; // return NO, so webView won't actually try to load this fake request
}
-
另一方面,让iOS代码通过
使用
Just an aside, you can do the other way, let iOS code invoke some JS codes in your html by using
NSString* returnValue = [self.webView stringByEvaluatingJavaScriptFromString: "someJSFunction()"];
这篇关于UIWebView可以与应用程序交互(沟通)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!