mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
## **BREAKING CHANGE** Adopting Apple's UISceneDelegate protocol shifts the initialization order of apps. For the common cases we've made sure they will work without change. The one case that will require a change is any app that in `-[UIApplicateDelegate didFinishLaunchingWithOptions:]` assumes that `UIApplicationDelegate.window.rootViewController` is a `FlutterViewController` instance. Users should follow the [migration guide](https://docs.google.com/document/d/16WsqYbANmhupw-gxGPQPZ9B3yz1YBIviS-N1UyIQNSE/edit?tab=t.0#heading=h.txry2otwqko3) to update that usage. ## Changes since revert It's been rebased onto the FlutterPluginRegistrant PR which was used for migration. The dynamic selection of the UISceneDelegate has been removed, instead there is a flutter tool migration to the Info.plist. ## Description fixes: https://github.com/flutter/flutter/issues/167267 design doc: https://docs.google.com/document/d/1ZfcQOs-UKRa9jsFG84-MTFeibZTLKCvPQLxF2eskx44/edit?tab=t.0 relands https://github.com/flutter/flutter/pull/168396 relands https://github.com/flutter/flutter/pull/168914 relands https://github.com/flutter/flutter/pull/169276 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
104 lines
3.3 KiB
Objective-C
104 lines
3.3 KiB
Objective-C
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#import "AppDelegate.h"
|
|
#import <Flutter/Flutter.h>
|
|
#import "GeneratedPluginRegistrant.h"
|
|
|
|
@interface AppDelegate () <FlutterPluginRegistrant>
|
|
@end
|
|
|
|
@implementation AppDelegate {
|
|
FlutterEventSink _eventSink;
|
|
}
|
|
|
|
- (BOOL)application:(UIApplication*)application
|
|
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
|
|
self.pluginRegistrant = self;
|
|
return [super application:application didFinishLaunchingWithOptions:launchOptions];
|
|
}
|
|
|
|
- (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
|
|
[GeneratedPluginRegistrant registerWithRegistry:registry];
|
|
NSObject<FlutterPluginRegistrar>* registrar = [registry registrarForPlugin:@"samples.flutter.io"];
|
|
FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
|
|
methodChannelWithName:@"samples.flutter.io/battery"
|
|
binaryMessenger:registrar.messenger];
|
|
__weak typeof(self) weakSelf = self;
|
|
[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
|
|
FlutterResult result) {
|
|
if ([@"getBatteryLevel" isEqualToString:call.method]) {
|
|
int batteryLevel = [weakSelf getBatteryLevel];
|
|
if (batteryLevel == -1) {
|
|
result([FlutterError errorWithCode:@"UNAVAILABLE"
|
|
message:@"Battery info unavailable"
|
|
details:nil]);
|
|
} else {
|
|
result(@(batteryLevel));
|
|
}
|
|
} else {
|
|
result(FlutterMethodNotImplemented);
|
|
}
|
|
}];
|
|
|
|
FlutterEventChannel* chargingChannel = [FlutterEventChannel
|
|
eventChannelWithName:@"samples.flutter.io/charging"
|
|
binaryMessenger:registrar.messenger];
|
|
[chargingChannel setStreamHandler:self];
|
|
}
|
|
|
|
- (int)getBatteryLevel {
|
|
UIDevice* device = UIDevice.currentDevice;
|
|
device.batteryMonitoringEnabled = YES;
|
|
if (device.batteryState == UIDeviceBatteryStateUnknown) {
|
|
return -1;
|
|
} else {
|
|
return ((int)(device.batteryLevel * 100));
|
|
}
|
|
}
|
|
|
|
- (FlutterError*)onListenWithArguments:(id)arguments
|
|
eventSink:(FlutterEventSink)eventSink {
|
|
_eventSink = eventSink;
|
|
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
|
|
[self sendBatteryStateEvent];
|
|
[[NSNotificationCenter defaultCenter]
|
|
addObserver:self
|
|
selector:@selector(onBatteryStateDidChange:)
|
|
name:UIDeviceBatteryStateDidChangeNotification
|
|
object:nil];
|
|
return nil;
|
|
}
|
|
|
|
- (void)onBatteryStateDidChange:(NSNotification*)notification {
|
|
[self sendBatteryStateEvent];
|
|
}
|
|
|
|
- (void)sendBatteryStateEvent {
|
|
if (!_eventSink) return;
|
|
UIDeviceBatteryState state = [[UIDevice currentDevice] batteryState];
|
|
switch (state) {
|
|
case UIDeviceBatteryStateFull:
|
|
case UIDeviceBatteryStateCharging:
|
|
_eventSink(@"charging");
|
|
break;
|
|
case UIDeviceBatteryStateUnplugged:
|
|
_eventSink(@"discharging");
|
|
break;
|
|
default:
|
|
_eventSink([FlutterError errorWithCode:@"UNAVAILABLE"
|
|
message:@"Charging status unavailable"
|
|
details:nil]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
- (FlutterError*)onCancelWithArguments:(id)arguments {
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
_eventSink = nil;
|
|
return nil;
|
|
}
|
|
|
|
@end
|