Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
8fd1148cbd | |||
04dfb12052 | |||
f0589b9b90 |
@ -5,8 +5,8 @@ import PackageDescription
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "Push-iOS",
|
name: "Push-iOS",
|
||||||
platforms: [
|
platforms: [
|
||||||
.macOS(.v12),
|
.macOS(.v10_15),
|
||||||
.iOS(.v15),
|
.iOS(.v13),
|
||||||
],
|
],
|
||||||
products: [
|
products: [
|
||||||
.library(
|
.library(
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import PushMessageDefinitions
|
import PushMessageDefinitions
|
||||||
import SwiftUI
|
|
||||||
|
|
||||||
|
// Use crypto replacement on Linux
|
||||||
#if canImport(CryptoKit)
|
#if canImport(CryptoKit)
|
||||||
import CryptoKit
|
import CryptoKit
|
||||||
#else
|
#else
|
||||||
import Crypto
|
import Crypto
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Import network types on Linux
|
||||||
|
#if canImport(FoundationNetworking)
|
||||||
|
import FoundationNetworking
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A client to interact with a push server.
|
A client to interact with a push server.
|
||||||
*/
|
*/
|
||||||
@ -140,6 +145,39 @@ public final class PushClient {
|
|||||||
var request = URLRequest(url: server.appendingPathComponent(route.rawValue))
|
var request = URLRequest(url: server.appendingPathComponent(route.rawValue))
|
||||||
request.httpBody = bodyData
|
request.httpBody = bodyData
|
||||||
request.httpMethod = "POST"
|
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 {
|
do {
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
guard let httpResponse = response as? HTTPURLResponse else {
|
guard let httpResponse = response as? HTTPURLResponse else {
|
||||||
|
Reference in New Issue
Block a user