无法在 Watchkit 上将 URLSession 与 Swift 一起使用
Can't use URLSession with Swift on Watchkit
我在 Apple Watch 上尝试使用 swift 获取一些数据时遇到问题。当我在 Swift Playground 中尝试此代码时,它运行良好,但当我将它放入 WatchOS 应用程序时,它向我显示此错误:
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=(
这是我在 WatchOS 应用程序中使用的代码
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()
}
}
在此先感谢您的帮助
我通过向我的节点服务器添加中间证书找到了解决方案。
我在 Apple Watch 上尝试使用 swift 获取一些数据时遇到问题。当我在 Swift Playground 中尝试此代码时,它运行良好,但当我将它放入 WatchOS 应用程序时,它向我显示此错误:
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=(
这是我在 WatchOS 应用程序中使用的代码
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()
}
}
在此先感谢您的帮助
我通过向我的节点服务器添加中间证书找到了解决方案。