// // KMPurchaseManager.swift // PDF Master // // Created by lizhe on 2023/6/1. // import Cocoa enum KMPurchaseManagerState: String, CaseIterable { case unknow = "unknow" case trial = "Trial" //试用 case trialExpired = "Trial Expired" //试用过期 case subscription = "Subscription" //订阅 case subscriptionExpired = "Subscription Expired" //订阅过期 } #if VERSION_DMG #endif typealias KMPurchaseCompletion = (_ isSuccess: Bool, _ error: KMInAppPurchaseState) -> Void typealias KMPurchaseFetchProductCompletion = (_ isSuccess: Bool, _ products: Array, _ error: String?) -> Void class KMPurchaseManager: NSObject { public static let manager = KMPurchaseManager() var state: KMPurchaseManagerState { get { let info = KMLightMemberManager.manager.info.subscriptionInfoList var tempState: KMPurchaseManagerState = .unknow if info.count > 0 { let isSubscription = false for item in info { switch item.status { case 0: tempState = .unknow case 1: tempState = .subscription case 2: tempState = .subscriptionExpired case 3: tempState = .trial case 4: tempState = .trialExpired default: tempState = .unknow } } } return tempState } //#if VERSION_FREE // return KMInAppPurchaseManager.manager.state //#endif // //#if VERSION_DMG // print("获取产品状态") // return .unknow //#endif // } } var availableProducts: [KMProduct] = [] override init() { super.init() // self.fetchProducts { isSuccess, products, error in // if isSuccess { // print("获取产品成功") // } else { // print("获取产品失败") // } // print(products) // } } func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) { print("获取产品中") #if VERSION_FREE print("正在获取产品中AppStore") KMInAppPurchaseManager.manager.fetchProducts(completion: completeion) #endif #if VERSION_DMG print("正在获取产品中DMG") KMDMGPurchaseManager.manager.fetchProducts(completion: completeion) #endif } func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) { print("准备订阅中") #if VERSION_FREE print("正在订阅中AppStore") KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, completion: completion) #endif #if VERSION_DMG print("正在订阅中DMG") KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, completion: completion) #endif } func restorePurchases() { print("准备restore") #if VERSION_FREE print("正在restore") KMInAppPurchaseManager.manager.restorePurchases() #endif #if VERSION_DMG KMDMGPurchaseManager.manager.restorePurchases() print("正在restore DMG") #endif } }