7 #import <AudioToolbox/AudioToolbox.h>
8 #import <Foundation/Foundation.h>
9 #import <UIKit/UIApplication.h>
10 #import <UIKit/UIKit.h>
12 #include "flutter/fml/logging.h"
13 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
38 "io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
40 "io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
42 "io.flutter.plugin.platform.SystemChromeOverlayNotificationName";
44 "io.flutter.plugin.platform.SystemChromeOverlayNotificationKey";
52 if (flutterApplication) {
53 flutterApplication.statusBarHidden = hidden;
55 [FlutterLogger logWarning:
@"Application based status bar styling is not available in app "
62 if (flutterApplication) {
65 [flutterApplication setStatusBarStyle:style];
67 [FlutterLogger logWarning:
@"Application based status bar styling is not available in app "
81 @property(nonatomic, assign) BOOL enableViewControllerBasedStatusBarAppearance;
87 @property(nonatomic, strong) UITextField* textField;
93 FML_DCHECK(
engine) <<
"engine must be set";
98 NSObject* infoValue = [[NSBundle mainBundle]
99 objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
100 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
101 if (infoValue != nil && ![infoValue isKindOfClass:[NSNumber
class]]) {
102 [FlutterLogger logError:@"The value of UIViewControllerBasedStatusBarAppearance in "
103 "Info.plist must be a Boolean type."];
106 _enableViewControllerBasedStatusBarAppearance =
107 (infoValue == nil || [(NSNumber*)infoValue boolValue]);
114 NSString* method = call.
method;
116 if ([method isEqualToString:
@"SystemSound.play"]) {
117 [
self playSystemSound:args];
119 }
else if ([method isEqualToString:
@"HapticFeedback.vibrate"]) {
120 [
self vibrateHapticFeedback:args];
122 }
else if ([method isEqualToString:
@"SystemChrome.setPreferredOrientations"]) {
123 [
self setSystemChromePreferredOrientations:args];
125 }
else if ([method isEqualToString:
@"SystemChrome.setApplicationSwitcherDescription"]) {
126 [
self setSystemChromeApplicationSwitcherDescription:args];
128 }
else if ([method isEqualToString:
@"SystemChrome.setEnabledSystemUIOverlays"]) {
129 [
self setSystemChromeEnabledSystemUIOverlays:args];
131 }
else if ([method isEqualToString:
@"SystemChrome.setEnabledSystemUIMode"]) {
132 [
self setSystemChromeEnabledSystemUIMode:args];
134 }
else if ([method isEqualToString:
@"SystemChrome.restoreSystemUIOverlays"]) {
135 [
self restoreSystemChromeSystemUIOverlays];
137 }
else if ([method isEqualToString:
@"SystemChrome.setSystemUIOverlayStyle"]) {
138 [
self setSystemChromeSystemUIOverlayStyle:args];
140 }
else if ([method isEqualToString:
@"SystemNavigator.pop"]) {
141 NSNumber* isAnimated = args;
142 [
self popSystemNavigator:isAnimated.boolValue];
144 }
else if ([method isEqualToString:
@"Clipboard.getData"]) {
145 result([
self getClipboardData:args]);
146 }
else if ([method isEqualToString:
@"Clipboard.setData"]) {
147 [
self setClipboardData:args];
149 }
else if ([method isEqualToString:
@"Clipboard.hasStrings"]) {
150 result([
self clipboardHasStrings]);
151 }
else if ([method isEqualToString:
@"LiveText.isLiveTextInputAvailable"]) {
152 result(@([
self isLiveTextInputAvailable]));
153 }
else if ([method isEqualToString:
@"SearchWeb.invoke"]) {
154 [
self searchWeb:args];
156 }
else if ([method isEqualToString:
@"LookUp.invoke"]) {
157 [
self showLookUpViewController:args];
159 }
else if ([method isEqualToString:
@"Translate.invoke"]) {
160 if (@available(iOS 17.4, *)) {
161 [
self showTranslateViewControllerForTerm:args];
166 }
else if ([method isEqualToString:
@"Share.invoke"]) {
167 [
self showShareViewController:args];
169 }
else if ([method isEqualToString:
@"ContextMenu.showSystemContextMenu"]) {
170 [
self showSystemContextMenu:args];
172 }
else if ([method isEqualToString:
@"ContextMenu.hideSystemContextMenu"]) {
173 [
self hideSystemContextMenu];
180 - (void)showSystemContextMenu:(NSDictionary*)args {
181 if (@available(iOS 16.0, *)) {
183 BOOL shownEditMenu = [textInputPlugin
showEditMenu:args];
184 if (!shownEditMenu) {
185 [FlutterLogger logError:@"Only text input supports system context menu for now. Ensure the "
186 "system context menu is shown with an active text input connection. "
187 "See https://github.com/flutter/flutter/issues/143033."];
192 - (void)hideSystemContextMenu {
193 if (@available(iOS 16.0, *)) {
195 [textInputPlugin hideEditMenu];
199 - (void)showShareViewController:(NSString*)content {
200 UIViewController* engineViewController = [
self.engine viewController];
202 NSArray* itemsToShare = @[ content ?: [NSNull null] ];
203 UIActivityViewController* activityViewController =
204 [[UIActivityViewController alloc] initWithActivityItems:itemsToShare
205 applicationActivities:nil];
207 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
211 UITextRange* range = _textInputPlugin.
textInputView.selectedTextRange;
216 caretRectForPosition:(FlutterTextPosition*)range.start];
218 localRectFromFrameworkTransform:firstRect];
220 caretRectForPosition:(FlutterTextPosition*)range.end];
222 localRectFromFrameworkTransform:lastRect];
224 activityViewController.popoverPresentationController.sourceView = engineViewController.view;
226 activityViewController.popoverPresentationController.sourceRect =
227 CGRectMake(fmin(transformedFirstRect.origin.x, transformedLastRect.origin.x),
228 transformedFirstRect.origin.y,
229 abs(transformedLastRect.origin.x - transformedFirstRect.origin.x),
230 transformedFirstRect.size.height);
233 [engineViewController presentViewController:activityViewController animated:YES completion:nil];
236 - (void)searchWeb:(NSString*)searchTerm {
238 if (flutterApplication == nil) {
239 [FlutterLogger logWarning:@"SearchWeb.invoke is not availabe in app extension."];
243 NSString* escapedText = [searchTerm
244 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet
245 URLHostAllowedCharacterSet]];
246 NSString* searchURL = [NSString stringWithFormat:@"%@%@", kSearchURLPrefix, escapedText];
248 [flutterApplication openURL:[NSURL URLWithString:searchURL] options:@{} completionHandler:nil];
251 - (void)playSystemSound:(NSString*)soundType {
252 if ([soundType isEqualToString:
@"SystemSoundType.click"]) {
256 }
else if ([soundType isEqualToString:
@"SystemSoundType.tick"]) {
261 - (void)vibrateHapticFeedback:(NSString*)feedbackType {
263 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
267 if ([
@"HapticFeedbackType.lightImpact" isEqualToString:feedbackType]) {
268 [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight] impactOccurred];
269 }
else if ([
@"HapticFeedbackType.mediumImpact" isEqualToString:feedbackType]) {
270 [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] impactOccurred];
271 }
else if ([
@"HapticFeedbackType.heavyImpact" isEqualToString:feedbackType]) {
272 [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy] impactOccurred];
273 }
else if ([
@"HapticFeedbackType.selectionClick" isEqualToString:feedbackType]) {
274 [[[UISelectionFeedbackGenerator alloc] init] selectionChanged];
275 }
else if ([
@"HapticFeedbackType.successNotification" isEqualToString:feedbackType]) {
276 [[[UINotificationFeedbackGenerator alloc] init]
277 notificationOccurred:UINotificationFeedbackTypeSuccess];
278 }
else if ([
@"HapticFeedbackType.warningNotification" isEqualToString:feedbackType]) {
279 [[[UINotificationFeedbackGenerator alloc] init]
280 notificationOccurred:UINotificationFeedbackTypeWarning];
281 }
else if ([
@"HapticFeedbackType.errorNotification" isEqualToString:feedbackType]) {
282 [[[UINotificationFeedbackGenerator alloc] init]
283 notificationOccurred:UINotificationFeedbackTypeError];
287 - (void)setSystemChromePreferredOrientations:(NSArray*)orientations {
288 UIInterfaceOrientationMask mask = 0;
290 if (orientations.count == 0) {
291 mask |= UIInterfaceOrientationMaskAll;
293 for (NSString* orientation in orientations) {
294 if ([orientation isEqualToString:
@"DeviceOrientation.portraitUp"]) {
295 mask |= UIInterfaceOrientationMaskPortrait;
296 }
else if ([orientation isEqualToString:
@"DeviceOrientation.portraitDown"]) {
297 mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
298 }
else if ([orientation isEqualToString:
@"DeviceOrientation.landscapeLeft"]) {
299 mask |= UIInterfaceOrientationMaskLandscapeLeft;
300 }
else if ([orientation isEqualToString:
@"DeviceOrientation.landscapeRight"]) {
301 mask |= UIInterfaceOrientationMaskLandscapeRight;
309 [[NSNotificationCenter defaultCenter]
310 postNotificationName:@(kOrientationUpdateNotificationName)
312 userInfo:@{@(kOrientationUpdateNotificationKey) : @(mask)}];
315 - (void)setSystemChromeApplicationSwitcherDescription:(NSDictionary*)object {
319 - (void)setSystemChromeEnabledSystemUIOverlays:(NSArray*)overlays {
320 BOOL statusBarShouldBeHidden = ![overlays containsObject:@"SystemUiOverlay.top"];
321 if ([overlays containsObject:
@"SystemUiOverlay.bottom"]) {
322 [[NSNotificationCenter defaultCenter]
323 postNotificationName:FlutterViewControllerShowHomeIndicator
326 [[NSNotificationCenter defaultCenter]
327 postNotificationName:FlutterViewControllerHideHomeIndicator
330 if (
self.enableViewControllerBasedStatusBarAppearance) {
331 [
self.engine viewController].prefersStatusBarHidden = statusBarShouldBeHidden;
343 - (void)setSystemChromeEnabledSystemUIMode:(NSString*)mode {
344 BOOL edgeToEdge = [mode isEqualToString:@"SystemUiMode.edgeToEdge"];
345 if (
self.enableViewControllerBasedStatusBarAppearance) {
346 [
self.engine viewController].prefersStatusBarHidden = !edgeToEdge;
356 [[NSNotificationCenter defaultCenter]
357 postNotificationName:edgeToEdge ? FlutterViewControllerShowHomeIndicator
358 : FlutterViewControllerHideHomeIndicator
362 - (void)restoreSystemChromeSystemUIOverlays {
366 - (void)setSystemChromeSystemUIOverlayStyle:(NSDictionary*)message {
367 NSString* brightness = message[@"statusBarBrightness"];
368 if (brightness == (
id)[NSNull
null]) {
372 UIStatusBarStyle statusBarStyle;
373 if ([brightness isEqualToString:
@"Brightness.dark"]) {
374 statusBarStyle = UIStatusBarStyleLightContent;
375 }
else if ([brightness isEqualToString:
@"Brightness.light"]) {
376 statusBarStyle = UIStatusBarStyleDarkContent;
381 if (
self.enableViewControllerBasedStatusBarAppearance) {
383 [[NSNotificationCenter defaultCenter]
384 postNotificationName:@(kOverlayStyleUpdateNotificationName)
386 userInfo:@{@(kOverlayStyleUpdateNotificationKey) : @(statusBarStyle)}];
392 - (void)popSystemNavigator:(BOOL)isAnimated {
400 UINavigationController* navigationController = [engineViewController navigationController];
401 if (navigationController) {
402 [navigationController popViewControllerAnimated:isAnimated];
404 UIViewController* rootViewController = nil;
406 if (flutterApplication) {
407 rootViewController = flutterApplication.keyWindow.rootViewController;
410 [engineViewController flutterWindowSceneIfViewLoaded].keyWindow.rootViewController;
413 if (engineViewController != rootViewController) {
414 [engineViewController dismissViewControllerAnimated:isAnimated completion:nil];
419 - (NSDictionary*)getClipboardData:(NSString*)format {
420 UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
422 NSString* stringInPasteboard = pasteboard.string;
424 return stringInPasteboard == nil ? nil : @{
@"text" : stringInPasteboard};
429 - (void)setClipboardData:(NSDictionary*)data {
430 UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
431 id copyText = data[@"text"];
432 if ([copyText isKindOfClass:[NSString
class]]) {
433 pasteboard.string = copyText;
435 pasteboard.string =
@"null";
439 - (NSDictionary*)clipboardHasStrings {
440 return @{
@"value" : @([UIPasteboard generalPasteboard].hasStrings)};
443 - (BOOL)isLiveTextInputAvailable {
444 return [[
self textField] canPerformAction:@selector(captureTextFromCamera:) withSender:nil];
447 - (void)showLookUpViewController:(NSString*)term {
448 UIViewController* engineViewController = [
self.engine viewController];
449 UIReferenceLibraryViewController* referenceLibraryViewController =
450 [[UIReferenceLibraryViewController alloc] initWithTerm:term];
451 [engineViewController presentViewController:referenceLibraryViewController
456 - (CGRect)popoverSourceRectForCurrentSelection {
469 return CGRectMake(fmin(transformedFirstRect.origin.x, transformedLastRect.origin.x),
470 transformedFirstRect.origin.y,
471 fabs(transformedLastRect.origin.x - transformedFirstRect.origin.x),
472 transformedFirstRect.size.height);
475 - (void)showTranslateViewControllerForTerm:(NSString*)term {
476 if (@available(iOS 17.4, *)) {
477 if (term.length == 0) {
480 UIViewController* flutterViewController = [
self.engine viewController];
481 if (!flutterViewController) {
484 FlutterTranslateViewController* translateViewController;
486 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
489 CGRect popoverSourceRect = [
self popoverSourceRectForCurrentSelection];
490 translateViewController =
491 [[FlutterTranslateViewController alloc] initWithTerm:term
492 popoverSourceRect:popoverSourceRect];
494 translateViewController = [[FlutterTranslateViewController alloc] initWithTerm:term];
497 translateViewController.view.frame = flutterViewController.view.bounds;
498 [flutterViewController addChildViewController:translateViewController];
499 [flutterViewController.view addSubview:translateViewController.view];
500 [translateViewController didMoveToParentViewController:flutterViewController];
504 - (UITextField*)textField {
505 if (_textField == nil) {
506 _textField = [[UITextField alloc] init];
void(^ FlutterResult)(id _Nullable result)
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
CGRect localRectFromFrameworkTransform
CGRect caretRectForPosition
FlutterTextInputPlugin * textInputPlugin
UIApplication * application
UIView< UITextInput > * textInputView()
BOOL showEditMenu:(ios(16.0) API_AVAILABLE)
constexpr char kTextPlainFormat[]
const UInt32 kKeyPressClickSoundId
NSString *const kSearchURLPrefix
const UInt32 kWheelsOfTimeSoundId
const char *const kOrientationUpdateNotificationKey
const char *const kOverlayStyleUpdateNotificationName
const char *const kOverlayStyleUpdateNotificationKey
const char *const kOrientationUpdateNotificationName