Chu*_*ZHB 5 ios xctest swift urlsession
测试在 iPhone 13 Pro 15.3.1 上进行,使用 Xcode 13.2.1。我已将问题范围缩小为以下两个测试。下面的测试代码只是发送一个简单的https请求。
我已允许 Wifi 和手机上的测试应用程序进行网络访问。我是一名付费开发人员。
下面两个测试在模拟器上都通过了,在iPhone上却失败了,说明我的iPhone这边有问题,找到一个帖子说国产品牌iPhone可能有这个问题,还在挖……orz
NSURLErrorNWPathKey=unsatisfied...

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)
| 归档时间: |
|
| 查看次数: |
664 次 |
| 最近记录: |