0
我想檢查在webView中的URL是否發生了變化。例如,如果我最初加載一個頁面,如Wordpress登錄頁面,並且我想知道它何時更改並重定向到登錄頁面。我試過使用這個資源enter link description here但答案似乎是不完整的,不起作用。檢查WebView中的URL是否已更改?
func validateUrl (stringURL : NSString) -> Bool {
var urlRegEx = "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+"
let predicate = NSPredicate(format:"SELF MATCHES %@", argumentArray:[urlRegEx])
var urlTest = NSPredicate.predicateWithSubstitutionVariables(predicate)
return predicate.evaluateWithObject(stringURL)
}
urlTest永遠不會被調用,所以我不確定它的目的。
if (validateUrl(stringURL: "http://google.com")) {
//will return true
print("Do Stuff");
}
else {
print("OTHER STUFf")
//If it is false then do stuff here.
}
然後調用這個函數
func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
if (validateUrl(request.URL().absoluteString())) {
//if will return true
print("Do Stuff");
}
}
我在我的代碼的末尾添加一個返回功能,但如不包括回報。我在Webview方面的經驗很少,所以任何建議或幫助將不勝感激。