跳转至正文

利用 Apple 系统 API 与框架

了解提供与 Apple 框架等价功能的 Flutter 插件。

若你来自 iOS 开发背景,可能需要找到与 Apple 系统库能力相当的 Flutter 插件,例如访问设备硬件或与 HealthKit 等特定框架交互。

要了解 SwiftUI 框架与 Flutter 的对比,请参阅 面向 SwiftUI 开发者的 Flutter 指南

Flutter 插件简介

#

Dart 将包含平台相关代码的库称为 plugins(plugin package 的缩写)。使用 Flutter 开发应用时,通过 plugins 与系统库交互。

在 Dart 代码中,你使用插件的 Dart API 调用所用系统库中的原生代码。也就是说,你只需编写调用 Dart API 的代码,API 会在插件支持的所有平台上生效。

有关插件的更多信息,请参阅 使用 package。本页虽链接了一些常用插件,你仍可在 pub.dev 上找到数千个插件及示例。下表不代表对任何特定插件的背书。若找不到满足需求的 package,可自行创建或在项目中直接使用 platform channel。详情请参阅 编写平台相关代码

向项目添加插件

#

要在原生项目中使用 Apple 框架,请将其导入 Swift 或 Objective-C 文件。

要添加 Flutter 插件,在项目根目录运行 flutter pub add package_name。这会将依赖写入 pubspec.yaml。添加依赖后,在 Dart 文件中 import 该 package。

你可能需要修改应用设置或初始化逻辑;如有需要,pub.dev 上该 package 的「Readme」页面会提供说明。

Flutter 插件与 Apple 框架

#
使用场景Apple 框架或类Flutter 插件
访问照片库 使用 PhotosPhotosUI 框架及 UIImagePickerControllerPhotoKit image_picker
访问相机 使用 .camera sourceTypeUIImagePickerController image_picker
使用高级相机功能 AVFoundation camera
提供应用内购买 StoreKit in_app_purchase 1 in_app_purchase 1
处理支付 PassKit pay 2 pay 2
发送推送通知 UserNotifications firebase_messaging 3 firebase_messaging 3
获取 GPS 坐标 CoreLocation geolocator
访问传感器数据4 CoreMotion sensors_plus
发起网络请求 URLSession http
存储键值对 @AppStorage 属性包装器与 NSUserDefaults shared_preferences
持久化到数据库 CoreData 或 SQLite sqflite
访问健康数据 HealthKit health
使用机器学习 CoreML google_ml_kit 5 google_ml_kit 5
识别文字 VisionKit google_ml_kit 5 google_ml_kit 5
语音识别 Speech speech_to_text
使用增强现实 ARKit ar_flutter_plugin
访问天气数据 WeatherKit weather 6 weather 6
访问和管理联系人 Contacts contacts_service
在主屏幕暴露快捷操作 UIApplicationShortcutItem quick_actions
在 Spotlight 搜索中建立索引 CoreSpotlight flutter_core_spotlight
配置、更新 widget 并与之通信 WidgetKit home_widget
通过 Siri/快捷指令自动化应用操作 AppIntents intelligence
  1. Supports both Google Play Store on Android and Apple App Store on iOS. 同时支持 Android 的 Google Play 商店与 iOS 的 Apple App Store。 2

  2. Adds Google Pay payments on Android and Apple Pay payments on iOS. 在 Android 上添加 Google Pay,在 iOS 上添加 Apple Pay。 2

  3. Uses Firebase Cloud Messaging and integrates with APNs. 使用 Firebase Cloud Messaging 并与 APNs 集成。 2

  4. Includes sensors like accelerometer, gyroscope, etc. 包括加速度计、陀螺仪等传感器。

  5. Uses Google's ML Kit and supports various features like text recognition, face detection, image labeling, landmark recognition, and barcode scanning. You can also create a custom model with Firebase. To learn more, see Use a custom TensorFlow Lite model with Flutter. 使用 Google ML Kit,支持文字识别、人脸检测、图像标注、地标识别、条码扫描等。也可通过 Firebase 创建自定义模型。详情请参阅 在 Flutter 中使用自定义 TensorFlow Lite 模型 2 3 4

  6. Uses the OpenWeatherMap API. Other packages exist that can pull from different weather APIs. 使用 OpenWeatherMap API。另有 package 可从其他天气 API 拉取数据。 2