KMPurchaseManager.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // KMPurchaseManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/6/1.
  6. //
  7. import Cocoa
  8. @objc enum KMPurchaseManagerState: Int, CaseIterable {
  9. case unknow = 0//"unknow"
  10. case trial = 1//"Trial" //试用
  11. case trialExpired = 2//"Trial Expired" //试用过期
  12. case subscription = 3//"Subscription" //订阅
  13. case subscriptionExpired = 4//"Subscription Expired" //订阅过期
  14. }
  15. #if VERSION_DMG
  16. #endif
  17. typealias KMPurchaseCompletion = (_ isSuccess: Bool, _ error: KMInAppPurchaseState) -> Void
  18. typealias KMPurchaseFetchProductCompletion = (_ isSuccess: Bool, _ products: Array<Any>, _ error: KMInAppPurchaseState?) -> Void
  19. typealias KMPurchaseRestoreCompletion = (_ isSuccess: Bool, _ error: KMInAppPurchaseState) -> Void
  20. typealias KMPurchaseCheckSubscriptionStatusCompletion = (_ isSubscription: Bool) -> Void
  21. class KMPurchaseManager: NSObject {
  22. public static let manager = KMPurchaseManager()
  23. var state: KMPurchaseManagerState {
  24. get {
  25. #if DEBUG
  26. //方便调整订阅状态
  27. // return .subscription
  28. #endif
  29. if (IAPProductsManager.default().isAvailableAllFunction()) {
  30. return .subscription
  31. }
  32. if KMInAppPurchaseManager.manager.state == .subscription {
  33. return .subscription
  34. } else {
  35. return KMDMGPurchaseManager.manager.state
  36. }
  37. }
  38. }
  39. var availableProducts: [KMProduct] = []
  40. override init() {
  41. super.init()
  42. }
  43. func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) {
  44. KMPrint("获取产品中")
  45. #if VERSION_FREE
  46. KMPrint("正在获取产品中AppStore")
  47. KMInAppPurchaseManager.manager.fetchProducts(completion: completeion)
  48. #endif
  49. #if VERSION_DMG
  50. KMPrint("正在获取产品中DMG")
  51. KMDMGPurchaseManager.manager.fetchProducts(completion: completeion)
  52. #endif
  53. }
  54. func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) {
  55. KMPrint("准备订阅中")
  56. #if VERSION_FREE
  57. KMPrint("正在订阅中AppStore")
  58. #endif
  59. #if VERSION_DMG
  60. KMPrint("正在订阅中DMG")
  61. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  62. KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, email: email, completion: completion)
  63. #endif
  64. }
  65. func showStore() {
  66. }
  67. func showPurchasesInfo() {
  68. }
  69. func restorePurchases(_ completion: @escaping KMPurchaseRestoreCompletion) {
  70. KMPrint("准备restore")
  71. #if VERSION_FREE
  72. KMPrint("正在restore")
  73. KMInAppPurchaseManager.manager.restorePurchases("", completion)
  74. #endif
  75. #if VERSION_DMG
  76. KMDMGPurchaseManager.manager.restorePurchases()
  77. KMPrint("正在restore DMG")
  78. #endif
  79. }
  80. func checkSubscriptionStatus(_ completion: @escaping KMPurchaseCheckSubscriptionStatusCompletion) {
  81. #if VERSION_FREE
  82. KMInAppPurchaseManager.manager.checkSubscriptionStatus(completion)
  83. #endif
  84. #if VERSION_DMG
  85. #endif
  86. }
  87. }