This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let config = URLSessionConfiguration.default | |
// Wait a total of 120 seconds before giving up starting from when there's an interval of no response data received from the server. | |
config.timeoutIntervalForRequest = 120 // Default is 60 | |
// Wait a total of 360 seconds before giving up starting from when we start a request. | |
config.timeoutIntervalForResource = 360 // Default is 7 days |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let task = URLSession.shared.dataTask(with: request) | |
// Let our server know, it should prioritise handling this above lower priority tasks. | |
task.priority = URLSessionTask.highPriority | |
task.resume() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func downloadHighResImage() { | |
var request = URLRequest(url: URL(string: "highQualityImageURL")!) | |
// Trigger error in `session.dataTask` if user has low data mode enabled. | |
request.allowsConstrainedNetworkAccess = false | |
URLSession.shared.dataTask(with: request) { data, response, error in | |
if let error = error as? URLError, | |
error.networkUnavailableReason == .constrained { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func expensiveLongRunningDataTask() { | |
let config = URLSessionConfiguration.default | |
// Be low data mode compliant | |
config.allowsConstrainedNetworkAccess = false | |
// Automatically retrigger the request when we are on Wi-Fi. | |
config.waitsForConnectivity = true | |
let request = URLRequest(url: myURL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lazy var backgroundSession: URLSession = { | |
let config = URLSessionConfiguration.background(withIdentifier: "LongRunningSession") | |
// System will trigger requests at the best time for optimal performance. | |
config.isDiscretionary = true | |
return URLSession(configuration: config, delegate: self, delegateQueue: nil) | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Low priority request | |
var prefetchRequest = URLRequest(url: URL(string: "https://www.apple.com")!) | |
prefetchRequest.networkServiceType = .background | |
// High priority request | |
var checkoutRequest = URLRequest(url: URL(string: "https://www.apple.com")!) | |
checkoutRequest.networkServiceType = .responsiveData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let config = URLSessionConfiguration.default | |
// You can explore the other types to set on Apple's documentation | |
config.multipathServiceType = .handover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = URLRequest(url: myURL)) | |
// If our server supports HTTP/3.0, we can enable this to speed up the discovery process; allowing our stack to assume we have a HTTP/3.0 compliant server. | |
request.assumesHTTP3Capable = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct UITestConnectionManager { | |
public static func setMockedResponse(with response: MockResponse) { | |
UITestConnectionHandler.setAllowedResponse(with: response) | |
} | |
public static func register() { | |
URLProtocol.registerClass(UITestConnectionHandler.self) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class MyAppUITests: XCTestCase { | |
override func setUpWithError() throws { | |
continueAfterFailure = false | |
} | |
func testShoppingList() { | |
UITestConnectionManager.setMockedResponse(with: .list) | |
let app = XCUIApplication() |
NewerOlder