|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// A namespace providing `URLSession` instances. |
| 18 | +enum GenAIURLSession { |
| 19 | + /// The default `URLSession` instance for the SDK; returns `URLSession.shared` by default. |
| 20 | + /// |
| 21 | + /// - Important: On affected simulators (iOS 18.4+, visionOS 2.4+), this returns an ephemeral |
| 22 | + /// `URLSession` instance as a workaround for a known system bug. |
| 23 | + static let `default` = { |
| 24 | + #if targetEnvironment(simulator) |
| 25 | + // The iOS 18.4 and visionOS 2.4 simulators (included in Xcode 16.3) contain a bug in |
| 26 | + // `URLSession` causing requests to fail. The following workaround, using an ephemeral session |
| 27 | + // resolves the issue. See https://developer.apple.com/forums/thread/777999 for more details. |
| 28 | + // |
| 29 | + // Note: This bug only impacts the simulator, not real devices, and does not impact watchOS |
| 30 | + // or tvOS. |
| 31 | + if #available(iOS 18.4, tvOS 100.0, watchOS 100.0, visionOS 2.4, *) { |
| 32 | + return URLSession(configuration: URLSessionConfiguration.ephemeral) |
| 33 | + } |
| 34 | + #endif // targetEnvironment(simulator) |
| 35 | + |
| 36 | + return URLSession.shared |
| 37 | + }() |
| 38 | +} |
0 commit comments