text stringlengths 1 372 |
|---|
if (batterylevel != -1) { |
result.success(batteryLevel); |
} else { |
result.error("UNAVAILABLE", "battery level not available.", null); |
} |
} else { |
result.notImplemented(); |
} |
} |
<code_end> |
you should now be able to run the app on android. if using the android |
emulator, set the battery level in the extended controls panel |
accessible from the … button in the toolbar. |
<topic_end> |
<topic_start> |
step 4: add an iOS platform-specific implementation |
start by opening the iOS host portion of your 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. |
add support for swift in the standard template setup that uses Objective-C: |
expand runner > runner in the project navigator. |
open the file AppDelegate.swift located under runner > runner |
in the project navigator. |
override the application:didFinishLaunchingWithOptions: function and create |
a FlutterMethodChannel tied to the channel name |
samples.flutter.dev/battery: |
<code_start> |
@uiapplicationmain |
@objc class AppDelegate: FlutterAppDelegate { |
override func application( |
_ application: UIApplication, |
didFinishLaunchingWithOptions launchOptions: [uiapplication.launchoptionskey: any]?) -> bool { |
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController |
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/battery", |
binaryMessenger: controller.binaryMessenger) |
batteryChannel.setMethodCallHandler({ |
(call: FlutterMethodCall, result: @escaping FlutterResult) -> void in |
// this method is invoked on the UI thread. |
// handle battery messages. |
}) |
GeneratedPluginRegistrant.register(with: self) |
return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
} |
} |
<code_end> |
next, add the iOS swift 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 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.