如何从 Swift 3 中的 UIWebview 中的链接打开另一个视图?
How to open another View from links within UIWebview in Swift 3?
当我实现代码时,它在浏览器上完美运行,在 ios 中打开 safari 并将 myapp://
放在地址栏上,它打开了我的应用程序,但我想要点击 link 在 UIWebView
里面
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let setURL=URL(string:"https://myapp.com/")
let setURLRequest=URLRequest(url:setURL!)
webView.loadRequest(setURLRequest)
}
这是我将网站加载到 webview 的代码,并添加了 URL TYPE >item0->myapp
item0->URL Scheme->myapp
当我添加到 link 例如 < a href="myapp://"></a>
时,它不会启动另一个出现错误的视图
Appdelegate
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// UIApplication.shared.open(request.url!, options: [:], //completionHandler: nil)
let myVC: MyAnotherController = MyAnotherController()
self.present(myVC, animated: true) { }
return false
}
return true
}
收到此错误 'AppDelegate' 类型的值没有成员 'present'"
您可以使用 UIWebView
委托方法 optional public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
示例
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// do your work here
UIApplication.shared.open(request.url, options: [:], completionHandler: nil)
}
return true
}
但是你也必须在你的应用程序列表中提及所有的愿望方案..
使用 url 方案打开其他应用程序
if let url = URL.init(string: "myapp://") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
print("scheme is missing in info.plist or invalid scheme")
}
} else {
print("invalid url")
}
从 if 块 内部,您需要 return false,它应该可以工作。
通过 returning false 委托方法告诉 webView 不应该开始加载 url.
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// do your work here
UIApplication.shared.open(request.url!, options: [:], completionHandler: nil)
//Add the below line of code to the existing code
return false
}
return true
}
使用此代码(如果 URL 的最后一个字符包含“#”,它将起作用)。添加“#”是因为如果我们导航回上一个屏幕,它不会打开。
link(www.google.com) 在网络视图中。
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool {
// request.url?.absoluteString = "www.google.com/#"
let last = request.url?.absoluteString.last
if last == "#" {
//do stuff here
}
return true
}
当我实现代码时,它在浏览器上完美运行,在 ios 中打开 safari 并将 myapp://
放在地址栏上,它打开了我的应用程序,但我想要点击 link 在 UIWebView
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let setURL=URL(string:"https://myapp.com/")
let setURLRequest=URLRequest(url:setURL!)
webView.loadRequest(setURLRequest)
}
这是我将网站加载到 webview 的代码,并添加了 URL TYPE >item0->myapp
item0->URL Scheme->myapp
当我添加到 link 例如 < a href="myapp://"></a>
时,它不会启动另一个出现错误的视图
Appdelegate
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// UIApplication.shared.open(request.url!, options: [:], //completionHandler: nil)
let myVC: MyAnotherController = MyAnotherController()
self.present(myVC, animated: true) { }
return false
}
return true
}
收到此错误 'AppDelegate' 类型的值没有成员 'present'"
您可以使用 UIWebView
委托方法 optional public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
示例
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// do your work here
UIApplication.shared.open(request.url, options: [:], completionHandler: nil)
}
return true
}
但是你也必须在你的应用程序列表中提及所有的愿望方案..
使用 url 方案打开其他应用程序
if let url = URL.init(string: "myapp://") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
print("scheme is missing in info.plist or invalid scheme")
}
} else {
print("invalid url")
}
从 if 块 内部,您需要 return false,它应该可以工作。 通过 returning false 委托方法告诉 webView 不应该开始加载 url.
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.scheme == "myapp" {
// do your work here
UIApplication.shared.open(request.url!, options: [:], completionHandler: nil)
//Add the below line of code to the existing code
return false
}
return true
}
使用此代码(如果 URL 的最后一个字符包含“#”,它将起作用)。添加“#”是因为如果我们导航回上一个屏幕,它不会打开。
link(www.google.com) 在网络视图中。
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool {
// request.url?.absoluteString = "www.google.com/#"
let last = request.url?.absoluteString.last
if last == "#" {
//do stuff here
}
return true
}