Skip to content

Instantly share code, notes, and snippets.

View LukeSmith16's full-sized avatar

Luke Smith LukeSmith16

  • Northamptonshire, UK
View GitHub Profile
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
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()
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 {
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)
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)
}()
// 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
let config = URLSessionConfiguration.default
// You can explore the other types to set on Apple's documentation
config.multipathServiceType = .handover
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
@LukeSmith16
LukeSmith16 / UITestConnectionManager.swift
Last active April 3, 2023 13:59
A public API for registering our subclassed URLProtocol; `UITestConnectionHandler`
public struct UITestConnectionManager {
public static func setMockedResponse(with response: MockResponse) {
UITestConnectionHandler.setAllowedResponse(with: response)
}
public static func register() {
URLProtocol.registerClass(UITestConnectionHandler.self)
}
@LukeSmith16
LukeSmith16 / MyAppUITests.swift
Created April 2, 2023 17:23
An example test using `UITestConnectionManager` with a mocked response
final class MyAppUITests: XCTestCase {
override func setUpWithError() throws {
continueAfterFailure = false
}
func testShoppingList() {
UITestConnectionManager.setMockedResponse(with: .list)
let app = XCUIApplication()