簡體   English   中英

錯誤域=NSURLErrorDomain 代碼=-1202 “此服務器的證書無效

[英]Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid

我試圖在我的家庭網絡中調用我的 api 但由於某種原因,我收到以下錯誤消息:

完成錯誤 [-1202] 錯誤域 = NSURLErrorDomain 代碼 = -1202“此服務器的證書無效。您可能正在連接到偽裝成“192.168.179.185”的服務器,這可能會使您的機密信息面臨風險。 "

我嘗試了一些解決方案,但它們都不適合我的代碼。

import SwiftUI
import EFQRCode

struct ShowQRCodeView: View {
    //@Binding var isLoggedIn : Bool
    @Binding var deviceId : String
    @Binding var apiKey : String
    @Binding var userId : String
    @Binding var employeeId : Int
    @State private var x = UUID().uuidString
    @State var users = [User]()

var body: some View {
    VStack(){
        Form{
            Section("QR-Code"){
                if let cgImage = EFQRCode.generate(for: deviceId) {
                    Image(uiImage: UIImage(cgImage: cgImage)).resizable().frame(width: 150, height: 150)
                }
                Button("Login"){
                    Task{
                        await doHTTPUserCall()
                    }
                }
            }
        }.frame(height: 180)
        
    }.onAppear {
        if (deviceId == "") {
            deviceId = x // Could change here
        }
        
        
    }
}



func doHTTPUserCall() async {
    
    var url = "https://192.168.179.185:8090/CC0001/BE/admin/api/v1/employee/deviceid/"
    url += String(deviceId)
    guard let reqUrl = URL(string: url) else {
        print("Invalid URL")
        return()
    }
    var req = URLRequest(url: reqUrl)
    req.httpMethod = "GET"
    
    
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    formatter.timeZone = TimeZone(abbreviation: "ETC")
    
    
    
    let task = URLSession.shared.dataTask(with: req) { data, response, error in
        if let data = data {
            do{
                let decoder = JSONDecoder()
                decoder.dateDecodingStrategy = .formatted(formatter)
                users = try decoder.decode(Array<User>.self, from: data)
                
            } catch{
                print(error)
            }
        } else if let error = error {
            print("HTTP Request Failed \(error)")
        }
        if let response = response as? HTTPURLResponse {
                print("Response HTTP Status code: \(response.statusCode)")
            }
    }
    task.resume()
    
}
 
}

我認為這與自簽名 ssl 證書有關。
將不勝感激任何幫助,謝謝

如果不先將設備設置為信任進行簽名的證書(即證書本身,因為它自己簽名),您就不能使用自簽名證書。 因此,您需要找到一種方法在 iOS 中打開該證書,以便將其添加到設備的受信任證書存儲中。 您可以自己使用 email 然后點擊附件,或將其放入 iCloud Drive 並從手機上的 Drive 打開。 然后進入設置並在那里搜索受信任的證書和 go 以將此證書標記為受信任。

您可能會發現為您的服務器獲取真正的證書更容易,例如使用certbot.eff.org的免費證書

將此添加到您的 info.plist 中,您的問題將得到解決。

     <key>NSAppTransportSecurity</key>
  <dict> 
     <key>NSAllowsArbitraryLoads</key>
     <true/>
  </dict>

PS:出現此問題是因為托管 api 的服務器沒有 SSL 證書,此密鑰將繞過該證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM