[英]How to use Charles Proxy on an Xcode Playground?
When tying out network requests from a Swift playground, I can't see the network call in Charles Proxy. 绑定来自Swift游乐场的网络请求时,在Charles Proxy中看不到网络呼叫。 However, it works when I do network requests from an iOS simulator by following the steps in the answer here .
但是,当我按照此处答案中的步骤执行来自iOS模拟器的网络请求时,它可以工作。
Would be nice to make it work for Xcode Playgrounds for faster iteration. 使它适用于Xcode Playgrounds以获得更快的迭代将是很好的。 Does anyone know what needs to be done to make it work there?
有谁知道需要做什么才能使其在那里工作?
I found a gist that describes a way to make it work with Playgrounds. 我发现了一个要点 ,该要点描述了使其与Playgrounds配合使用的方法。 It circumvents the need to alter the ATS settings.
它避免了更改ATS设置的需要。 Create an object conforming to
URLSessionDelegate
with the following implementation: 使用以下实现创建符合
URLSessionDelegate
的对象:
public class NetworkEnabler: NSObject, NSURLSessionDelegate {
public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
completionHandler(.UseCredential, NSURLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
and create a session with an instance as its delegate: URLSession(configuration: .default, delegate: enabler, delegateQueue: nil)
. 并创建一个以实例作为其委托的会话:
URLSession(configuration: .default, delegate: enabler, delegateQueue: nil)
。 Using that session for requests will allow you to use Charles. 使用该会话进行请求将使您可以使用Charles。
Swift 5 version of fruitcoder's answer: Swift 5版本的fruitcoder的答案:
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 to initialize the URLSession: 然后初始化URLSession:
let session = URLSession(configuration: .default, delegate: NetworkEnabler(), delegateQueue: nil)
Unlike the simulator, Playgrounds don't have their own network configuration. 与模拟器不同,Playgrounds没有自己的网络配置。 If you need a proxy on a Playground then you need to proxy your Mac's network connection.
如果您需要在Playground上使用代理服务器,则需要代理Mac的网络连接。 That will affect every app on your Mac, which is potentially a lot of data.
这将影响Mac上的每个应用程序,这可能是大量数据。 It should work but you might want to quit any other network-related apps that you can.
它应该可以工作,但是您可能想要退出任何其他与网络相关的应用程序。 If you don't need to use your browser while testing, for example, quit it until you're done.
例如,如果您在测试时不需要使用浏览器,请退出浏览器直至完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.