Use URLSession to access the HTTPS server. For testing purposes, and to save you the trouble of writing your own HTTPS server, you can use a web service called httpbin.org/ip that returns a JSON file in the format:

{
    origin = "221.237.156.243";
}Copy the code

The HTTPS code is as follows:

import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate,URLSessionDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { foo() return true } func foo(){ let task = URLSession.shared.dataTask(with: URL(string: "https://httpbin.org/ip")!) { (data, response, error) in if error ! = nil { print(error) } else { if let usableData = data { do { let json = try JSONSerialization.jsonObject(with: usableData, options:[]) print("json: \(json)") } catch { print("Error: \(error)") } } } } task.resume() } }Copy the code