KMPurchaseManager.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // KMPurchaseManager.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/6/1.
  6. //
  7. import Cocoa
  8. enum KMPurchaseManagerState: String, CaseIterable {
  9. case unknow = "unknow"
  10. case trial = "Trial" //试用
  11. case trialExpired = "Trial Expired" //试用过期
  12. case subscription = "Subscription" //订阅
  13. case subscriptionExpired = "Subscription Expired" //订阅过期
  14. }
  15. #if VERSION_DMG
  16. #endif
  17. typealias KMPurchaseCompletion = (_ isSuccess: Bool, _ error: String) -> Void
  18. typealias KMPurchaseFetchProductCompletion = (_ isSuccess: Bool, _ products: Array<Any>, _ error: String?) -> Void
  19. class KMPurchaseManager: NSObject {
  20. public static let manager = KMPurchaseManager()
  21. var state: KMPurchaseManagerState = .unknow
  22. var availableProducts: [KMProduct] = []
  23. override init() {
  24. super.init()
  25. // self.fetchProducts { isSuccess, products, error in
  26. // if isSuccess {
  27. // print("获取产品成功")
  28. // } else {
  29. // print("获取产品失败")
  30. // }
  31. // print(products)
  32. // }
  33. }
  34. func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) {
  35. print("获取产品中")
  36. #if VERSION_FREE
  37. print("正在获取产品中AppStore")
  38. KMInAppPurchaseManager.manager.fetchProducts(completion: completeion)
  39. #endif
  40. #if VERSION_DMG
  41. print("正在获取产品中DMG")
  42. KMDMGPurchaseManager.manager.fetchProducts(completion: completeion)
  43. #endif
  44. }
  45. func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) {
  46. print("准备订阅中")
  47. #if VERSION_FREE
  48. print("正在订阅中AppStore")
  49. KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, completion: completion)
  50. #endif
  51. #if VERSION_DMG
  52. print("正在订阅中DMG")
  53. KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, completion: completion)
  54. #endif
  55. }
  56. func restorePurchases() {
  57. print("准备restore")
  58. #if VERSION_FREE
  59. print("正在restore")
  60. KMInAppPurchaseManager.manager.restorePurchases()
  61. #endif
  62. #if VERSION_DMG
  63. print("正在restore DMG")
  64. #endif
  65. }
  66. }