Skip to content

Instantly share code, notes, and snippets.

@shepting
Last active June 8, 2018 17:03
Show Gist options
  • Save shepting/829ff2f6940e71c0990c to your computer and use it in GitHub Desktop.
Save shepting/829ff2f6940e71c0990c to your computer and use it in GitHub Desktop.
Xcode Playgrounds will fail network requests when Charles is running since ATS will fail. With an Xcode project you can update the Info.plist but Playgrounds don't have any such option. This is that option.
// When using Charles, the network requests will always fail unless you can bypass ATS.
public class NetworkEnabler: NSObject, NSURLSessionDelegate {
public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
completionHandler(.UseCredential, NSURLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
// Then later when making a request:
let enabler = NetworkEnabler()
let session = NSURLSession(configuration: NSURLSession.sharedSession().configuration, delegate: enabler, delegateQueue: nil)
session.dataTaskWithRequest(request) {data,response,error in
// Code
}.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment