method_chanel_ios_util.dart 4.2 KB

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