4

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 my iPhone side, find a post said China brand iPhone may have this issue, still digging...orz

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

NSURLErrorNWPathKey=unsatisfied... enter image description here

enter image description here

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)
    }
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32
  • I think you’ll need `expectations` to test async code – Abhinav Mathur Feb 13 '22 at 05:56
  • You should not be using semaphores for async methods. Use closures or use combine. I second what @AbhinavMathur said about expectations in your tests. – Rob C Feb 13 '22 at 06:05
  • Thanks for reply. I use `semaphores` to block the thread and return the result when it is available `semaphore.signal()`. I will use completion handler to have another try. Well, I dont see AbhinavMathur's comment. – Zhou Haibo Feb 13 '22 at 06:17
  • Now I see Abhinav Mathur's comment. – Zhou Haibo Feb 13 '22 at 06:36
  • @AbhinavMathur, please see my update. – Zhou Haibo Feb 13 '22 at 08:29
  • The settings to make a network call seem fine enough – Abhinav Mathur Feb 13 '22 at 09:32
  • @AbhinavMathur, yes, those scenario works OK in Simulator, but failed on my iPhone 13 Pro and iPhone SE, I'm still digging on what is missing. – Zhou Haibo Feb 13 '22 at 10:21
  • Okay, I have narrow down the issue and update PO title and desc to make it more clear and simple. – Zhou Haibo Feb 13 '22 at 12:37
  • Have you solved this? My QA is reporting a similar issue. Just strange. What we do know is that this happens when he's on data and is using 5G. – Glenn Posadas Mar 01 '22 at 15:13
  • Nop, since it is only happened on my iPhone 13 Pro but works fine in my colleague's iPhone 12. I put this as a low priority problem, I would go back to look on this weird problem when I get some time later. – Zhou Haibo Mar 03 '22 at 05:32
  • Now, it is failed on my colleague's iPhone 12 too. I file a bug on Apple dev forum 3 weeks ago, they didn't reply by now... – Zhou Haibo Mar 10 '22 at 06:20

0 Answers0