Flutter and iOS Swift: How to Interact and Add Platform-Specific Code
Flutter is a powerful framework for building cross-platform mobile apps with a single codebase. However, there may be times when you need to add platform-specific code to your app to access features that are not available in Flutter. In this blog post, we will show you how to add platform-specific code to a Flutter app to detect system uptime on iOS.
Prerequisites
Before we begin, make sure you have the following installed:
- Flutter SDK
- Xcode (for iOS development)
Creating the Method Channel
To add platform-specific code to a Flutter app, we need to create a method channel that connects the Flutter code to the native code. In this case, we will create a method channel named my_channel
that will be used to call a method that returns the system uptime on iOS.
In AppDelegate.swift
, add the following code to create the method channel:
import UIKit import Flutter @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let controller = window?.rootViewController as! FlutterViewController let channel = FlutterMethodChannel(name: "my_channel", binaryMessenger: controller.binaryMessenger) channel.setMethodCallHandler { (call, result) in if call.method == "getSystemUptime" { let uptime = ProcessInfo.processInfo.systemUptime result(uptime) } else { result(FlutterMethodNotImplemented) } } GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
This code creates a new method channel named my_channel
and adds a handler for the getSystemUptime
method. When this method is called from Flutter, it returns the system uptime using ProcessInfo.processInfo.systemUptime
.
Creating the Flutter Class
Next, we will create a new Flutter class named SystemUptime
that connects to AppDelegate.swift
and returns the system uptime using the getSystemUptime
method.
In system_uptime.dart
, add the following code:
import 'package:flutter/services.dart'; class SystemUptime { static const platform = const MethodChannel('my_channel'); static Future<double> getUptime() async { try { final double uptime = await platform.invokeMethod('getSystemUptime'); return uptime; } on PlatformException catch (e) { print("Failed to get system uptime: ${e.message}"); return 0.0; } } }
This code creates a new class named SystemUptime
that connects to AppDelegate.swift
using a method channel named my_channel
. The getUptime
method calls the getSystemUptime
method in AppDelegate.swift
and returns the system uptime.
Using the Flutter Class
Finally, we will use the SystemUptime
class in our Flutter code to display the system uptime in the app.
In main.dart
, add the following code:
import 'package:flutter/material.dart'; import 'system_uptime.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { double _uptime = 0.0; @override void initState() { super.initState(); _getSystemUptime(); } Future<void> _getSystemUptime() async { final uptime = await SystemUptime.getUptime(); setState(() { _uptime = uptime; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('System Uptime'), ), body: Center( child: Text('System uptime: $_uptime seconds'), ), ), ); } }
This code creates a new Flutter app that displays the system uptime in the app. The initState
method calls the _getSystemUptime
method to get the system uptime using the SystemUptime
class. The build
method displays the system uptime in the app.
Conclusion
In this blog post, we showed you how to add platform-specific code to a Flutter app to detect system uptime on iOS. We created a method channel in AppDelegate.swift
, a Flutter class in system_uptime.dart
, and used the class in main.dart
to display the system uptime in the app. With this knowledge, you can now add platform-specific code to your Flutter app to access features that are not available in Flutter.