method_chanel_ios_util.dart 3.9 KB

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