method_chanel_ios_util.dart 3.8 KB

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