Flutter Engine
FlutterPlatformPlugin.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
6 
7 #import <AudioToolbox/AudioToolbox.h>
8 #import <Foundation/Foundation.h>
9 #import <UIKit/UIApplication.h>
10 #import <UIKit/UIKit.h>
11 
12 #include "flutter/fml/logging.h"
13 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
14 
15 namespace {
16 
17 constexpr char kTextPlainFormat[] = "text/plain";
18 const UInt32 kKeyPressClickSoundId = 1306;
19 
20 } // namespaces
21 
22 namespace flutter {
23 
24 // TODO(abarth): Move these definitions from system_chrome_impl.cc to here.
26  "io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
28  "io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
30  "io.flutter.plugin.platform.SystemChromeOverlayNotificationName";
32  "io.flutter.plugin.platform.SystemChromeOverlayNotificationKey";
33 
34 } // namespace flutter
35 
36 using namespace flutter;
37 
38 @implementation FlutterPlatformPlugin {
40 }
41 
42 - (instancetype)init {
43  @throw([NSException exceptionWithName:@"FlutterPlatformPlugin must initWithEngine"
44  reason:nil
45  userInfo:nil]);
46 }
47 
48 - (instancetype)initWithEngine:(fml::WeakPtr<FlutterEngine>)engine {
49  FML_DCHECK(engine) << "engine must be set";
50  self = [super init];
51 
52  if (self) {
53  _engine = engine;
54  }
55 
56  return self;
57 }
58 
59 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
60  NSString* method = call.method;
61  id args = call.arguments;
62  if ([method isEqualToString:@"SystemSound.play"]) {
63  [self playSystemSound:args];
64  result(nil);
65  } else if ([method isEqualToString:@"HapticFeedback.vibrate"]) {
66  [self vibrateHapticFeedback:args];
67  result(nil);
68  } else if ([method isEqualToString:@"SystemChrome.setPreferredOrientations"]) {
69  [self setSystemChromePreferredOrientations:args];
70  result(nil);
71  } else if ([method isEqualToString:@"SystemChrome.setApplicationSwitcherDescription"]) {
72  [self setSystemChromeApplicationSwitcherDescription:args];
73  result(nil);
74  } else if ([method isEqualToString:@"SystemChrome.setEnabledSystemUIOverlays"]) {
75  [self setSystemChromeEnabledSystemUIOverlays:args];
76  result(nil);
77  } else if ([method isEqualToString:@"SystemChrome.restoreSystemUIOverlays"]) {
78  [self restoreSystemChromeSystemUIOverlays];
79  result(nil);
80  } else if ([method isEqualToString:@"SystemChrome.setSystemUIOverlayStyle"]) {
81  [self setSystemChromeSystemUIOverlayStyle:args];
82  result(nil);
83  } else if ([method isEqualToString:@"SystemNavigator.pop"]) {
84  NSNumber* isAnimated = args;
85  [self popSystemNavigator:isAnimated.boolValue];
86  result(nil);
87  } else if ([method isEqualToString:@"Clipboard.getData"]) {
88  result([self getClipboardData:args]);
89  } else if ([method isEqualToString:@"Clipboard.setData"]) {
90  [self setClipboardData:args];
91  result(nil);
92  } else if ([method isEqualToString:@"Clipboard.hasStrings"]) {
93  result([self clipboardHasStrings]);
94  } else {
96  }
97 }
98 
99 - (void)playSystemSound:(NSString*)soundType {
100  if ([soundType isEqualToString:@"SystemSoundType.click"]) {
101  // All feedback types are specific to Android and are treated as equal on
102  // iOS.
103  AudioServicesPlaySystemSound(kKeyPressClickSoundId);
104  }
105 }
106 
107 - (void)vibrateHapticFeedback:(NSString*)feedbackType {
108  if (!feedbackType) {
109  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
110  return;
111  }
112 
113  if (@available(iOS 10, *)) {
114  if ([@"HapticFeedbackType.lightImpact" isEqualToString:feedbackType]) {
115  [[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight] autorelease]
116  impactOccurred];
117  } else if ([@"HapticFeedbackType.mediumImpact" isEqualToString:feedbackType]) {
118  [[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] autorelease]
119  impactOccurred];
120  } else if ([@"HapticFeedbackType.heavyImpact" isEqualToString:feedbackType]) {
121  [[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy] autorelease]
122  impactOccurred];
123  } else if ([@"HapticFeedbackType.selectionClick" isEqualToString:feedbackType]) {
124  [[[[UISelectionFeedbackGenerator alloc] init] autorelease] selectionChanged];
125  }
126  }
127 }
128 
129 - (void)setSystemChromePreferredOrientations:(NSArray*)orientations {
130  UIInterfaceOrientationMask mask = 0;
131 
132  if (orientations.count == 0) {
133  mask |= UIInterfaceOrientationMaskAll;
134  } else {
135  for (NSString* orientation in orientations) {
136  if ([orientation isEqualToString:@"DeviceOrientation.portraitUp"])
137  mask |= UIInterfaceOrientationMaskPortrait;
138  else if ([orientation isEqualToString:@"DeviceOrientation.portraitDown"])
139  mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
140  else if ([orientation isEqualToString:@"DeviceOrientation.landscapeLeft"])
141  mask |= UIInterfaceOrientationMaskLandscapeLeft;
142  else if ([orientation isEqualToString:@"DeviceOrientation.landscapeRight"])
143  mask |= UIInterfaceOrientationMaskLandscapeRight;
144  }
145  }
146 
147  if (!mask)
148  return;
149  [[NSNotificationCenter defaultCenter]
150  postNotificationName:@(kOrientationUpdateNotificationName)
151  object:nil
152  userInfo:@{@(kOrientationUpdateNotificationKey) : @(mask)}];
153 }
154 
155 - (void)setSystemChromeApplicationSwitcherDescription:(NSDictionary*)object {
156  // No counterpart on iOS but is a benign operation. So no asserts.
157 }
158 
159 - (void)setSystemChromeEnabledSystemUIOverlays:(NSArray*)overlays {
160  // Checks if the top status bar should be visible. This platform ignores all
161  // other overlays
162 
163  // We opt out of view controller based status bar visibility since we want
164  // to be able to modify this on the fly. The key used is
165  // UIViewControllerBasedStatusBarAppearance
166  [UIApplication sharedApplication].statusBarHidden =
167  ![overlays containsObject:@"SystemUiOverlay.top"];
168  if ([overlays containsObject:@"SystemUiOverlay.bottom"]) {
169  [[NSNotificationCenter defaultCenter]
170  postNotificationName:FlutterViewControllerShowHomeIndicator
171  object:nil];
172  } else {
173  [[NSNotificationCenter defaultCenter]
174  postNotificationName:FlutterViewControllerHideHomeIndicator
175  object:nil];
176  }
177 }
178 
179 - (void)restoreSystemChromeSystemUIOverlays {
180  // Nothing to do on iOS.
181 }
182 
183 - (void)setSystemChromeSystemUIOverlayStyle:(NSDictionary*)message {
184  NSString* brightness = message[@"statusBarBrightness"];
185  if (brightness == (id)[NSNull null])
186  return;
187 
188  UIStatusBarStyle statusBarStyle;
189  if ([brightness isEqualToString:@"Brightness.dark"]) {
190  statusBarStyle = UIStatusBarStyleLightContent;
191  } else if ([brightness isEqualToString:@"Brightness.light"]) {
192  if (@available(iOS 13, *)) {
193  statusBarStyle = UIStatusBarStyleDarkContent;
194  } else {
195  statusBarStyle = UIStatusBarStyleDefault;
196  }
197  } else {
198  return;
199  }
200 
201  NSNumber* infoValue = [[NSBundle mainBundle]
202  objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
203  Boolean delegateToViewController = (infoValue == nil || [infoValue boolValue]);
204 
205  if (delegateToViewController) {
206  // This notification is respected by the iOS embedder
207  [[NSNotificationCenter defaultCenter]
208  postNotificationName:@(kOverlayStyleUpdateNotificationName)
209  object:nil
210  userInfo:@{@(kOverlayStyleUpdateNotificationKey) : @(statusBarStyle)}];
211  } else {
212  // Note: -[UIApplication setStatusBarStyle] is deprecated in iOS9
213  // in favor of delegating to the view controller
214  [[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle];
215  }
216 }
217 
218 - (void)popSystemNavigator:(BOOL)isAnimated {
219  // Apple's human user guidelines say not to terminate iOS applications. However, if the
220  // root view of the app is a navigation controller, it is instructed to back up a level
221  // in the navigation hierarchy.
222  // It's also possible in an Add2App scenario that the FlutterViewController was presented
223  // outside the context of a UINavigationController, and still wants to be popped.
224  UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
225  if ([viewController isKindOfClass:[UINavigationController class]]) {
226  [((UINavigationController*)viewController) popViewControllerAnimated:isAnimated];
227  } else {
228  auto engineViewController = static_cast<UIViewController*>([_engine.get() viewController]);
229  if (engineViewController != viewController) {
230  [engineViewController dismissViewControllerAnimated:isAnimated completion:nil];
231  }
232  }
233 }
234 
235 - (NSDictionary*)getClipboardData:(NSString*)format {
236  UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
237  if (!format || [format isEqualToString:@(kTextPlainFormat)]) {
238  NSString* stringInPasteboard = pasteboard.string;
239  // The pasteboard may contain an item but it may not be a string (an image for instance).
240  return stringInPasteboard == nil ? nil : @{@"text" : stringInPasteboard};
241  }
242  return nil;
243 }
244 
245 - (void)setClipboardData:(NSDictionary*)data {
246  UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
247  if (data[@"text"]) {
248  pasteboard.string = data[@"text"];
249  } else {
250  pasteboard.string = @"null";
251  }
252 }
253 
254 - (NSDictionary*)clipboardHasStrings {
255  bool hasStrings = false;
256  UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
257  if (@available(iOS 10, *)) {
258  hasStrings = pasteboard.hasStrings;
259  } else {
260  NSString* stringInPasteboard = pasteboard.string;
261  hasStrings = stringInPasteboard != nil;
262  }
263  return @{@"value" : @(hasStrings)};
264 }
265 
266 @end
G_BEGIN_DECLS FlValue * args
#define FML_DCHECK(condition)
Definition: logging.h:86
const char *const kOrientationUpdateNotificationKey
const char *const kOverlayStyleUpdateNotificationKey
const char *const kOrientationUpdateNotificationName
fml::scoped_nsobject< FlutterEngine > _engine
const char *const kOverlayStyleUpdateNotificationName
void(^ FlutterResult)(id _Nullable result)
FLUTTER_EXPORT NSObject const * FlutterMethodNotImplemented
static constexpr char kTextPlainFormat[]