繁体   English   中英

如何在Xcode Playground上使用Charles Proxy?

[英]How to use Charles Proxy on an Xcode Playground?

绑定来自Swift游乐场的网络请求时,在Charles Proxy中看不到网络呼叫。 但是,当我按照此处答案中的步骤执行来自iOS模拟器的网络请求时,它可以工作。

使它适用于Xcode Playgrounds以获得更快的迭代将是很好的。 有谁知道需要做什么才能使其在那里工作?

我发现了一个要点 ,该要点描述了使其与Playgrounds配合使用的方法。 它避免了更改ATS设置的需要。 使用以下实现创建符合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!))
    }
}

并创建一个以实例作为其委托的会话: URLSession(configuration: .default, delegate: enabler, delegateQueue: nil) 使用该会话进行请求将使您可以使用Charles。

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

然后初始化URLSession:

let session = URLSession(configuration: .default, delegate: NetworkEnabler(), delegateQueue: nil)

与模拟器不同,Playgrounds没有自己的网络配置。 如果您需要在Playground上使用代理服务器,则需要代理Mac的网络连接。 这将影响Mac上的每个应用程序,这可能是大量数据。 它应该可以工作,但是您可能想要退出任何其他与网络相关的应用程序。 例如,如果您在测试时不需要使用浏览器,请退出浏览器直至完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM