AppDelegate.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import Flutter
  2. import UIKit
  3. @main
  4. @objc class AppDelegate: FlutterAppDelegate {
  5. // 存储冷启动时的URL请求
  6. private var hasLaunched: Bool = false
  7. override func application(
  8. _ application: UIApplication,
  9. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  10. ) -> Bool {
  11. GeneratedPluginRegistrant.register(with: self)
  12. let controller = window?.rootViewController as! FlutterViewController
  13. FlutterMethodChannelManager.shared.setupMethodChannels(controller: controller)
  14. // 创建事件通道
  15. let keyboardEventChannel = FlutterEventChannel(name: "keyboard_ios_events",
  16. binaryMessenger: controller.binaryMessenger)
  17. // 设置事件处理器
  18. let keyboardStreamHandler = KeyboardStreamHandler()
  19. keyboardEventChannel.setStreamHandler(keyboardStreamHandler)
  20. // 开始监听键盘切换
  21. startMonitoringKeyboardChanges()
  22. // 延迟处理
  23. DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
  24. self.hasLaunched = true
  25. }
  26. JPUSHService.setup(withOption: launchOptions, appKey: "e4c8b538fa1046eccce756de", channel: "appStore", apsForProduction: false)
  27. // JPUSHService.setup(withOption: )
  28. return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  29. }
  30. override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  31. let path = url.path
  32. if let channel = FlutterMethodChannelManager.shared.keyboardChannel {
  33. if path == "/login" {
  34. if hasLaunched {
  35. channel.invokeMethod("navigateToLogin", arguments: nil)
  36. } else {
  37. // 延迟处理
  38. DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
  39. channel.invokeMethod("navigateToLogin", arguments: nil)
  40. }
  41. }
  42. } else if path == "/member" {
  43. if hasLaunched {
  44. channel.invokeMethod("navigateToMember", arguments: nil)
  45. } else {
  46. // 延迟处理
  47. DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
  48. channel.invokeMethod("navigateToMember", arguments: nil)
  49. }
  50. }
  51. } else if path == "/character/market" {
  52. if hasLaunched {
  53. channel.invokeMethod("navigateToCharacterMarket", arguments: nil)
  54. } else {
  55. // 延迟处理
  56. DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
  57. channel.invokeMethod("navigateToCharacterMarket", arguments: nil)
  58. }
  59. }
  60. } else if path == "/intimacy" {
  61. if hasLaunched {
  62. channel.invokeMethod("navigateToIntimacy", arguments: nil)
  63. } else {
  64. // 延迟处理
  65. DispatchQueue.main.asyncAfter(deadline: .now() + 3.5) {
  66. channel.invokeMethod("navigateToIntimacy", arguments: nil)
  67. }
  68. }
  69. } else if path == "/character/custom" {
  70. if hasLaunched {
  71. channel.invokeMethod("navigateToCustomCharacter", arguments: nil)
  72. } else {
  73. // 延迟处理
  74. DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
  75. channel.invokeMethod("navigateToCustomCharacter", arguments: nil)
  76. }
  77. }
  78. }
  79. }
  80. return true
  81. }
  82. func startMonitoringKeyboardChanges() {
  83. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  84. }
  85. @objc func keyboardDidChange() {
  86. // 收到通知后延迟检测
  87. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
  88. if let isCustom = self?.isCustomKeybroad() {
  89. // 发送通知
  90. NotificationCenter.default.post(name: NSNotification.Name("KeyboardDidChange"), object: nil, userInfo: ["isCustomKeyboard": isCustom])
  91. }
  92. }
  93. }
  94. func isCustomKeybroad() -> Bool {
  95. let currentKeyboardName = (((UITextInputMode.activeInputModes as NSArray).filtered(using: NSPredicate(format: "isDisplayed = YES"))).last as? NSObject)?.value(forKey: "extendedDisplayName") as? String
  96. let infoDictionary = Bundle.main.infoDictionary!
  97. let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String
  98. return currentKeyboardName == appDisplayName
  99. }
  100. }
  101. // 事件流处理器
  102. class KeyboardStreamHandler: NSObject, FlutterStreamHandler {
  103. private var eventSink: FlutterEventSink?
  104. override init() {
  105. super.init()
  106. // 注册通知
  107. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChange(_:)), name: NSNotification.Name("KeyboardDidChange"), object: nil)
  108. }
  109. @objc func keyboardDidChange(_ notification: Notification) {
  110. if let userInfo = notification.userInfo,
  111. let isCustomKeyboard = userInfo["isCustomKeyboard"] as? Bool,
  112. let eventSink = eventSink {
  113. // 发送事件到Flutter
  114. eventSink(isCustomKeyboard)
  115. }
  116. }
  117. func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  118. eventSink = events
  119. return nil
  120. }
  121. func onCancel(withArguments arguments: Any?) -> FlutterError? {
  122. eventSink = nil
  123. return nil
  124. }
  125. deinit {
  126. NotificationCenter.default.removeObserver(self)
  127. }
  128. }