在所有 UIWebView 请求上添加自定义 http header 字段
Adding custom http header field on all UIWebView requests
我正在使用以下代码对从我的 UIWebView 发送的请求设置自定义 HTTP header。问题是我看到页面加载了一秒钟,然后转到 white/blank 屏幕。我用不同的 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
}
}
根据 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
}
}
我认为 var headerIsPresent...
行存在语法错误,可能是由于更新了 Swift 中的字符串字符 API?
我使用 map
而不是 array
,然后对结果调用 contains()
。
let headerIsPresent = request.allHTTPHeaderFields?.keys.map({[=10=]}).contains("YOUR_HEADER_NAME")
如果您要查找 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
}
我正在使用以下代码对从我的 UIWebView 发送的请求设置自定义 HTTP header。问题是我看到页面加载了一秒钟,然后转到 white/blank 屏幕。我用不同的 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
}
}
根据 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
}
}
我认为 var headerIsPresent...
行存在语法错误,可能是由于更新了 Swift 中的字符串字符 API?
我使用 map
而不是 array
,然后对结果调用 contains()
。
let headerIsPresent = request.allHTTPHeaderFields?.keys.map({[=10=]}).contains("YOUR_HEADER_NAME")
如果您要查找 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
}