[英]Open link in browser inside iOS app with web techniques
Let's assume someone is running a web app directly inside a simple iOS app with UIWebView. 假设有人使用UIWebView在简单的iOS应用程序中直接运行Web应用程序。 It it possible to open a external website link inside the browser (Safari Mobile) with web techniques like JavaScript?
是否可以使用JavaScript之类的Web技术在浏览器(Safari Mobile)中打开外部网站链接? If yes, how could it be done?
如果是,该怎么办?
To open a URL programmatically: 要以编程方式打开URL:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"];
As far as your WebView, you can use your WebViewDelegate to catch and inspect any hyperlinks the user selects. 对于WebView,您可以使用WebViewDelegate捕获并检查用户选择的任何超链接。 For example:
例如:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *urlPath = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
if ([[urlPath scheme] hasPrefix:@"http"]))
{
[[UIApplication sharedApplication] openURL:urlPath];
return (NO);
}
}
return (YES);
}
This should open any hyperlink click in browser app. 这应该打开在浏览器应用程序中单击的任何超链接。
You can also create a custom protocol, see Android / iOS - Custom URI / Protocol Handling , and catch those in this same delegate handler by checking the [url scheme] property. 您还可以创建自定义协议,请参阅Android / iOS-自定义URI /协议处理 ,并通过检查[url scheme]属性来捕获同一委托处理程序中的协议 。 This will allow you to explicitly do something with javascript.
这将允许您显式地使用javascript做某事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.