CustomHttpHeadersURLProtocol is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "CustomHttpHeadersURLProtocol"
github "taka0125/CustomHttpHeadersURLProtocol"
- Swift 3.0
fileprivate func setupCustomHttpHeadersURLProtocol() {
let setupCustomHeaders: CustomHttpHeadersConfig.SetupCustomHeaders = { (request: NSMutableURLRequest) in
request.addValue("CustomHttpHeadersURLProtocolSample", forHTTPHeaderField: "X-App-Name")
request.addValue("\(Date().timeIntervalSince1970)", forHTTPHeaderField: "X-Timestamp")
}
let canHandleRequest: CustomHttpHeadersConfig.CanHandleRequest = { (request: URLRequest) -> Bool in
guard let scheme = request.url?.scheme else { return false }
guard let host = request.url?.host else { return false }
if !["http", "https"].contains(scheme) { return false }
if host == "0.0.0.0" { return true }
return false
}
let config = CustomHttpHeadersConfig(setupCustomHeaders: setupCustomHeaders, canHandleRequest: canHandleRequest)
CustomHttpHeadersURLProtocol.start(config)
}
- Swift 2.3
private func setupCustomHttpHeadersURLProtocol() {
let setupCustomHeaders: CustomHttpHeadersConfig.SetupCustomHeaders = { (request: NSMutableURLRequest) in
request.addValue("CustomHttpHeadersURLProtocolSample", forHTTPHeaderField: "X-App-Name")
request.addValue("\(NSDate().timeIntervalSince1970)", forHTTPHeaderField: "X-Timestamp")
}
let canHandleRequest: CustomHttpHeadersConfig.CanHandleRequest = { (request: NSURLRequest) -> Bool in
guard let scheme = request.URL?.scheme else { return false }
guard let host = request.URL?.host else { return false }
if !["http", "https"].contains(scheme) { return false }
if host == "0.0.0.0" { return true }
return false
}
let config = CustomHttpHeadersConfig(setupCustomHeaders: setupCustomHeaders, canHandleRequest: canHandleRequest)
CustomHttpHeadersURLProtocol.start(config)
}
Takahiro Ooishi, [email protected]
CustomHttpHeadersURLProtocol is available under the MIT license. See the LICENSE file for more info.