method_chanel_ios_util.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:io';
  2. import 'package:flutter/services.dart';
  3. import 'package:get/get.dart';
  4. import 'package:keyboard/module/character_custom/character_custom_page.dart';
  5. import 'package:keyboard/module/login/login_page.dart';
  6. import 'package:keyboard/module/main/main_page.dart';
  7. import 'package:keyboard/module/store/store_page.dart';
  8. import 'package:keyboard/utils/default_keyboard_helper.dart';
  9. import '../module/intimacy_scale/intimacy_scale_page.dart';
  10. class MethodChanelIOSUtil {
  11. static final MethodChanelIOSUtil _instance = MethodChanelIOSUtil._internal();
  12. factory MethodChanelIOSUtil() => _instance;
  13. MethodChanelIOSUtil._internal();
  14. static const MethodChannel _channel = MethodChannel('keyboard_ios');
  15. static void initialize() {
  16. _channel.setMethodCallHandler(_handleMethod);
  17. // 默认键盘切换监听器
  18. DefaultKeyboardHelper.init();
  19. }
  20. static Future<dynamic> _handleMethod(MethodCall call) async {
  21. switch (call.method) {
  22. case 'navigateToLogin':
  23. LoginPage.start();
  24. break;
  25. case 'navigateToMember':
  26. StorePage.start();
  27. break;
  28. case 'navigateToCharacterMarket':
  29. MainPage.start(arguments: {"tabName": "character"});
  30. break;
  31. case 'navigateToCustomCharacter':
  32. CharacterCustomPage.start();
  33. break;
  34. case 'navigateToIntimacy':
  35. IntimacyScalePage.start();
  36. break;
  37. case 'isKeyboardAdd':
  38. break;
  39. default:
  40. throw PlatformException(
  41. code: 'Unimplemented',
  42. details: '未实现的方法: ${call.method}',
  43. );
  44. }
  45. }
  46. // 保存token到ios端
  47. static Future<void> saveAuthToken(String token) async {
  48. // 通知iOS键盘扩展
  49. if (Platform.isIOS) {
  50. _channel.invokeMethod('saveAuthToken', {'token': token});
  51. }
  52. }
  53. // 保存idfv到ios端
  54. static Future<void> saveIDFV(String? idfv) async {
  55. // 通知iOS键盘扩展
  56. if (Platform.isIOS) {
  57. _channel.invokeMethod('saveIDFV', {'idfv': idfv});
  58. }
  59. }
  60. // 保存idfa到ios端
  61. static Future<void> saveIDFA(String? idfa) async {
  62. // 通知iOS键盘扩展
  63. if (Platform.isIOS) {
  64. _channel.invokeMethod('saveIDFA', {'idfa': idfa});
  65. }
  66. }
  67. // 保存token到ios端
  68. static Future<void> clearAuthToken() async {
  69. // 通知iOS键盘扩展
  70. if (Platform.isIOS) {
  71. _channel.invokeMethod('clearAuthToken');
  72. }
  73. }
  74. // 检查键盘是否已添加
  75. static Future<bool> isDefaultKeyboard() async {
  76. try {
  77. // 调用原生方法并等待返回结果
  78. final bool result = await _channel.invokeMethod('isDefaultKeyboard');
  79. return result;
  80. } on PlatformException catch (e) {
  81. print('检查键盘是否为默认键盘: ${e.message}');
  82. return false; // 发生错误时返回 false
  83. }
  84. }
  85. // 检查键盘是否已添加
  86. static Future<bool> isKeyboardAdded() async {
  87. try {
  88. // 调用原生方法并等待返回结果
  89. final bool result = await _channel.invokeMethod('isKeyboardAdded');
  90. return result;
  91. } on PlatformException catch (e) {
  92. print('检查键盘是否添加失败: ${e.message}');
  93. return false; // 发生错误时返回 false
  94. }
  95. }
  96. static Future<void> openKeyboardGuide() async {
  97. // 通知iOS键盘扩展
  98. if (Platform.isIOS) {
  99. _channel.invokeMethod('openKeyboardGuide');
  100. }
  101. }
  102. }