Post

Replies

Boosts

Views

Activity

Reply to How to accept a self-signed SSL certificate with URLSession and Combine
Here is a solution. urlSession is called twice. The second time challenge.protectionSpace.serverTrust is nil. I'm still trying to fully understand this so any help is appreciated. This implementation is not secure and should only be used for testing. This code has been built and tested with Swift 5, iOS 14 running on the simulator in Xcode 12.2. import Foundation import Combine class myClass: NSObject, URLSessionDelegate { &#9;&#9;var ip: String &#9;&#9;var directorBearerToken: String &#9;&#9;var baseURI: String &#9;&#9;private var getRequestCancellable: AnyCancellable? &#9;&#9;private var getRequestPublisher = PassthroughSubject<Data, Never>() &#9;&#9; &#9;&#9;init(ip: String, token: String) { &#9;&#9;&#9;&#9;self.ip = ip &#9;&#9;&#9;&#9;self.directorBearerToken = token &#9;&#9;&#9;&#9;self.baseURI = "https://\(ip)" &#9;&#9;} &#9;&#9; &#9;&#9;// Get a JSON data with all the items &#9;&#9;func getAllItemInfo() -> PassthroughSubject<Data, Never> { &#9;&#9;&#9;&#9;let test = sendControllerGetRequest(uri: "/api/v1/items") &#9;&#9;&#9;&#9;return test &#9;&#9;} &#9;&#9;func sendControllerGetRequest(uri: String) -> PassthroughSubject<Data, Never>&#9;{ &#9;&#9;&#9;&#9;print("sendControllerGetRequest") &#9;&#9;&#9;&#9;getRequestPublisher = PassthroughSubject<Data, Never>() &#9;&#9;&#9;&#9;var urlRequest = URLRequest(url: URL(string: self.baseURI+uri)!) &#9;&#9;&#9;&#9;urlRequest.httpMethod = "GET" &#9;&#9;&#9;&#9;urlRequest.setValue("Bearer \(self.directorBearerToken)", forHTTPHeaderField: "Authorization") &#9;&#9;&#9;&#9;let configuration = URLSessionConfiguration.default &#9;&#9;&#9;&#9;let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main) &#9;&#9;&#9;&#9;getRequestCancellable = session.dataTaskPublisher(for: urlRequest) &#9;&#9;&#9;&#9;&#9;&#9;.map{ $0.data } &#9;&#9;&#9;&#9;&#9;&#9;.sink(receiveCompletion: { completion in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;switch completion { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case .failure(let error): &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("sendControllerGetRequest error") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(error) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case .finished: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("sendControllerGetRequest finished") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;break &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}, receiveValue: { requestDetails in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(String(data: requestDetails, encoding: .utf8)!) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("sendControllerGetRequest()") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.getRequestPublisher.send(requestDetails) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.getRequestPublisher.send(completion: .finished) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;) &#9;&#9;&#9;&#9;return getRequestPublisher &#9;&#9;} &#9;&#9;func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { &#9;&#9;&#9;&#9;if challenge.protectionSpace.serverTrust == nil { &#9;&#9;&#9;&#9;&#9;&#9;completionHandler(.useCredential, nil) &#9;&#9;&#9;&#9;} else { &#9;&#9;&#9;&#9;&#9;&#9;let trust: SecTrust = challenge.protectionSpace.serverTrust! &#9;&#9;&#9;&#9;&#9;&#9;let credential = URLCredential(trust: trust) &#9;&#9;&#9;&#9;&#9;&#9;completionHandler(.useCredential, credential) &#9;&#9;&#9;&#9;} &#9;&#9;} }
Nov ’20