簡體   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