Compare commits
3 Commits
1131cb17c6
...
0.4.3
Author | SHA1 | Date | |
---|---|---|---|
8fd1148cbd | |||
04dfb12052 | |||
f0589b9b90 |
@ -5,8 +5,8 @@ import PackageDescription
|
||||
let package = Package(
|
||||
name: "Push-iOS",
|
||||
platforms: [
|
||||
.macOS(.v12),
|
||||
.iOS(.v15),
|
||||
.macOS(.v10_15),
|
||||
.iOS(.v13),
|
||||
],
|
||||
products: [
|
||||
.library(
|
||||
|
@ -1,13 +1,18 @@
|
||||
import Foundation
|
||||
import PushMessageDefinitions
|
||||
import SwiftUI
|
||||
|
||||
// Use crypto replacement on Linux
|
||||
#if canImport(CryptoKit)
|
||||
import CryptoKit
|
||||
#else
|
||||
import Crypto
|
||||
#endif
|
||||
|
||||
// Import network types on Linux
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
|
||||
/**
|
||||
A client to interact with a push server.
|
||||
*/
|
||||
@ -140,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 {
|
||||
|
Reference in New Issue
Block a user