method_chanel_ios_util.dart 3.8 KB

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