英文:
Platform Method Channel From iOS Side to Flutter
问题
所以,当我尝试为我的Flutter项目实现平台方法通道时,我遇到了一个问题。当我尝试从Flutter调用iOS端的方法时,它被触发并一切都运行完美,但是当我尝试从iOS端(AppDelegate文件)调用一个方法以执行特定任务时,它不起作用。
AppDelegate.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 main.dart:
const methodChannel = MethodChannel('channelMethodTest');
Future<void> methodHandler(MethodCall call) async {
final String idea = call.arguments;
switch (call.method) {
case "taskName":
print("从iOS到Flutter接收任务");
break;
default:
print('对于方法 ${call.method} 没有方法处理程序');
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
methodChannel.setMethodCallHandler(methodHandler);
}
我尝试搜索了一些教程,但是没有找到适合的解决方法。
<details>
<summary>英文:</summary>
so i have an issue when trying to implement the platform method channel for my flutter project, When i try to invoke a method from flutter to ios side, it gets triggered and everything is working perfectly but when i try to invoke a method from ios side (appDelegate file) to flutter in order to perform a sepecific task, it's not working.
AppDelegate.swift Code:
@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 main.dart:
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);
}
I tried searching for tutos on how to do it but i cannot find any
</details>
# 答案1
**得分**: 0
让渠道名称在Flutter和iOS端保持相同。现在它们似乎是不同的:
```Dart
let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
const methodChannel = MethodChannel('beyang.com-channelMethodTest');
英文:
Ensure that the channel name is the same in Flutter and iOS side. Now they seem to be different:
let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
const methodChannel = MethodChannel('beyang.com-channelMethodTest');
答案2
得分: 0
需要将channelmethod
变量设为全局变量,然后需要在didFinishLaunchingWithOptions
函数中实例化方法通道(method channel
),如果你要使用方法通道(method channel
)来调用应用程序代理(app delegate
)或与之相关的任何扩展(extension)中的方法。
英文:
you need to make the channelmethod var global then you need to instantiate the method channel in the didFinishLaunchingWithOptions func if you're using the method channel to invoke any method in the app deletage or any extension related to it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论