在所有UIWebView请求上添加自定义http头字段

时间:2015-01-13 20:43:19

标签: ios swift uiwebview

我正在使用以下代码为从我的UIWebView发送的请求设置自定义HTTP标头。问题是我看到页面加载一秒钟然后它进入白色/空白屏幕。我已经测试了不同的URL,但行为是相同的。有什么想法吗?

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        var headerIsPresent = false
        let headerFields = request.allHTTPHeaderFields
        for headerField in headerFields?.keys.array as [String] {
            if headerField == "X-Test-App" {
                headerIsPresent = true
            }
        }

        if headerIsPresent {
            return true
        } else {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                dispatch_async(dispatch_get_main_queue(), {
                let url = request.URL
                var newRequest: NSMutableURLRequest = request as NSMutableURLRequest

                // set new header
                newRequest.addValue("MyValue", forHTTPHeaderField: "X-Test-App")

                // reload the request
                self.webView.loadRequest(newRequest)
                })
            })
            return false
        }
    }

3 个答案:

答案 0 :(得分:7)

根据David的评论,我使用了以下解决方案:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let headerFields = request.allHTTPHeaderFields
        var headerIsPresent = contains(request.allHTTPHeaderFields?.keys.array as [String], "X-Test-App")

        if headerIsPresent || navigationType == UIWebViewNavigationType.Other {
            return true
        } else {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                dispatch_async(dispatch_get_main_queue(), {
                    let url = request.URL
                    var newRequest: NSMutableURLRequest = request as NSMutableURLRequest
                    // set new header
                    newRequest.addValue("MyValue", forHTTPHeaderField: "X-Test-App")

                    // reload the request
                    self.webView.loadRequest(newRequest)
                })
            })
            return false
        }
    }

答案 1 :(得分:1)

我认为行var headerIsPresent...存在语法错误可能是由于Swift中更新的字符串字符API?

我使用map代替array,然后在结果上调用contains()

let headerIsPresent = request.allHTTPHeaderFields?.keys.map({$0}).contains("YOUR_HEADER_NAME")            

答案 2 :(得分:1)

如果您正在寻找swift中的代码:

    _ = request.allHTTPHeaderFields
    let headerIsPresent = request.allHTTPHeaderFields?.keys.contains("doerlist")

    if headerIsPresent! || navigationType == UIWebViewNavigationType.other {
        return true
    } else {

        DispatchQueue.global(qos: .background).async {
            DispatchQueue.main.async {
                let newRequest: NSMutableURLRequest = request as! NSMutableURLRequest
                newRequest.addValue("doerlist-a61444ea-6fef-4366-a39e-e029e6d521ef", forHTTPHeaderField: "doerlist")
                self.webView.loadRequest(newRequest as URLRequest)
            }
        }

        return false
    }