Home > Blockchain >  Can't use URLSession with Swift on Watchkit
Can't use URLSession with Swift on Watchkit

Time:04-10

I have a problem when trying to fetch some data with swift on apple watch. When i try this code in Swift Playground it works well but when I put it in an WatchOS app it shows me this error :

Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “kholle.fr” which could put your confidential information at risk." UserInfo={NSErrorClientCertificateStateKey=0, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataPDTask <8B22FE2A-DFA9-4655-82A5-357F5A732CCD>.<1>, NSErrorFailingURLKey=https://kholle.fr/backend_saved, _NSURLErrorRelatedURLSessionTaskErrorKey=(

Here is the code I use in my WatchOS App

class ViewModel: ObservableObject {

@Published var kholles: [Kholle] = []

func fetch(group: String) {
    let url = URL(string: "https://kholle.fr/backend_saved")! //Here is my https server link

    let task = URLSession.shared.dataTask(with: url) { data, _,
        error in
        guard let data = data, error == nil else {
            return
        }
        
        //Convert to JSON
        
        do {
            let kholle = try JSONDecoder().decode([String: [Kholle]].self, from: data)
            DispatchQueue.main.async {
                print(kholle["B 16"])
            }
        } catch {
            print(error)
        }
    }

    task.resume()
}

}

Thanks in advance for your help

CodePudding user response:

I found out the solution by adding to my node server an Intermediate Certificate.

  • Related