A Flutter plugin to access mobility data from Apple HealthKit, including walking speed, step length, and other mobility metrics.
The Mobility Plugin provides a simple and unified way to access mobility-related data from Apple HealthKit within your Flutter applications. This plugin allows you to:
- Fetch various mobility metrics such as walking speed, step length, double support percentage, asymmetry percentage, and walking steadiness.
- Retrieve mobility data for specific date ranges.
- Obtain recent mobility data with a specified limit.
- Request user authorization to access HealthKit data.
libiosexample
This directory contains the Dart code for the plugin.
mobility_plugin.dart: The main class that exposes the plugin's functionalities to the Flutter app. It defines methods to interact with the platform-specific implementation.mobility_plugin_method_channel.dart: Implements the platform-specific code using Flutter's method channels. It communicates with the native code (Swift for iOS) to perform the required operations.mobility_plugin_platform_interface.dart: Defines the abstract class that sets the contract for the platform-specific implementations. This ensures consistency across different platforms.
Contains the iOS-specific implementation of the plugin using Swift and Objective-C.
MobilityPlugin.swift: The core of the iOS implementation. It interacts with Apple's HealthKit framework to access mobility data. Key functionalities include:- Handling method calls from Dart and executing corresponding native code.
- Requesting authorization from the user to access HealthKit data.
- Fetching various mobility metrics and returning them to the Flutter app.
MobilityPlugin.handMobilityPlugin.m: Objective-C bridge files that facilitate the communication between Flutter and Swift code.
PrivacyInfo.xcprivacy: A privacy configuration file required by Xcode to specify how the app handles user data.mobility_plugin.podspec: The CocoaPods specification file that describes the plugin's iOS dependencies and configurations. It defines the plugin's name, version, source files, and other metadata.
An example Flutter application demonstrating how to use the Mobility Plugin. This is a practical reference to help you integrate the plugin into your own projects.
To run the example application, follow these steps:
-
Clone the repository to your local machine:
git clone https://github.com/XY-Corp/swift_modules.git cd swift_modules/example -
Navigate to the
exampledirectory of themobility_plugin:cd mobility_plugin/example -
Get the dependencies:
flutter pub get
-
Run the app on an emulator or connected device:
flutter run
main.dart: The main entry point of the example app. It showcases how to:- Initialize and use the MobilityPlugin class.
- Request authorization to access HealthKit data.
- Fetch and display mobility metrics in the app's UI.
Runner: Contains iOS-specific files for the example app.AppDelegate.swift: The application delegate for the iOS app.Assets.xcassets: App icons and launch images.Base.lproj: Storyboard files for app launch screens.GeneratedPluginRegistrant.: Files that register the plugin with the Flutter engine.Runner.entitlements: Specifies app capabilities and permissions.
plugin_integration_test.dart: Integration tests for the plugin to ensure it works as expected within an app.
Add the following to your pubspec.yaml:
dependencies:
mobility_plugin:
git:
url: url: https://github.com/XY-Corp/swift_modules.gitRun flutter pub get to fetch the plugin.
Import the plugin in your Dart code:
import 'package:mobility_plugin/mobility_plugin.dart';Initialize the Plugin:
final MobilityPlugin mobilityPlugin = MobilityPlugin();Before accessing HealthKit data, you must request authorization:
await mobilityPlugin.requestAuthorization();- Walking Speed (WALKING_SPEED)
- Step Length (STEP_LENGTH)
- Double Support Percentage (DOUBLE_SUPPORT_PERCENTAGE)
- Asymmetry Percentage (ASYMMETRY_PERCENTAGE) (iOS 15 and above)
- Walking Steadiness (WALKING_STEADINESS) (iOS 15 and above)
In your app ensure you have included the necessary permissions in your Info.plist (iOS):
<key>NSHealthShareUsageDescription</key>
<string>Need access to health data for mobility metrics.</string>


