Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
8fd1148cbd | |||
04dfb12052 | |||
f0589b9b90 | |||
1131cb17c6 | |||
c05643c6f1 | |||
fde2062507 | |||
e4883f9b2b | |||
910f7e5f91 | |||
1092d776cd |
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ DerivedData/
|
|||||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||||
.netrc
|
.netrc
|
||||||
Package.resolved
|
Package.resolved
|
||||||
|
.swiftpm/
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
||||||
<plist version="1.0">
|
|
||||||
<dict>
|
|
||||||
<key>IDEDidComputeMac32BitWarning</key>
|
|
||||||
<true/>
|
|
||||||
</dict>
|
|
||||||
</plist>
|
|
@ -1,12 +1,12 @@
|
|||||||
// swift-tools-version: 5.6
|
// swift-tools-version: 5.5
|
||||||
|
|
||||||
import PackageDescription
|
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(
|
||||||
@ -14,14 +14,15 @@ let package = Package(
|
|||||||
targets: ["Push"]),
|
targets: ["Push"]),
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://christophhagen.de/git/ch/Push-API.git", branch: "main"),
|
.package(url: "https://christophhagen.de/git/ch/Push-Definitions.git", from: "1.0.0"),
|
||||||
|
.package(url: "https://github.com/swift-server-community/APNSwift.git", from: "4.0.0"),
|
||||||
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0")
|
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.target(
|
||||||
name: "Push",
|
name: "Push",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "PushAPI", package: "Push-API"),
|
.product(name: "PushMessageDefinitions", package: "Push-Definitions"),
|
||||||
.product(name: "Crypto", package: "swift-crypto")
|
.product(name: "Crypto", package: "swift-crypto")
|
||||||
]),
|
]),
|
||||||
]
|
]
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import PushAPI
|
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.
|
||||||
*/
|
*/
|
||||||
@ -76,7 +81,8 @@ public final class PushClient {
|
|||||||
*/
|
*/
|
||||||
public func getUnapprovedDevices(masterKey: String) async -> [DeviceRegistration]? {
|
public func getUnapprovedDevices(masterKey: String) async -> [DeviceRegistration]? {
|
||||||
let hash = hash(masterKey)
|
let hash = hash(masterKey)
|
||||||
guard let data = await post(.listUnapprovedDevices, bodyData: hash) else {
|
let request = AdminRequest(keyHash: hash, application: application)
|
||||||
|
guard let data = await post(.listUnapprovedDevices, body: request) else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
@ -139,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 {
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
import XCTest
|
|
||||||
@testable import Push_iOS
|
|
||||||
|
|
||||||
final class Push_iOSTests: XCTestCase {
|
|
||||||
func testExample() throws {
|
|
||||||
// This is an example of a functional test case.
|
|
||||||
// Use XCTAssert and related functions to verify your tests produce the correct
|
|
||||||
// results.
|
|
||||||
XCTAssertEqual(Push_iOS().text, "Hello, World!")
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user