text
stringlengths
1
474
the battery level. This code is exactly the same as you
would write in a native iOS app.Add the following as a new method at the bottom of AppDelegate.swift:
<code_start>private func receiveBatteryLevel(result: FlutterResult) {
let device = UIDevice.current
device.isBatteryMonitoringEnabled = true
if device.batteryState == UIDevice.BatteryState.unknown {
result(FlutterError(code: "UNAVAILABLE",
message: "Battery level not available.",
details: nil))
} else {
result(Int(device.batteryLevel * 100))
}
}<code_end>
Finally, complete the setMethodCallHandler() method added earlier.
You need to handle a single platform method, getBatteryLevel(),
so test for that in the call argument.
The implementation of this platform method calls
the iOS code written in the previous step. If an unknown method
is called, report that instead.
<code_start>batteryChannel.setMethodCallHandler({
[weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
// This method is invoked on the UI thread.
guard call.method == "getBatteryLevel" else {
result(FlutterMethodNotImplemented)
return
}
self?.receiveBatteryLevel(result: result)
})<code_end>
Start by opening the iOS host portion of the Flutter app in Xcode:Start Xcode.Select the menu item File > Open….Navigate to the directory holding your Flutter app,
and select the ios folder inside it. Click OK.Make sure the Xcode projects builds without errors.Open the file AppDelegate.m, located under Runner > Runner
in the Project navigator.Create a FlutterMethodChannel and add a handler inside the application
didFinishLaunchingWithOptions: method.
Make sure to use the same channel name
as was used on the Flutter client side.
<code_start>#import <Flutter/Flutter.h>
#import "GeneratedPluginRegistrant.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
methodChannelWithName:@"samples.flutter.dev/battery"
binaryMessenger:controller.binaryMessenger];
[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
// This method is invoked on the UI thread.
// TODO
}];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}<code_end>
Next, add the iOS ObjectiveC code that uses the iOS battery APIs to
retrieve the battery level. This code is exactly the same as you
would write in a native iOS app.Add the following method in the AppDelegate class, just before @end:
<code_start>- (int)getBatteryLevel {
UIDevice* device = UIDevice.currentDevice;
device.batteryMonitoringEnabled = YES;
if (device.batteryState == UIDeviceBatteryStateUnknown) {
return -1;
} else {
return (int)(device.batteryLevel * 100);
}
}<code_end>
Finally, complete the setMethodCallHandler() method added earlier.
You need to handle a single platform method, getBatteryLevel(),
so test for that in the call argument. The implementation of
this platform method calls the iOS code written in the previous step,
and returns a response for both the success and error cases using
the result argument. If an unknown method is called, report that instead.
<code_start>__weak typeof(self) weakSelf = self;
[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
// This method is invoked on the UI thread.
if ([@"getBatteryLevel" isEqualToString:call.method]) {
int batteryLevel = [weakSelf getBatteryLevel];
if (batteryLevel == -1) {
result([FlutterError errorWithCode:@"UNAVAILABLE"
message:@"Battery level not available."
details:nil]);
} else {
result(@(batteryLevel));
}
} else {
result(FlutterMethodNotImplemented);
}
}];<code_end>
You should now be able to run the app on iOS.
If using the iOS Simulator,
note that it doesn’t support battery APIs,
and the app displays ‘Battery level not available’.<topic_end>
<topic_start>
Step 5: Add a Windows platform-specific implementation
Start by opening the Windows host portion of your Flutter app in Visual Studio:Run flutter build windows in your project directory once to generate
the Visual Studio solution file.Start Visual Studio.Select Open a project or solution.Navigate to the directory holding your Flutter app, then into the build
folder, then the windows folder, then select the batterylevel.sln file.
Click Open.Add the C++ implementation of the platform channel method:Expand batterylevel > Source Files in the Solution Explorer.Open the file flutter_window.cpp.First, add the necessary includes to the top of the file, just
after #include "flutter_window.h":
<code_start>#include <flutter/event_channel.h>
#include <flutter/event_sink.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/method_channel.h>
#include <flutter/standard_method_codec.h>
#include <windows.h>