因此,我在尝试为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);
}
我试着搜索如何做这件事的教程,但我找不到
2条答案
按热度按时间d6kp6zgx1#
确保Flutter和iOS端的频道名称相同,现在它们似乎不同:
cclgggtu2#
如果您使用方法通道调用应用删除中的任何方法或与之相关的任何扩展,则需要使channelmethod var全局化,然后需要在didFinishLaunchingWithOptions函数中示例化方法通道。