如何解决如何从Web浏览器控件中托管的HTML中打开Safari中的链接?
| 我们有一个iphone应用程序,该应用程序承载一个Web浏览器控件,该控件指向一个网站,其唯一目的是显示该应用程序的信息。我想知道是否可以使用HTML / JavaScript打开锚定/按钮的Safari。如果可能,怎么办? 谢谢...解决方法
您可以为使用的UIWebview设置一个委托。在此委托中,编写如下内容:
-(bool) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//You might need to set up an \"interceptLinks\"-Bool since you don\'t want to intercept the initial loading of the content
if (self.interceptLinks) {
NSURL *url = request.URL;
//This launches Safari with your URL
[[UIApplication sharedApplication] openURL:url];
return NO;
}
//No need to intercept the initial request to fill the WebView
else {
self.interceptLinks = TRUE;
return YES;
}
}
如果您只想截取某些链接,则可以解析该网址,并仅在必要时打开Safari。
,将您的UIWebView对象的委托属性(您称为“ Web浏览器控件”)设置为您自己的对象,该对象实现UIWebViewDelegate协议的方法“ webView:shouldStartLoadWithRequest:navigationType:\”:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intf/UIWebViewDelegate
然后,您可以检测是否请求了所需的URL,在这种情况下,可以使用UIApplication \的openURL方法在外部Safari实例上将其打开。
,您可以使用如下形式:
myWebView.ShouldStartLoad += LoadHook;
[...]
bool LoadHook (UIWebView sender,NSUrlRequest request,UIWebViewNavigationType navType){
var requestString = request.Url.AbsoluteString;
// determine here if this is a url you want to open in Safari or not
if (WantToOpenInSafari (requestString)){
UIApplication.SharedApplication.OpenUrl (new NSUrl (requestString));
return true;
} else {
return false;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。