Repository navigation
Expand file tree
/
Copy pathSubscriptionManager.swift
More file actions
76 lines (64 loc) · 2.11 KB
/
Copy pathSubscriptionManager.swift
File metadata and controls
76 lines (64 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Path: AtlasOSINT/Core/Services/SubscriptionManager.swift
import Foundation
import StoreKit
@MainActor
final class SubscriptionManager: ObservableObject {
static let shared = SubscriptionManager()
@Published var isPro: Bool = false
@Published var availableProducts: [Product] = []
private init() {}
// IDs produktů z App Store Connect
private let productIDs = [
"atlas.pro.monthly",
"atlas.pro.yearly"
]
/// Načtení produktů z App Store
func loadProducts() async {
do {
let products = try await Product.products(for: productIDs)
availableProducts = products.sorted(by: { $0.price < $1.price })
} catch {
print("❌ Failed to load products: \(error)")
}
}
/// Aktualizace entitlements – jestli má user aktivní Pro
func updateEntitlement() async {
var hasPro = false
for await result in Transaction.currentEntitlements {
guard case .verified(let transaction) = result else { continue }
if transaction.productType == .autoRenewable,
productIDs.contains(transaction.productID) {
hasPro = true
break
}
}
isPro = hasPro
}
/// Nákup předplatného
func purchase(_ product: Product) async -> Bool {
do {
let result = try await product.purchase()
switch result {
case .success(let verification):
if case .verified(let transaction) = verification {
if productIDs.contains(transaction.productID) {
isPro = true
}
await transaction.finish()
return true
} else {
return false
}
case .userCancelled:
return false
case .pending:
return false
@unknown default:
return false
}
} catch {
print("❌ Purchase failed: \(error)")
return false
}
}
}