Skip to content

taka0125/CustomHttpHeadersURLProtocol

Repository files navigation

CustomHttpHeadersURLProtocol

Version License Platform Carthage compatible

CocoaPods

CustomHttpHeadersURLProtocol is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "CustomHttpHeadersURLProtocol"

Carthage

github "taka0125/CustomHttpHeadersURLProtocol"

Sample

  • 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)
}

Author

Takahiro Ooishi, [email protected]

License

CustomHttpHeadersURLProtocol is available under the MIT license. See the LICENSE file for more info.

About

Add HTTP Header to HTTP Request.

Resources

License

Stars

Watchers

Forks

Packages

No packages published