How to load a url link that is inside a web view and keep it in that web view in SWIFT(如何加载 Web 视图中的 url 链接并将其保留在 SWIFT 中的该 Web 视图中)
问题描述
我有一个网页视图,它的网页底部带有链接.我有办法处理它们被点击,但它所做的只是将它们打开到一个新的 safari 浏览器中.这不是我想要的.我希望他们留在 Web 视图中并允许我像那样轻松地导航它们.有没有办法做到这一点?
I have a web view that has a webpage with links at the bottom. I have a way to deal with them being clicked, but all it does is open them into a new safari browser. This is not what I want.I would like for them to stay inside the web view and allow me to navigate them easily like that. Is there a way to do this?
我尝试过只使用常规的 Web 视图,但底部的链接不起作用.
I have tried just using a regular web view, but then the links at the bottom will not work.
这是我目前所拥有的:
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var myWebview: UIWebView!
@IBOutlet weak var returnButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
prettyReturnButton(returnButton)
let url = NSURL(string: "mywebpage")
myWebview.delegate = self
print("Web page:" , url)
let requestObj = NSURLRequest(URL: url!);
myWebview.loadRequest(requestObj);
myWebview.keyboardDisplayRequiresUserAction = true
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .LinkClicked:
// Open links in Safari
//let newRequestObj = NSURLRequest(URL: request.URL!)
// webView.loadRequest(newRequestObj)
UIApplication.sharedApplication().openURL(request.URL!)
return false
default:
// Handle other navigation types...
return true
}
}
推荐答案
在 webview 委托中,您将希望使用相同的 webview 导航到新的 url.
In the webview delegate you are going to want to navigate to the new url using that same webview.
对您而言,这意味着您应该在您的开关盒内使用loadRequest
For you this means that inside your switch case you should use
loadRequest
loadRequest 文档
要解决您的问题,您应该使用 UIApplication.sharedApplication().openURL(request.URL!)
To fix your issue you should remove the line using UIApplication.sharedApplication().openURL(request.URL!)
并将其替换为对 loadRequest
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .LinkClicked:
// Open links in Safari
guard let newRequest = request as? URLRequest else { return false }
webView.loadRequest(newRequest)
return false
default:
// Handle other navigation types...
return true
}
}
希望这会有所帮助!祝你有美好的一天!
Hope this helps! Have a nice day!
这篇关于如何加载 Web 视图中的 url 链接并将其保留在 SWIFT 中的该 Web 视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何加载 Web 视图中的 url 链接并将其保留在 SWIFT 中的该 Web 视图中
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01