FlutterMethodChannelManager.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // FlutterMethodChannelManager.swift
  3. // Runner
  4. //
  5. // Created by Destiny on 2025/5/8.
  6. //
  7. import Flutter
  8. import UIKit
  9. class FlutterMethodChannelManager: NSObject {
  10. // 单例模式
  11. static let shared = FlutterMethodChannelManager()
  12. // 方法通道
  13. var keyboardChannel: FlutterMethodChannel?
  14. // 私有初始化方法
  15. private override init() {
  16. super.init()
  17. }
  18. // 设置方法通道
  19. func setupMethodChannels(controller: FlutterViewController) {
  20. // 创建键盘相关的方法通道
  21. keyboardChannel = FlutterMethodChannel(
  22. name: "keyboard_ios",
  23. binaryMessenger: controller.binaryMessenger)
  24. // 设置方法调用处理器
  25. keyboardChannel?.setMethodCallHandler { [weak self] (call, result) in
  26. guard let self = self else { return }
  27. switch call.method {
  28. case "saveAuthToken":
  29. if let args = call.arguments as? [String: Any],
  30. let token = args["token"] as? String {
  31. KeyboardSharedDataManager.shared.saveToken(token)
  32. result(true)
  33. } else {
  34. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  35. }
  36. case "clearAuthToken":
  37. KeyboardSharedDataManager.shared.clearAuthToken()
  38. result(true)
  39. case "saveIDFV":
  40. if let args = call.arguments as? [String: Any],
  41. let idfv = args["idfv"] as? String {
  42. KeyboardSharedDataManager.shared.saveInitIDFV(idfv)
  43. result(true)
  44. } else {
  45. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  46. }
  47. case "saveIDFA":
  48. if let args = call.arguments as? [String: Any],
  49. let idfa = args["idfa"] as? String {
  50. KeyboardSharedDataManager.shared.saveInitIDFA(idfa)
  51. result(true)
  52. } else {
  53. result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
  54. }
  55. case "isKeyboardAdded":
  56. let isAdd = self.isKeyboardEnabled()
  57. result(isAdd)
  58. case "isDefaultKeyboard":
  59. let isDefault = self.isCustomKeybroad()
  60. result(isDefault)
  61. case "openKeyboardGuide":
  62. KeyboardSharedDataManager.shared.saveIsShowGuide()
  63. result(true)
  64. default:
  65. result(FlutterMethodNotImplemented)
  66. }
  67. }
  68. }
  69. // 是否添加键盘
  70. func isKeyboardEnabled() -> Bool {
  71. // 获取系统中已安装的键盘列表
  72. let keyboards = UserDefaults.standard.dictionaryRepresentation()
  73. .filter { $0.key.contains("Keyboard") }
  74. if let keyboardList = keyboards["AppleKeyboards"] as? [String] {
  75. let keyboardBundleId = "com.qihuan.zhuiaijianpan.AiKeyboard"
  76. let isAdded = keyboardList.contains(keyboardBundleId)
  77. return isAdded
  78. }
  79. return false
  80. }
  81. // 是否为默认键盘
  82. func isCustomKeybroad() -> Bool {
  83. let currentKeyboardName = (((UITextInputMode.activeInputModes as NSArray).filtered(using: NSPredicate(format: "isDisplayed = YES"))).last as? NSObject)?.value(forKey: "extendedDisplayName") as? String
  84. let infoDictionary = Bundle.main.infoDictionary!
  85. let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String
  86. return currentKeyboardName == appDisplayName
  87. }
  88. // 是否添加键盘
  89. func isKeyboardEnabled() -> Bool {
  90. // 获取系统中已安装的键盘列表
  91. let keyboards = UserDefaults.standard.dictionaryRepresentation()
  92. .filter { $0.key.contains("Keyboard") }
  93. if let keyboardList = keyboards["AppleKeyboards"] as? [String] {
  94. let keyboardBundleId = "com.qihuan.zhuiaijianpan.AiKeyboard"
  95. let isAdded = keyboardList.contains(keyboardBundleId)
  96. return isAdded
  97. }
  98. return false
  99. }
  100. // 是否为默认键盘
  101. func isCustomKeybroad() -> Bool {
  102. let currentKeyboardName = (((UITextInputMode.activeInputModes as NSArray).filtered(using: NSPredicate(format: "isDisplayed = YES"))).last as? NSObject)?.value(forKey: "extendedDisplayName") as? String
  103. let infoDictionary = Bundle.main.infoDictionary!
  104. let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String
  105. return currentKeyboardName == appDisplayName
  106. }
  107. }