简体   繁体   中英

Handling HTTPS certificate error with NSURLSession

I have the following code:

let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
        if(data != nil){
            //...
        } else if(error != nil) {
            //...
        }

        //...
    })

    task.resume()

I´m trying to implement HTTPS request to the server with unauthorised CA and handle the unauthorised CA error properly. The problem is that it´s certificate, non-server, client error, so the response is nil. The only thing I have is the Error object, which has only two useful methods: localisedDescription() and debugDescription() .

As I suppose none of them should be really used for parsing error in the production, so what should be the best practice to do that?

guard let httpResponse = response as? HTTPURLResponse else { let errorNetwork = error as! NSError ; Print("Status:(String(describing: errorNetwork.code))" completion(nil, .requestFailed, errorNetwork.code); // pass this to your layer above. return }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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