kodecocodes / rxs-materials

The projects and the materials that accompany the RxSwift: Reactive Programming with Swift Book
Apache License 2.0
163 stars 87 forks source link

Need To Avoid SSL Problems #4

Open loinsir opened 2 years ago

loinsir commented 2 years ago

In Chapter 10, net.sci.gsfc.nasa.gov api has a ssl problem.

gordonbeijing commented 2 years ago

Facing the same issue :(

loinsir commented 2 years ago

To @gordonbeijing .

Finally, I solved this issue.

You should implement URLSessionDelegate in EONET.swift like this:

extension EONET: URLSessionDelegate {
  func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    //accept all certs when testing, perform default handling otherwise
    completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
  }
}

and you should create your URLSession request like this:

return URLSession(configuration: .ephemeral,
                        delegate: EONET(), delegateQueue: nil).rx.response(request: request)

I hope you can solve this problem smoothly. :)

gordonbeijing commented 2 years ago

To @loinsir .

Thanks a lot ! It works well.

At first it didn't work after I modifying the codes according to your solution. I checked the error message, it was not the same as before. So I doubt it may be caused by some mysterious strength , and after I enabling the proxy, it works as expected 🥳

Anyway, thanks again!