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"
25 #if not APPLICATION_EXTENSION_API_ONLY
26 const NSString* searchURLPrefix =
@"x-web-search://?";
35 "io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
37 "io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
39 "io.flutter.plugin.platform.SystemChromeOverlayNotificationName";
41 "io.flutter.plugin.platform.SystemChromeOverlayNotificationKey";
48 #if not APPLICATION_EXTENSION_API_ONLY
49 [UIApplication sharedApplication].statusBarHidden = hidden;
51 FML_LOG(WARNING) <<
"Application based status bar styling is not available in app extension.";
56 #if not APPLICATION_EXTENSION_API_ONLY
59 [[UIApplication sharedApplication] setStatusBarStyle:style];
61 FML_LOG(WARNING) <<
"Application based status bar styling is not available in app extension.";
74 @property(nonatomic, assign) BOOL enableViewControllerBasedStatusBarAppearance;
80 @property(nonatomic, strong) UITextField* textField;
85 - (instancetype)initWithEngine:(fml::WeakNSObject<
FlutterEngine>)engine {
86 FML_DCHECK(
engine) <<
"engine must be set";
91 NSObject* infoValue = [[NSBundle mainBundle]
92 objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
93 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
94 if (infoValue != nil && ![infoValue isKindOfClass:[NSNumber
class]]) {
95 FML_LOG(ERROR) <<
"The value of UIViewControllerBasedStatusBarAppearance in info.plist must "
99 _enableViewControllerBasedStatusBarAppearance =
100 (infoValue == nil || [(NSNumber*)infoValue boolValue]);
107 NSString* method = call.
method;
109 if ([method isEqualToString:
@"SystemSound.play"]) {
110 [
self playSystemSound:args];
112 }
else if ([method isEqualToString:
@"HapticFeedback.vibrate"]) {
113 [
self vibrateHapticFeedback:args];
115 }
else if ([method isEqualToString:
@"SystemChrome.setPreferredOrientations"]) {
116 [
self setSystemChromePreferredOrientations:args];
118 }
else if ([method isEqualToString:
@"SystemChrome.setApplicationSwitcherDescription"]) {
119 [
self setSystemChromeApplicationSwitcherDescription:args];
121 }
else if ([method isEqualToString:
@"SystemChrome.setEnabledSystemUIOverlays"]) {
122 [
self setSystemChromeEnabledSystemUIOverlays:args];
124 }
else if ([method isEqualToString:
@"SystemChrome.setEnabledSystemUIMode"]) {
125 [
self setSystemChromeEnabledSystemUIMode:args];
127 }
else if ([method isEqualToString:
@"SystemChrome.restoreSystemUIOverlays"]) {
128 [
self restoreSystemChromeSystemUIOverlays];
130 }
else if ([method isEqualToString:
@"SystemChrome.setSystemUIOverlayStyle"]) {
131 [
self setSystemChromeSystemUIOverlayStyle:args];
133 }
else if ([method isEqualToString:
@"SystemNavigator.pop"]) {
134 NSNumber* isAnimated = args;
135 [
self popSystemNavigator:isAnimated.boolValue];
137 }
else if ([method isEqualToString:
@"Clipboard.getData"]) {
138 result([
self getClipboardData:args]);
139 }
else if ([method isEqualToString:
@"Clipboard.setData"]) {
140 [
self setClipboardData:args];
142 }
else if ([method isEqualToString:
@"Clipboard.hasStrings"]) {
143 result([
self clipboardHasStrings]);
144 }
else if ([method isEqualToString:
@"LiveText.isLiveTextInputAvailable"]) {
145 result(@([
self isLiveTextInputAvailable]));
146 }
else if ([method isEqualToString:
@"SearchWeb.invoke"]) {
147 [
self searchWeb:args];
149 }
else if ([method isEqualToString:
@"LookUp.invoke"]) {
150 [
self showLookUpViewController:args];
152 }
else if ([method isEqualToString:
@"Share.invoke"]) {
153 [
self showShareViewController:args];
155 }
else if ([method isEqualToString:
@"ContextMenu.showSystemContextMenu"]) {
156 [
self showSystemContextMenu:args];
158 }
else if ([method isEqualToString:
@"ContextMenu.hideSystemContextMenu"]) {
159 [
self hideSystemContextMenu];
166 - (void)showSystemContextMenu:(NSDictionary*)args {
167 if (@available(iOS 16.0, *)) {
169 BOOL shownEditMenu = [textInputPlugin
showEditMenu:args];
170 if (!shownEditMenu) {
171 FML_LOG(ERROR) <<
"Only text input supports system context menu for now. Ensure the system "
172 "context menu is shown with an active text input connection. See "
173 "https://github.com/flutter/flutter/issues/143033.";
178 - (void)hideSystemContextMenu {
179 if (@available(iOS 16.0, *)) {
181 [textInputPlugin hideEditMenu];
185 - (void)showShareViewController:(NSString*)content {
186 UIViewController* engineViewController = [
self.engine viewController];
188 NSArray* itemsToShare = @[ content ?: [NSNull null] ];
189 UIActivityViewController* activityViewController =
190 [[UIActivityViewController alloc] initWithActivityItems:itemsToShare
191 applicationActivities:nil];
193 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
202 caretRectForPosition:(FlutterTextPosition*)range.start];
204 localRectFromFrameworkTransform:firstRect];
206 caretRectForPosition:(FlutterTextPosition*)range.end];
208 localRectFromFrameworkTransform:lastRect];
210 activityViewController.popoverPresentationController.sourceView = engineViewController.view;
212 activityViewController.popoverPresentationController.sourceRect =
213 CGRectMake(fmin(transformedFirstRect.origin.x, transformedLastRect.origin.x),
214 transformedFirstRect.origin.y,
215 abs(transformedLastRect.origin.x - transformedFirstRect.origin.x),
216 transformedFirstRect.size.height);
219 [engineViewController presentViewController:activityViewController animated:YES completion:nil];
222 - (void)searchWeb:(NSString*)searchTerm {
223 #if APPLICATION_EXTENSION_API_ONLY
224 FML_LOG(WARNING) <<
"SearchWeb.invoke is not availabe in app extension.";
226 NSString* escapedText = [searchTerm
227 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet
228 URLHostAllowedCharacterSet]];
229 NSString* searchURL = [NSString stringWithFormat:@"%@%@", searchURLPrefix, escapedText];
231 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:searchURL]
233 completionHandler:nil];
237 - (void)playSystemSound:(NSString*)soundType {
238 if ([soundType isEqualToString:
@"SystemSoundType.click"]) {
245 - (void)vibrateHapticFeedback:(NSString*)feedbackType {
247 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
251 if ([
@"HapticFeedbackType.lightImpact" isEqualToString:feedbackType]) {
252 [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight] impactOccurred];
253 }
else if ([
@"HapticFeedbackType.mediumImpact" isEqualToString:feedbackType]) {
254 [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] impactOccurred];
255 }
else if ([
@"HapticFeedbackType.heavyImpact" isEqualToString:feedbackType]) {
256 [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy] impactOccurred];
257 }
else if ([
@"HapticFeedbackType.selectionClick" isEqualToString:feedbackType]) {
258 [[[UISelectionFeedbackGenerator alloc] init] selectionChanged];
262 - (void)setSystemChromePreferredOrientations:(NSArray*)orientations {
263 UIInterfaceOrientationMask mask = 0;
265 if (orientations.count == 0) {
266 mask |= UIInterfaceOrientationMaskAll;
268 for (NSString* orientation in orientations) {
269 if ([orientation isEqualToString:
@"DeviceOrientation.portraitUp"]) {
270 mask |= UIInterfaceOrientationMaskPortrait;
271 }
else if ([orientation isEqualToString:
@"DeviceOrientation.portraitDown"]) {
272 mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
273 }
else if ([orientation isEqualToString:
@"DeviceOrientation.landscapeLeft"]) {
274 mask |= UIInterfaceOrientationMaskLandscapeLeft;
275 }
else if ([orientation isEqualToString:
@"DeviceOrientation.landscapeRight"]) {
276 mask |= UIInterfaceOrientationMaskLandscapeRight;
284 [[NSNotificationCenter defaultCenter]
285 postNotificationName:@(kOrientationUpdateNotificationName)
287 userInfo:@{@(kOrientationUpdateNotificationKey) : @(mask)}];
290 - (void)setSystemChromeApplicationSwitcherDescription:(NSDictionary*)object {
294 - (void)setSystemChromeEnabledSystemUIOverlays:(NSArray*)overlays {
295 BOOL statusBarShouldBeHidden = ![overlays containsObject:@"SystemUiOverlay.top"];
296 if ([overlays containsObject:
@"SystemUiOverlay.bottom"]) {
297 [[NSNotificationCenter defaultCenter]
298 postNotificationName:FlutterViewControllerShowHomeIndicator
301 [[NSNotificationCenter defaultCenter]
302 postNotificationName:FlutterViewControllerHideHomeIndicator
305 if (
self.enableViewControllerBasedStatusBarAppearance) {
306 [
self.engine viewController].prefersStatusBarHidden = statusBarShouldBeHidden;
318 - (void)setSystemChromeEnabledSystemUIMode:(NSString*)mode {
319 BOOL edgeToEdge = [mode isEqualToString:@"SystemUiMode.edgeToEdge"];
320 if (
self.enableViewControllerBasedStatusBarAppearance) {
321 [
self.engine viewController].prefersStatusBarHidden = !edgeToEdge;
331 [[NSNotificationCenter defaultCenter]
332 postNotificationName:edgeToEdge ? FlutterViewControllerShowHomeIndicator
333 : FlutterViewControllerHideHomeIndicator
337 - (void)restoreSystemChromeSystemUIOverlays {
341 - (void)setSystemChromeSystemUIOverlayStyle:(NSDictionary*)message {
342 NSString* brightness = message[@"statusBarBrightness"];
343 if (brightness == (
id)[NSNull
null]) {
347 UIStatusBarStyle statusBarStyle;
348 if ([brightness isEqualToString:
@"Brightness.dark"]) {
349 statusBarStyle = UIStatusBarStyleLightContent;
350 }
else if ([brightness isEqualToString:
@"Brightness.light"]) {
351 if (@available(iOS 13, *)) {
352 statusBarStyle = UIStatusBarStyleDarkContent;
354 statusBarStyle = UIStatusBarStyleDefault;
360 if (
self.enableViewControllerBasedStatusBarAppearance) {
362 [[NSNotificationCenter defaultCenter]
363 postNotificationName:@(kOverlayStyleUpdateNotificationName)
365 userInfo:@{@(kOverlayStyleUpdateNotificationKey) : @(statusBarStyle)}];
371 - (void)popSystemNavigator:(BOOL)isAnimated {
379 UINavigationController* navigationController = [engineViewController navigationController];
380 if (navigationController) {
381 [navigationController popViewControllerAnimated:isAnimated];
383 UIViewController* rootViewController = nil;
384 #if APPLICATION_EXTENSION_API_ONLY
385 if (@available(iOS 15.0, *)) {
387 [engineViewController flutterWindowSceneIfViewLoaded].keyWindow.rootViewController;
390 <<
"rootViewController is not available in application extension prior to iOS 15.0.";
393 rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
395 if (engineViewController != rootViewController) {
396 [engineViewController dismissViewControllerAnimated:isAnimated completion:nil];
401 - (NSDictionary*)getClipboardData:(NSString*)format {
402 UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
404 NSString* stringInPasteboard = pasteboard.string;
406 return stringInPasteboard == nil ? nil : @{
@"text" : stringInPasteboard};
411 - (void)setClipboardData:(NSDictionary*)data {
412 UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
413 id copyText = data[@"text"];
414 if ([copyText isKindOfClass:[NSString
class]]) {
415 pasteboard.string = copyText;
417 pasteboard.string =
@"null";
421 - (NSDictionary*)clipboardHasStrings {
422 return @{
@"value" : @([UIPasteboard generalPasteboard].hasStrings)};
425 - (BOOL)isLiveTextInputAvailable {
426 return [[
self textField] canPerformAction:@selector(captureTextFromCamera:) withSender:nil];
429 - (void)showLookUpViewController:(NSString*)term {
430 UIViewController* engineViewController = [
self.engine viewController];
431 UIReferenceLibraryViewController* referenceLibraryViewController =
432 [[UIReferenceLibraryViewController alloc] initWithTerm:term];
433 [engineViewController presentViewController:referenceLibraryViewController
438 - (UITextField*)textField {
439 if (_textField == nil) {
440 _textField = [[UITextField alloc] init];