Apple基础(Xcode④-Flutter-Platform Channels)

✅ Swift 端(iOS)

ios/Runner/AppDelegate.swift 里加几行:

Swift 复制代码
let channel = FlutterMethodChannel(name: "myapp/channel", binaryMessenger: controller.binaryMessenger)

channel.setMethodCallHandler { (call, result) in
  if call.method == "getX" {
    result(1) // 直接返回 x = 1
  } else if call.method == "getAnswer" {
    result(self.getAnswer()) // 调用方法
  } else {
    result(FlutterMethodNotImplemented)
  }
}

✅ Flutter 端(Dart)

Dart 复制代码
const channel = MethodChannel('myapp/channel');

// 获取 x
final int x = await channel.invokeMethod('getX');
print(x); // 输出 1

// 调用方法
final String msg = await channel.invokeMethod('getAnswer');
print(msg); // 输出 Hello from Swift
相关推荐
zhangmeng1 小时前
FlutterBoost在iOS26真机运行崩溃问题
flutter·app·swift
恋猫de小郭2 小时前
对于普通程序员来说 AI 是什么?AI 究竟用的是什么?
前端·flutter·ai编程
卡尔特斯2 小时前
Flutter A GlobalKey was used multipletimes inside one widget'schild list.The ...
flutter
w_y_fan5 小时前
Flutter 滚动组件总结
前端·flutter
大熊猫侯佩5 小时前
桃花岛 Xcode 构建秘籍:Swift 中的 “Feature Flags” 心法
app·xcode·swift
悄然林静6 小时前
Mac终端执行`brew doctor`报`openssl@1.1`警告
mac·xcode·apple
醉过才知酒浓6 小时前
Flutter Getx 的页面传参
flutter
权咚1 天前
阿权的开发经验小集
git·ios·xcode
小溪彼岸1 天前
macOS自带截图命令ScreenCapture
macos
火柴就是我1 天前
flutter 之真手势冲突处理
android·flutter