URLSession 请求:请求代码=-1009“(null)”UserInfo={_NSURLErrorNWPathKey=unsatisfied(通过蜂窝接口拒绝)

Chu*_*ZHB 5 ios xctest swift urlsession

测试在 iPhone 13 Pro 15.3.1 上进行,使用 Xcode 13.2.1。我已将问题范围缩小为以下两个测试。下面的测试代码只是发送一个简单的https请求。

我已允许 Wifi 和手机上的测试应用程序进行网络访问。我是一名付费开发人员。

下面两个测试在模拟器上都通过了,在iPhone上却失败了,说明我的iPhone这边有问题,找到一个帖子说国产品牌iPhone可能有这个问题,还在挖……orz

WiFi 和蜂窝网络均存在此错误。Wi-Fi 接口和蜂窝接口上的 URLSession 请求均被拒绝。

NSURLErrorNWPathKey=unsatisfied... 在此输入图像描述

在此输入图像描述

第一次测试:

Apple 文档 ( https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations )中提供了测试代码。

func testDownloadWebData() {
    
    // Create an expectation for a background download task.
    let expectation = XCTestExpectation(description: "Download apple.com home page")
    
    // Create a URL for a web page to be downloaded.
    let url = URL(string: "https://apple.com")!
    
    // Create a background task to download the web page.
    let dataTask = URLSession.shared.dataTask(with: url) { (data, _, _) in
        
        // Make sure we downloaded some data.
        XCTAssertNotNil(data, "No data was downloaded.")
        
        // Fulfill the expectation to indicate that the background task has finished successfully.
        expectation.fulfill()
        
    }
    
    // Start the download task.
    dataTask.resume()
    
    // Wait until the expectation is fulfilled, with a timeout of 10 seconds.
    wait(for: [expectation], timeout: 10.0)
}
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

我也尝试了我的版本,但得到了相同的结果。

    func testDownloadWebData() {
        
        // Create an expectation for a background download task.
        let expectation = expectation(description: "Download apple.com home page")
        
        // Create a URL for a web page to be downloaded.
        let url = URL(string: "https://apple.com")!
        
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        
        let config = URLSessionConfiguration.default
        config.waitsForConnectivity = true
        
        let session = URLSession(configuration: config)
        
        // Create a background task to download the web page.
        let dataTask = session.dataTask(with: request) { (data, _, error) in
            // put a breakpoint here, no triggered.
            guard error == nil else {
                print(error!.localizedDescription)
                return
            }
            
            // Make sure we downloaded some data.
            XCTAssertNotNil(data, "No data was downloaded.")
            
            // Fulfill the expectation to indicate that the background task has finished successfully.
            expectation.fulfill()
            
        }
        
        // Start the download task.
        dataTask.resume()
        
        // Wait until the expectation is fulfilled, with a timeout of 10 seconds.
        wait(for: [expectation], timeout: 10.0)
    }
Run Code Online (Sandbox Code Playgroud)