Hello, I'm working on SwiftUI app where I'm trying to access ReST API. When I run may app in Simulator, I'm getting "The network connection was lost", but it's working without issues on physical device.
struct ContentView: View { @State var jednotky: Jednotky = Bundle.main.decode("jednotky.json") @State private var isOffline = false var body: some View { NavigationView{ VStack{ List(jednotky.rootLevel, id: \.id) { item in NavigationLink{ SoutezeView(rocnik: 2023, jednotka: item.childId, nazev: item.nazev) } label: { JednotkaRowView(nazev: item.nazev, childId: item.childId, parentId: item.parentId) } } if (isOffline) { Text("Using offline data") } } .navigationTitle("Fotbal") }.onAppear{ Task{ await loadData() } } } func loadData() async { let url = URL(string: JEDNOTKY_URL)! var request = URLRequest(url: url) guard let authData = (USERNAME + ":" + PASSWORD).data(using: .utf8)?.base64EncodedString() else { print("Can't create authorization header") return } request.addValue("Basic \(authData)", forHTTPHeaderField: "Authorization") do { let (data, _) = try await URLSession.shared.data(for: request) if let temp = try? JSONDecoder().decode(Jednotky.self, from: data) { jednotky = temp } } catch { print("Cannot download data, using stored sample instead") isOffline = true } } }
The error in console:
nw_socket_handle_socket_event [C1.1.1:3] Socket SO_ERROR 9 Connection 1: received failure notification Connection 1: failed to connect 1:9, reason 18 446 744 073 709 551 615 Connection 1: encountered error(1:9) Task <103A0196-6715-429B-A5F2-79698C6E9D45>.<1> HTTP load failed, 0/0 bytes (error code: 18 446 744 073 709 550 611 [1:9]) Cannot download data, using stored sample instead Task <103A0196-6715-429B-A5F2-79698C6E9D45>.<1> finished with error [18 446 744 073 709 550 611] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=9, NSUnderlyingError=0x600000c03d50 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en0[802.11], _kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <103A0196-6715-429B-A5F2-79698C6E9D45>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <103A0196-6715-429B-A5F2-79698C6E9D45>.<1>" ), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://is.fotbal.cz/api/jmobile2/orgJednotky.aspx, NSErrorFailingURLKey=https://is.fotbal.cz/api/jmobile2/orgJednotky.aspx, _kCFStreamErrorDomainKey=1}