从iOS端到Flutter的平台方法通道

huangapple go评论71阅读模式
英文:

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&#39;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: &quot;channelMethodTest&quot;, binaryMessenger: controller.binaryMessenger)
    methodChannle.invokeMethod(&quot;taskName&quot;, 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: &quot;channelMethodTest&quot;, binaryMessenger: controller.binaryMessenger)
const methodChannel = MethodChannel(&#39;beyang.com-channelMethodTest&#39;);

答案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.

huangapple
  • 本文由 发表于 2023年2月8日 22:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定