FlutterMethodChannelManager.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // FlutterMethodChannelManager.swift
  3. // Runner
  4. //
  5. // Created by Destiny on 2025/5/8.
  6. //
  7. import Flutter
  8. import UIKit
  9. import StoreKit
  10. class FlutterMethodChannelManager: NSObject {
  11. // 单例模式
  12. static let shared = FlutterMethodChannelManager()
  13. // 方法通道
  14. var keyboardChannel: FlutterMethodChannel?
  15. // 私有初始化方法
  16. private override init() {
  17. super.init()
  18. }
  19. // 设置方法通道
  20. func setupMethodChannels(controller: FlutterViewController) {
  21. // 创建键盘相关的方法通道
  22. keyboardChannel = FlutterMethodChannel(
  23. name: "keyboard_ios",
  24. binaryMessenger: controller.binaryMessenger)
  25. // 设置方法调用处理器
  26. keyboardChannel?.setMethodCallHandler { [weak self] (call, result) in
  27. guard let self = self else { return }
  28. switch call.method {
  29. case "saveAuthToken":
  30. if let args = call.arguments as? [String: Any],
  31. let token = args["token"] as? String {
  32. KeyboardSharedDataManager.shared.saveToken(token)
  33. result(true)
  34. } else {
  35. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  36. }
  37. case "clearAuthToken":
  38. KeyboardSharedDataManager.shared.clearAuthToken()
  39. result(true)
  40. case "saveIDFV":
  41. if let args = call.arguments as? [String: Any],
  42. let idfv = args["idfv"] as? String {
  43. KeyboardSharedDataManager.shared.saveInitIDFV(idfv)
  44. result(true)
  45. } else {
  46. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  47. }
  48. case "saveIDFA":
  49. if let args = call.arguments as? [String: Any],
  50. let idfa = args["idfa"] as? String {
  51. KeyboardSharedDataManager.shared.saveInitIDFA(idfa)
  52. result(true)
  53. } else {
  54. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  55. }
  56. case "isKeyboardAdded":
  57. let isAdd = self.isKeyboardEnabled()
  58. result(isAdd)
  59. case "isDefaultKeyboard":
  60. let isDefault = self.isCustomKeybroad()
  61. result(isDefault)
  62. case "openKeyboardGuide":
  63. KeyboardSharedDataManager.shared.saveIsShowGuide()
  64. result(true)
  65. case "isHasDiscount":
  66. if let args = call.arguments as? [String: Any],
  67. let appleGoodId = args["appleGoodId"] as? String {
  68. self.checkProductDiscount(appleGoodId: appleGoodId, completion: { hasDiscount in
  69. result(hasDiscount)
  70. })
  71. } else {
  72. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  73. }
  74. default:
  75. result(FlutterMethodNotImplemented)
  76. }
  77. }
  78. }
  79. // 是否添加键盘
  80. func isKeyboardEnabled() -> Bool {
  81. // 获取系统中已安装的键盘列表
  82. let keyboards = UserDefaults.standard.dictionaryRepresentation()
  83. .filter { $0.key.contains("Keyboard") }
  84. if let keyboardList = keyboards["AppleKeyboards"] as? [String] {
  85. let keyboardBundleId = "com.qihuan.zhuiaijianpan.AiKeyboard"
  86. let isAdded = keyboardList.contains(keyboardBundleId)
  87. return isAdded
  88. }
  89. return false
  90. }
  91. // 是否为默认键盘
  92. func isCustomKeybroad() -> Bool {
  93. let currentKeyboardName = (((UITextInputMode.activeInputModes as NSArray).filtered(using: NSPredicate(format: "isDisplayed = YES"))).last as? NSObject)?.value(forKey: "extendedDisplayName") as? String
  94. let infoDictionary = Bundle.main.infoDictionary!
  95. let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String
  96. return currentKeyboardName == appDisplayName
  97. }
  98. }
  99. extension FlutterMethodChannelManager {
  100. private func checkProductDiscount(appleGoodId: String, completion: @escaping (Bool) -> Void) {
  101. if #available(iOS 15.0, *) {
  102. Task {
  103. do {
  104. // print("开始请求产品信息 (StoreKit 2)")
  105. let products = try await Product.products(for: [appleGoodId])
  106. print("成功获取产品信息: \(products.count) 个产品")
  107. if let product = products.first {
  108. // 检查是否有优惠
  109. var hasDiscount = await product.subscription?.isEligibleForIntroOffer ?? false
  110. DispatchQueue.main.async {
  111. completion(hasDiscount)
  112. }
  113. } else {
  114. print("未找到产品")
  115. DispatchQueue.main.async {
  116. completion(false)
  117. }
  118. }
  119. } catch {
  120. print("获取产品失败 (StoreKit 2): \(error)")
  121. DispatchQueue.main.async {
  122. completion(false)
  123. }
  124. }
  125. }
  126. }
  127. // // 使用StoreKit查询商品信息
  128. // let request = SKProductsRequest(productIdentifiers: [appleGoodId])
  129. // request.delegate = ProductRequestDelegate(completion: { product in
  130. // if let product = product {
  131. // // iOS 12.2及以上版本支持促销优惠
  132. // if #available(iOS 12.2, *) {
  133. // // 检查商品是否有促销优惠
  134. // let hasDiscount = product.discounts.count > 0
  135. // completion(hasDiscount)
  136. // } else {
  137. // completion(false)
  138. // }
  139. // } else {
  140. // completion(false)
  141. // }
  142. // })
  143. // request.start()
  144. }
  145. }
  146. // 处理StoreKit产品请求的代理
  147. class ProductRequestDelegate: NSObject, SKProductsRequestDelegate {
  148. private let completion: (SKProduct?) -> Void
  149. init(completion: @escaping (SKProduct?) -> Void) {
  150. self.completion = completion
  151. super.init()
  152. }
  153. func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
  154. if let product = response.products.first {
  155. completion(product)
  156. } else {
  157. completion(nil)
  158. }
  159. }
  160. func request(_ request: SKRequest, didFailWithError error: Error) {
  161. print("商品请求失败: \(error.localizedDescription)")
  162. completion(nil)
  163. }
  164. }