Swift 从HTTPS到HTTP的快速切换

Swift 从HTTPS到HTTP的快速切换,swift,xcode,swiftui,Swift,Xcode,Swiftui,这个很奇怪 我有一个小的API服务器,它做一些我想让我的移动应用程序与之对话的事情。它有正确配置的DNS和SSL,我可以从我的浏览器或邮递员没有问题。此外,如果有人试图通过HTTP访问HTTPS,我还设置了301重定向到HTTPS 我在swift中有一个viewModel,它通过一个非常标准的设置调用这个API func getEndpointData() { var urlComponents = URLComponents() urlComponents.s

这个很奇怪

我有一个小的API服务器,它做一些我想让我的移动应用程序与之对话的事情。它有正确配置的DNS和SSL,我可以从我的浏览器或邮递员没有问题。此外,如果有人试图通过HTTP访问HTTPS,我还设置了301重定向到HTTPS

我在swift中有一个viewModel,它通过一个非常标准的设置调用这个API

 func getEndpointData() {
        var urlComponents = URLComponents()
        urlComponents.scheme = "https"
        urlComponents.host = "mycoolApi.com"
        urlComponents.path = "/v1/endpoint/\(model.endpoint.param)"
        let url = urlComponents.url!
        
        let session = URLSession(configuration: .default)
        session.dataTaskPublisher(for: url)
            .tryMap(){ element -> Data in
                guard let httpResponse = element.response as? HTTPURLResponse,
                      httpResponse.statusCode == 200 else {
                    throw URLError(.badServerResponse)
                }
                return element.data
            }
            .decode(type: EndpointData.self, decoder: JSONDecoder())
            .receive(on: RunLoop.main)
            .sink(receiveCompletion: { print("recieved completion \($0)")},
                  receiveValue: { endpointData in
                    self.rawData = endpointData.myCoolProperty
                  })
            .store(in: &cancellables)
    }

应该通过HTTPS进行呼叫,对吗? 错

这是我得到的错误:

recieved completion failure(Foundation.URLError(_nsError: Error Domain=NSURLErrorDomain 
Code=-1022 "The resource could not be loaded because the App Transport Security policy 
requires the use of a secure connection." UserInfo={NSLocalizedDescription=The resource could 
not be loaded because the App Transport Security policy requires the use of a secure 
connection., NSErrorFailingURLStringKey=http://mycoolApi.com/v1/endpoint/<Path_Param_ID>/, 
NSErrorFailingURLKey=http://mycoolApi.com/v1/endpoint/<Path_Param_ID>/, 
_NSURLErrorRelatedURLSessionTaskErrorKey=(
接收到完成失败(Foundation.URLError(\n错误:错误域=NSURLErrorDomain
代码=-1022“由于应用程序传输安全策略错误,无法加载资源
需要使用安全连接。“UserInfo={NSLocalizedDescription=资源可能会丢失
无法加载,因为应用程序传输安全策略要求使用安全的
连接,NSErrorFailingURLStringKey=http://mycoolApi.com/v1/endpoint//, 
N错误失效键=http://mycoolApi.com/v1/endpoint//, 
_NSURLErrorRelatedURLSessionTaskErrorKey=(
我觉得我好像在吃疯狂的药丸。为什么模拟器会强迫一些显式的https到http的东西