|
| 1 | +// |
| 2 | +// KeychainHelper.swift |
| 3 | +// Guozaoke |
| 4 | +// |
| 5 | +// Created by scy on 2025/3/12. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Security |
| 10 | + |
| 11 | +struct KeychainKeys { |
| 12 | + static let purchaseGuozaokeKey = "purchaseGuozaokeKey" |
| 13 | +} |
| 14 | + |
| 15 | +/// 1.5.2 |
| 16 | +let purchasedVersion = "1.5.2" |
| 17 | + |
| 18 | +// MARK: - PurchaseAppState |
| 19 | + |
| 20 | +class PurchaseAppState: ObservableObject { |
| 21 | + @Published var isPurchased: Bool = false |
| 22 | + |
| 23 | + private let purchaseKey = KeychainKeys.purchaseGuozaokeKey |
| 24 | + |
| 25 | + init() { |
| 26 | + checkAndSavePurchaseStatus() |
| 27 | + } |
| 28 | + |
| 29 | + func checkAndSavePurchaseStatus() { |
| 30 | + let currentVersion = AppInfo.appVersion |
| 31 | + if currentVersion <= purchasedVersion { |
| 32 | + if let _ = KeychainHelper.retrieve(key: purchaseKey) { |
| 33 | + } else { |
| 34 | + savePurchaseStatus(isPurchased: self.isPurchased) |
| 35 | + } |
| 36 | + self.isPurchased = true |
| 37 | + } else if let savedStatus = KeychainHelper.retrieve(key: purchaseKey) { |
| 38 | + self.isPurchased = savedStatus == "purchased".data(using: .utf8) |
| 39 | + } else { |
| 40 | + self.isPurchased = false |
| 41 | + } |
| 42 | + log("[app][version][iap] currentVersion \(currentVersion) purchasedVersion \(purchasedVersion) isPurchased \(self.isPurchased)") |
| 43 | + } |
| 44 | + |
| 45 | + func savePurchaseStatus(isPurchased: Bool) { |
| 46 | + self.isPurchased = isPurchased |
| 47 | + let status = isPurchased ? "purchased" : "not_purchased" |
| 48 | + if let data = status.data(using: .utf8) { |
| 49 | + let success = KeychainHelper.save(key: purchaseKey, data: data) |
| 50 | + if success { |
| 51 | + log("[iap][purchase] Purchase status saved successfully") |
| 52 | + } else { |
| 53 | + log("[iap][purchase] Failed to save purchase status") |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + func clear() { |
| 59 | + isPurchased = false |
| 60 | + KeychainHelper.clearPurchaseStatus() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// MARK: - KeychainHelper |
| 65 | +class KeychainHelper { |
| 66 | + |
| 67 | + static func save(key: String, data: Data) -> Bool { |
| 68 | + let query: [String: Any] = [ |
| 69 | + kSecClass as String: kSecClassGenericPassword, |
| 70 | + kSecAttrAccount as String: key, |
| 71 | + kSecValueData as String: data |
| 72 | + ] |
| 73 | + SecItemDelete(query as CFDictionary) |
| 74 | + let status = SecItemAdd(query as CFDictionary, nil) |
| 75 | + return status == errSecSuccess |
| 76 | + } |
| 77 | + |
| 78 | + // Retrieve data from Keychain |
| 79 | + static func retrieve(key: String) -> Data? { |
| 80 | + let query: [String: Any] = [ |
| 81 | + kSecClass as String: kSecClassGenericPassword, |
| 82 | + kSecAttrAccount as String: key, |
| 83 | + kSecReturnData as String: kCFBooleanTrue!, |
| 84 | + kSecMatchLimit as String: kSecMatchLimitOne |
| 85 | + ] |
| 86 | + |
| 87 | + var dataTypeRef: AnyObject? = nil |
| 88 | + let status = SecItemCopyMatching(query as CFDictionary, &dataTypeRef) |
| 89 | + |
| 90 | + if status == errSecSuccess { |
| 91 | + return dataTypeRef as? Data |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + static func update(key: String, data: Data) -> Bool { |
| 98 | + let query: [String: Any] = [ |
| 99 | + kSecClass as String: kSecClassGenericPassword, |
| 100 | + kSecAttrAccount as String: key |
| 101 | + ] |
| 102 | + |
| 103 | + let attributes: [String: Any] = [ |
| 104 | + kSecValueData as String: data |
| 105 | + ] |
| 106 | + |
| 107 | + let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary) |
| 108 | + return status == errSecSuccess |
| 109 | + } |
| 110 | + |
| 111 | + static func delete(key: String) -> Bool { |
| 112 | + let query: [String: Any] = [ |
| 113 | + kSecClass as String: kSecClassGenericPassword, |
| 114 | + kSecAttrAccount as String: key |
| 115 | + ] |
| 116 | + |
| 117 | + let status = SecItemDelete(query as CFDictionary) |
| 118 | + return status == errSecSuccess |
| 119 | + } |
| 120 | + |
| 121 | + static func clearPurchaseStatus() { |
| 122 | + let clear = delete(key: KeychainKeys.purchaseGuozaokeKey) |
| 123 | + log("[iap][clear] \(clear)") |
| 124 | + } |
| 125 | +} |
0 commit comments