Reduce platform requirements

This commit is contained in:
Christoph Hagen 2022-06-09 16:00:21 +02:00
parent 04dfb12052
commit 8fd1148cbd
2 changed files with 35 additions and 2 deletions

View File

@ -5,8 +5,8 @@ import PackageDescription
let package = Package(
name: "Push-iOS",
platforms: [
.macOS(.v12),
.iOS(.v15),
.macOS(.v10_15),
.iOS(.v13),
],
products: [
.library(

View File

@ -145,6 +145,39 @@ public final class PushClient {
var request = URLRequest(url: server.appendingPathComponent(route.rawValue))
request.httpBody = bodyData
request.httpMethod = "POST"
if #available(iOS 15.0, *) {
return await post(request)
} else {
return await withCheckedContinuation { continuation in
postRequest(request) { data in
continuation.resume(returning: data)
}
}
}
}
private func postRequest(_ request: URLRequest, completion: @escaping (Data?) -> Void) {
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Failed with error: \(error)")
completion(nil)
}
guard let httpResponse = response as? HTTPURLResponse else {
completion(nil)
return
}
guard httpResponse.statusCode == 200 else {
print("Failed with code: \(httpResponse.statusCode)")
completion(nil)
return
}
completion(data)
}.resume()
}
@available(iOS 15.0, *)
private func post(_ request: URLRequest) async -> Data? {
do {
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {