AppDelegate.swift 4.0 KB

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