如何在UIWebView中检测“原始”URL请求

时间:2015-01-12 05:42:47

标签: ios swift uiwebview

我有一个问题,到目前为止我无法找到答案。基本上,我已经知道如何检查重定向网址:

func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
        println(webView.request?.URL.absoluteURL)
        if (navigationType == UIWebViewNavigationType.LinkClicked){
            if (webView.request?.URL.path == "/contact-us") {
                self.tabBarController?.selectedIndex = 4
            }
            return false
        } else {
            return true;
        }

    }

我现在遇到的问题是我实现了这段代码:

var path = NSBundle.mainBundle().bundlePath
    var baseUrl  = NSURL.fileURLWithPath("\(path)")
    webView.loadHTMLString(finalHtml, baseURL: baseUrl)

因此,我没有获得超链接中的URL。我相信这是因为我改变了baseURL。现在,我应该如何获得"原创"标签中的网址?

任何建议都将不胜感激! = d

编辑为了让自己更清楚,这是打印请求网址时的输出: Optional(file:///Users/<My folder name>/Library/Developer/CoreSimulator/Devices/681E8ACA-B0EB-48B0-9FA4-FC89D3EE2044/data/Containers/Bundle/Application/8EB249C9-6362-4A92-B83F-8CDB5C181AAE/<appname>.app/)

此外,如果baseUrlnil,我会得到&#34; about:blank&#34;。

1 个答案:

答案 0 :(得分:0)

我不是100%肯定你在问什么,但它似乎是一个简单的字符串替换。

NSString *path = webView.request?.URL.path;
NSString *cmp  = [path stringByReplacingOccurrencesOfString:webView.request.baseURL
                                 withString:@""];
if (cmp == "/contact-us") {

}

实际上,只是检查文档似乎你可能只想要最后一个路径组件

if (webView.request.URL.lastPathComponent == "contact-us") {

}
相关问题