flutter 从iOS端到抖音的平台方法通道

rn0zuynd  于 2023-02-09  发布在  Flutter
关注(0)|答案(2)|浏览(177)

因此,我在尝试为flutter项目实现平台方法通道时遇到了一个问题,当我尝试从flutter调用一个方法到ios端时,它会被触发,一切都正常工作,但当我尝试从ios端(appDelegate文件)调用一个方法到flutter以执行特定任务时,它不工作。
应用委托.swift代码:

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    application.registerForRemoteNotifications()
    GeneratedPluginRegistrant.register(with: self)

    let controller = (window?.rootViewController as! FlutterViewController)
        let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
        methodChannle.invokeMethod("taskName", arguments: [:])
      
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

Flutter主省道:

const methodChannel = MethodChannel('channelMethodTest');

Future<void> methodHandler(MethodCall call) async {
  final String idea = call.arguments;

  switch (call.method) {
    case "taskName":
      print(
          "receiving task from ios to flutter");
      break;
    default:
      print('no method handler for method ${call.method}');
  }
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  methodChannel.setMethodCallHandler(methodHandler);
}

我试着搜索如何做这件事的教程,但我找不到

d6kp6zgx

d6kp6zgx1#

确保Flutter和iOS端的频道名称相同,现在它们似乎不同:

let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
const methodChannel = MethodChannel('beyang.com-channelMethodTest');
cclgggtu

cclgggtu2#

如果您使用方法通道调用应用删除中的任何方法或与之相关的任何扩展,则需要使channelmethod var全局化,然后需要在didFinishLaunchingWithOptions函数中示例化方法通道。

相关问题