flutter IOS 原生交互同一个插件多个方法调用

发布时间 2023-12-28 14:38:20作者: vx_guanchaoguo0

由于代码比价简单直接上代码

ios/Runner/AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      GeneratedPluginRegistrant.register(with: self)
      
      let messenger : FlutterBinaryMessenger = window?.rootViewController as! FlutterBinaryMessenger
         
     testPlugin(messenger: messenger)
      
      
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}


func testPlugin(messenger: FlutterBinaryMessenger) {
   let channel = FlutterMethodChannel(name: "plugin_apple", binaryMessenger: messenger)
   channel.setMethodCallHandler { (call:FlutterMethodCall, result:@escaping FlutterResult) in
   
       if (call.method == "apple_one") {
           result(["result":"success","code":200]);
       }
       
       if (call.method == "apple_two") {
           result(["result":"success","code":404]);
       }

   }
}