Skip to content

Instantly share code, notes, and snippets.

@backslash-f
Created January 23, 2022 19:54
Show Gist options
  • Select an option

  • Save backslash-f/66c9bdd40e7dc56d7d376299df3dc7c9 to your computer and use it in GitHub Desktop.

Select an option

Save backslash-f/66c9bdd40e7dc56d7d376299df3dc7c9 to your computer and use it in GitHub Desktop.

Revisions

  1. backslash-f revised this gist Jan 23, 2022. No changes.
  2. backslash-f created this gist Jan 23, 2022.
    17 changes: 17 additions & 0 deletions Swift.playground
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // "Charles Enabler"
    // https://stackoverflow.com/a/57253011/584548
    public class NetworkEnabler: NSObject, URLSessionDelegate {
    public func urlSession(_ session: URLSession,
    didReceive challenge: URLAuthenticationChallenge,
    completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
    }
    }

    // Then, when creating a URLSession instance:
    let urlSession = URLSession(configuration: .default, delegate: NetworkEnabler(), delegateQueue: nil)

    // Create a request and finally...
    let (data, response) = try await urlSession.data(for: request)

    // Profit 💰