Reduce platform requirements
This commit is contained in:
parent
04dfb12052
commit
8fd1148cbd
@ -5,8 +5,8 @@ import PackageDescription
|
||||
let package = Package(
|
||||
name: "Push-iOS",
|
||||
platforms: [
|
||||
.macOS(.v12),
|
||||
.iOS(.v15),
|
||||
.macOS(.v10_15),
|
||||
.iOS(.v13),
|
||||
],
|
||||
products: [
|
||||
.library(
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user