mirror of
https://github.com/flutter/samples.git
synced 2026-04-06 19:52:04 +00:00
add iOS implementation for battery plugin (#579)
This commit is contained in:
@@ -3,12 +3,24 @@ import UIKit
|
||||
|
||||
public class SwiftFederatedPlugin: NSObject, FlutterPlugin {
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let channel = FlutterMethodChannel(name: "federated_plugin", binaryMessenger: registrar.messenger())
|
||||
let channel = FlutterMethodChannel(name: "battery", binaryMessenger: registrar.messenger())
|
||||
let instance = SwiftFederatedPlugin()
|
||||
registrar.addMethodCallDelegate(instance, channel: channel)
|
||||
}
|
||||
|
||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
result("iOS " + UIDevice.current.systemVersion)
|
||||
guard call.method == "getBatteryLevel" else {
|
||||
result(FlutterMethodNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
let device = UIDevice.current
|
||||
device.isBatteryMonitoringEnabled = true
|
||||
|
||||
if device.batteryState == UIDevice.BatteryState.unknown {
|
||||
result(FlutterError(code: "STATUS_UNAVAILABLE", message: "Not able to determine battery level", details: nil))
|
||||
}
|
||||
|
||||
result(Int(device.batteryLevel * 100))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user