我有一个当前正在使用Web视图的应用程序,当在Web视图中单击某些链接时,它将在Safari中打开这些链接。我现在想实现Safari View Controller(SVC),而不是将其启动到Safari应用程序。我已经进行了研究,并查看了有关SVC的示例;但是,我所看到的只是那些通过单击按钮即可打开SVC的工具。有人对我有建议或尝试的建议吗?
这是我的一些代码:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
let host = request.URL!.host!;
if (host != "www.example.com"){
return true
} else {
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
return true
}
func showLinksClicked() {
let safariVC = SFSafariViewController(URL: NSURL(string: "www.example.com")!)
self.presentViewController(safariVC, animated: true, completion: nil)
safariVC.delegate = self }
func safariViewControllerDidFinish(controller: SFSafariViewController) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
如果我理解正确,那么您正在Webview上加载一个页面,该页面现在具有某些链接,当用户单击链接时,您想在SVC中打开这些页面。您可以使用以下委托方法在webview中检测链接单击,然后从那里打开SVC。
编辑
基于已编辑的问题,我可以看到您没有调用showLinksClicked函数,您可以调用此函数,因为我在以下代码中进行了更新,它应该可以工作。
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
self.showLinksClicked()
return false
}
return true;
}
func showLinksClicked() {
let safariVC = SFSafariViewController(url: URL(string: "www.google.com")!)
present(safariVC, animated: true, completion: nil)
safariVC.delegate = self
}
func safariViewControllerDidFinish(controller: SFSafariViewController) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句