iOS Swift:无法检索在 UIwebView 中加载的当前 url [英] iOS Swift : can't Retrieve current url loaded in UIwebView
问题描述
我在 webview 应用程序 iOS swift 中工作:
i am working in webview application iOS swift :
我面临的问题是我想获取 webview 显示的每个网址:
problem i am facing is i want to get each url which webview displays :
在 url 上我必须执行一些 if else 检查.但我无法获得 webview 加载的网址.
upon urls i have to perform some if else checks . but i could not get the urls which webview loads.
webview 在点击 时显示良好的结果和加载 url.我想获取 webview 导航的所有网址..
webview is displaying fine results and loading urls upon clicking . i want to fetch all urls which webview navigates on..
import Foundation
import UIKit
class seconVC: UIViewController {
var toPass:String!
var touser:String!
var toPassing:String!
@IBOutlet weak var webV: UIWebView!
@IBOutlet weak var L_back: UIBarButtonItem!
@IBOutlet weak var R_frwrd: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var domurl = toPass;
var username = touser;
var passwing = toPassing;
var sendurl = stringA+username+"/"+passwing;
//exchange login with auth/username/password
println(sendurl);
let url = NSURL (string: sendurl);
let requestObj = NSURLRequest(URL: url!);
webV.userInteractionEnabled = true
webV.loadRequest(requestObj);
}
@IBAction func L_back(sender: UIBarButtonItem) {
if (webV.canGoBack){
webV.goBack()
}
}
@IBAction func R_frwrd(sender: UIBarButtonItem) {
if (webV.canGoForward){
webV.goForward()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我想要的只是获取 webview 的当前 url 字符串和导航按钮:请帮忙?我参考了所有互联网,没有解决方案适用于我的情况.
all i want is to get string of current url and navigation buttons for webview :please help ? i referred all the internet no solution is working in my case .
推荐答案
UIWebViewDelegate
有几个,例如 shouldStartLoadWithRequest
或 webViewDidStartLoad
或 webViewDidFinishLoad
或 didFailLoadWithError
帮助您实现目标.如果你想在视图完成后执行操作,那么实现这个委托方法
There are couple of UIWebViewDelegate
such as shouldStartLoadWithRequest
or webViewDidStartLoad
or webViewDidFinishLoad
or didFailLoadWithError
that help you to accomplish your goal. If you want to perform operation after view did finished then implement this delegate method
Swift 3
if let currentURL = webView.request?.url?.absoluteString{
print(currentURL)
}
正如它在此处所提到的.
这篇关于iOS Swift:无法检索在 UIwebView 中加载的当前 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!