URLSession request: request Code=-1009 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Denied over cellular interface)

My UI test is performed on iPhone 13 Pro 15.3.1, with Xcode 13.2.1. I have narrow down the issue as below two tests. The test code below just send a simple https request.

I have allowed the network access for the test app on both Wifi and cell. And I'm a paid developer.

All following two tests are passed in simulator but failed on iPhone, so it means something is wrong in iPhone side, but I couldn't figure out it. Need help, guys.

The error exists with WiFi and Cellular. URLSession request is denied on both Wi-Fi interface and cellular interface.

NSURLErrorNWPathKey=unsatisfied...

1st test:

The test code is provided in Apple doc (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)
}

2nd attempt:

I also try my version, but get the same result.

    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)
    }

I am getting a similar issue with iPhone 13 (Pro) devices on cellular data specifically. If I am able to figure anything out, I will mention it here, although I am unsure whether our issues are the exact same.

Do we have any workaround for this issue? Thanks!

Hi Haibo, did you resolve this issue? I am also getting this issue with physical devices but works in the simulator.

URLSession request: request Code=-1009 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Denied over cellular interface)
 
 
Q