PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/front/plugins/cordova-plugin-statusbar/src/ios/CDVStatusBar.m

https://gitlab.com/boxnia/NFU_MOVIL
Objective C | 480 lines | 344 code | 99 blank | 37 comment | 51 complexity | d4cd6808c74e01265cfeb11719c91395 MD5 | raw file
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /*
  18. NOTE: plugman/cordova cli should have already installed this,
  19. but you need the value UIViewControllerBasedStatusBarAppearance
  20. in your Info.plist as well to set the styles in iOS 7
  21. */
  22. #import "CDVStatusBar.h"
  23. #import <objc/runtime.h>
  24. #import <Cordova/CDVViewController.h>
  25. static const void *kHideStatusBar = &kHideStatusBar;
  26. static const void *kStatusBarStyle = &kStatusBarStyle;
  27. @interface CDVViewController (StatusBar)
  28. @property (nonatomic, retain) id sb_hideStatusBar;
  29. @property (nonatomic, retain) id sb_statusBarStyle;
  30. @end
  31. @implementation CDVViewController (StatusBar)
  32. @dynamic sb_hideStatusBar;
  33. @dynamic sb_statusBarStyle;
  34. - (id)sb_hideStatusBar {
  35. return objc_getAssociatedObject(self, kHideStatusBar);
  36. }
  37. - (void)setSb_hideStatusBar:(id)newHideStatusBar {
  38. objc_setAssociatedObject(self, kHideStatusBar, newHideStatusBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  39. }
  40. - (id)sb_statusBarStyle {
  41. return objc_getAssociatedObject(self, kStatusBarStyle);
  42. }
  43. - (void)setSb_statusBarStyle:(id)newStatusBarStyle {
  44. objc_setAssociatedObject(self, kStatusBarStyle, newStatusBarStyle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  45. }
  46. - (BOOL) prefersStatusBarHidden {
  47. return [self.sb_hideStatusBar boolValue];
  48. }
  49. - (UIStatusBarStyle)preferredStatusBarStyle
  50. {
  51. return (UIStatusBarStyle)[self.sb_statusBarStyle intValue];
  52. }
  53. @end
  54. @interface CDVStatusBar () <UIScrollViewDelegate>
  55. - (void)fireTappedEvent;
  56. - (void)updateIsVisible:(BOOL)visible;
  57. @end
  58. @implementation CDVStatusBar
  59. - (id)settingForKey:(NSString*)key
  60. {
  61. return [self.commandDelegate.settings objectForKey:[key lowercaseString]];
  62. }
  63. - (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
  64. {
  65. if ([keyPath isEqual:@"statusBarHidden"]) {
  66. NSNumber* newValue = [change objectForKey:NSKeyValueChangeNewKey];
  67. [self updateIsVisible:![newValue boolValue]];
  68. }
  69. }
  70. -(void)statusBarDidChangeFrame:(NSNotification*)notification
  71. {
  72. //add a small delay for iOS 7 ( 0.1 seconds )
  73. __weak CDVStatusBar* weakSelf = self;
  74. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  75. [weakSelf resizeWebView];
  76. });
  77. }
  78. - (void)pluginInitialize
  79. {
  80. BOOL isiOS7 = (IsAtLeastiOSVersion(@"7.0"));
  81. // init
  82. NSNumber* uiviewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
  83. _uiviewControllerBasedStatusBarAppearance = (uiviewControllerBasedStatusBarAppearance == nil || [uiviewControllerBasedStatusBarAppearance boolValue]) && isiOS7;
  84. // observe the statusBarHidden property
  85. [[UIApplication sharedApplication] addObserver:self forKeyPath:@"statusBarHidden" options:NSKeyValueObservingOptionNew context:NULL];
  86. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarDidChangeFrame:) name: UIApplicationDidChangeStatusBarFrameNotification object:nil];
  87. _statusBarOverlaysWebView = YES; // default
  88. [self initializeStatusBarBackgroundView];
  89. self.viewController.view.autoresizesSubviews = YES;
  90. NSString* setting;
  91. setting = @"StatusBarBackgroundColor";
  92. if ([self settingForKey:setting]) {
  93. [self _backgroundColorByHexString:[self settingForKey:setting]];
  94. }
  95. setting = @"StatusBarStyle";
  96. if ([self settingForKey:setting]) {
  97. [self setStatusBarStyle:[self settingForKey:setting]];
  98. }
  99. // blank scroll view to intercept status bar taps
  100. self.webView.scrollView.scrollsToTop = NO;
  101. UIScrollView *fakeScrollView = [[UIScrollView alloc] initWithFrame:UIScreen.mainScreen.bounds];
  102. fakeScrollView.delegate = self;
  103. fakeScrollView.scrollsToTop = YES;
  104. [self.viewController.view addSubview:fakeScrollView]; // Add scrollview to the view heirarchy so that it will begin accepting status bar taps
  105. [self.viewController.view sendSubviewToBack:fakeScrollView]; // Send it to the very back of the view heirarchy
  106. fakeScrollView.contentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height * 2.0f); // Make the scroll view longer than the screen itself
  107. fakeScrollView.contentOffset = CGPointMake(0.0f, UIScreen.mainScreen.bounds.size.height); // Scroll down so a tap will take scroll view back to the top
  108. }
  109. - (void)onReset {
  110. _eventsCallbackId = nil;
  111. }
  112. - (void)fireTappedEvent {
  113. if (_eventsCallbackId == nil) {
  114. return;
  115. }
  116. NSDictionary* payload = @{@"type": @"tap"};
  117. CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:payload];
  118. [result setKeepCallbackAsBool:YES];
  119. [self.commandDelegate sendPluginResult:result callbackId:_eventsCallbackId];
  120. }
  121. - (void)updateIsVisible:(BOOL)visible {
  122. if (_eventsCallbackId == nil) {
  123. return;
  124. }
  125. CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:visible];
  126. [result setKeepCallbackAsBool:YES];
  127. [self.commandDelegate sendPluginResult:result callbackId:_eventsCallbackId];
  128. }
  129. - (void) _ready:(CDVInvokedUrlCommand*)command
  130. {
  131. _eventsCallbackId = command.callbackId;
  132. [self updateIsVisible:![UIApplication sharedApplication].statusBarHidden];
  133. NSString* setting = @"StatusBarOverlaysWebView";
  134. if ([self settingForKey:setting]) {
  135. self.statusBarOverlaysWebView = [(NSNumber*)[self settingForKey:setting] boolValue];
  136. if (self.statusBarOverlaysWebView) {
  137. [self resizeWebView];
  138. }
  139. }
  140. }
  141. - (void) initializeStatusBarBackgroundView
  142. {
  143. CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
  144. if ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown &&
  145. statusBarFrame.size.height + statusBarFrame.origin.y == [[UIScreen mainScreen] bounds].size.height) {
  146. // When started in upside-down orientation on iOS 7, status bar will be bound to lower edge of the
  147. // screen (statusBarFrame.origin.y will be somewhere around screen height). In this case we need to
  148. // correct frame's coordinates
  149. statusBarFrame.origin.y = 0;
  150. }
  151. statusBarFrame = [self invertFrameIfNeeded:statusBarFrame];
  152. _statusBarBackgroundView = [[UIView alloc] initWithFrame:statusBarFrame];
  153. _statusBarBackgroundView.backgroundColor = _statusBarBackgroundColor;
  154. _statusBarBackgroundView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin);
  155. _statusBarBackgroundView.autoresizesSubviews = YES;
  156. }
  157. - (CGRect) invertFrameIfNeeded:(CGRect)rect {
  158. // landscape is where (width > height). On iOS < 8, we need to invert since frames are
  159. // always in Portrait context
  160. if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && (rect.size.width < rect.size.height)) {
  161. CGFloat temp = rect.size.width;
  162. rect.size.width = rect.size.height;
  163. rect.size.height = temp;
  164. rect.origin = CGPointZero;
  165. }
  166. return rect;
  167. }
  168. - (void) setStatusBarOverlaysWebView:(BOOL)statusBarOverlaysWebView
  169. {
  170. // we only care about the latest iOS version or a change in setting
  171. if (!IsAtLeastiOSVersion(@"7.0") || statusBarOverlaysWebView == _statusBarOverlaysWebView) {
  172. return;
  173. }
  174. _statusBarOverlaysWebView = statusBarOverlaysWebView;
  175. [self resizeWebView];
  176. if (statusBarOverlaysWebView) {
  177. [_statusBarBackgroundView removeFromSuperview];
  178. } else {
  179. [self initializeStatusBarBackgroundView];
  180. [self.webView.superview addSubview:_statusBarBackgroundView];
  181. }
  182. }
  183. - (BOOL) statusBarOverlaysWebView
  184. {
  185. return _statusBarOverlaysWebView;
  186. }
  187. - (void) overlaysWebView:(CDVInvokedUrlCommand*)command
  188. {
  189. id value = [command argumentAtIndex:0];
  190. if (!([value isKindOfClass:[NSNumber class]])) {
  191. value = [NSNumber numberWithBool:YES];
  192. }
  193. self.statusBarOverlaysWebView = [value boolValue];
  194. }
  195. - (void) refreshStatusBarAppearance
  196. {
  197. SEL sel = NSSelectorFromString(@"setNeedsStatusBarAppearanceUpdate");
  198. if ([self.viewController respondsToSelector:sel]) {
  199. #pragma clang diagnostic push
  200. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  201. [self.viewController performSelector:sel withObject:nil];
  202. #pragma clang diagnostic pop
  203. }
  204. }
  205. - (void) setStyleForStatusBar:(UIStatusBarStyle)style
  206. {
  207. if (_uiviewControllerBasedStatusBarAppearance) {
  208. CDVViewController* vc = (CDVViewController*)self.viewController;
  209. vc.sb_statusBarStyle = [NSNumber numberWithInt:style];
  210. [self refreshStatusBarAppearance];
  211. } else {
  212. [[UIApplication sharedApplication] setStatusBarStyle:style];
  213. }
  214. }
  215. - (void) setStatusBarStyle:(NSString*)statusBarStyle
  216. {
  217. // default, lightContent, blackTranslucent, blackOpaque
  218. NSString* lcStatusBarStyle = [statusBarStyle lowercaseString];
  219. if ([lcStatusBarStyle isEqualToString:@"default"]) {
  220. [self styleDefault:nil];
  221. } else if ([lcStatusBarStyle isEqualToString:@"lightcontent"]) {
  222. [self styleLightContent:nil];
  223. } else if ([lcStatusBarStyle isEqualToString:@"blacktranslucent"]) {
  224. [self styleBlackTranslucent:nil];
  225. } else if ([lcStatusBarStyle isEqualToString:@"blackopaque"]) {
  226. [self styleBlackOpaque:nil];
  227. }
  228. }
  229. - (void) styleDefault:(CDVInvokedUrlCommand*)command
  230. {
  231. [self setStyleForStatusBar:UIStatusBarStyleDefault];
  232. }
  233. - (void) styleLightContent:(CDVInvokedUrlCommand*)command
  234. {
  235. [self setStyleForStatusBar:UIStatusBarStyleLightContent];
  236. }
  237. - (void) styleBlackTranslucent:(CDVInvokedUrlCommand*)command
  238. {
  239. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000
  240. # define TRANSLUCENT_STYLE UIStatusBarStyleBlackTranslucent
  241. #else
  242. # define TRANSLUCENT_STYLE UIStatusBarStyleLightContent
  243. #endif
  244. [self setStyleForStatusBar:TRANSLUCENT_STYLE];
  245. }
  246. - (void) styleBlackOpaque:(CDVInvokedUrlCommand*)command
  247. {
  248. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000
  249. # define OPAQUE_STYLE UIStatusBarStyleBlackOpaque
  250. #else
  251. # define OPAQUE_STYLE UIStatusBarStyleLightContent
  252. #endif
  253. [self setStyleForStatusBar:OPAQUE_STYLE];
  254. }
  255. - (void) backgroundColorByName:(CDVInvokedUrlCommand*)command
  256. {
  257. id value = [command argumentAtIndex:0];
  258. if (!([value isKindOfClass:[NSString class]])) {
  259. value = @"black";
  260. }
  261. SEL selector = NSSelectorFromString([value stringByAppendingString:@"Color"]);
  262. if ([UIColor respondsToSelector:selector]) {
  263. _statusBarBackgroundView.backgroundColor = [UIColor performSelector:selector];
  264. }
  265. }
  266. - (void) _backgroundColorByHexString:(NSString*)hexString
  267. {
  268. unsigned int rgbValue = 0;
  269. NSScanner* scanner = [NSScanner scannerWithString:hexString];
  270. [scanner setScanLocation:1];
  271. [scanner scanHexInt:&rgbValue];
  272. _statusBarBackgroundColor = [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  273. _statusBarBackgroundView.backgroundColor = _statusBarBackgroundColor;
  274. }
  275. - (void) backgroundColorByHexString:(CDVInvokedUrlCommand*)command
  276. {
  277. NSString* value = [command argumentAtIndex:0];
  278. if (!([value isKindOfClass:[NSString class]])) {
  279. value = @"#000000";
  280. }
  281. if (![value hasPrefix:@"#"] || [value length] < 7) {
  282. return;
  283. }
  284. [self _backgroundColorByHexString:value];
  285. }
  286. - (void) hideStatusBar
  287. {
  288. if (_uiviewControllerBasedStatusBarAppearance) {
  289. CDVViewController* vc = (CDVViewController*)self.viewController;
  290. vc.sb_hideStatusBar = [NSNumber numberWithBool:YES];
  291. [self refreshStatusBarAppearance];
  292. } else {
  293. UIApplication* app = [UIApplication sharedApplication];
  294. [app setStatusBarHidden:YES];
  295. }
  296. }
  297. - (void) hide:(CDVInvokedUrlCommand*)command
  298. {
  299. UIApplication* app = [UIApplication sharedApplication];
  300. if (!app.isStatusBarHidden)
  301. {
  302. [self hideStatusBar];
  303. if (IsAtLeastiOSVersion(@"7.0")) {
  304. [_statusBarBackgroundView removeFromSuperview];
  305. }
  306. [self resizeWebView];
  307. _statusBarBackgroundView.hidden = YES;
  308. }
  309. }
  310. - (void) showStatusBar
  311. {
  312. if (_uiviewControllerBasedStatusBarAppearance) {
  313. CDVViewController* vc = (CDVViewController*)self.viewController;
  314. vc.sb_hideStatusBar = [NSNumber numberWithBool:NO];
  315. [self refreshStatusBarAppearance];
  316. } else {
  317. UIApplication* app = [UIApplication sharedApplication];
  318. [app setStatusBarHidden:NO];
  319. }
  320. }
  321. - (void) show:(CDVInvokedUrlCommand*)command
  322. {
  323. UIApplication* app = [UIApplication sharedApplication];
  324. if (app.isStatusBarHidden)
  325. {
  326. BOOL isIOS7 = (IsAtLeastiOSVersion(@"7.0"));
  327. [self showStatusBar];
  328. [self resizeWebView];
  329. if (isIOS7) {
  330. if (!self.statusBarOverlaysWebView) {
  331. // there is a possibility that when the statusbar was hidden, it was in a different orientation
  332. // from the current one. Therefore we need to expand the statusBarBackgroundView as well to the
  333. // statusBar's current size
  334. CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
  335. statusBarFrame = [self invertFrameIfNeeded:statusBarFrame];
  336. CGRect sbBgFrame = _statusBarBackgroundView.frame;
  337. sbBgFrame.size = statusBarFrame.size;
  338. _statusBarBackgroundView.frame = sbBgFrame;
  339. [self.webView.superview addSubview:_statusBarBackgroundView];
  340. }
  341. }
  342. _statusBarBackgroundView.hidden = NO;
  343. }
  344. }
  345. -(void)resizeWebView
  346. {
  347. BOOL isIOS7 = (IsAtLeastiOSVersion(@"7.0"));
  348. if (isIOS7) {
  349. CGRect bounds = [[UIScreen mainScreen] bounds];
  350. bounds = [self invertFrameIfNeeded:bounds];
  351. if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
  352. self.viewController.view.frame = bounds;
  353. }
  354. self.webView.frame = bounds;
  355. if (!self.statusBarOverlaysWebView) {
  356. CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
  357. statusBarFrame = [self invertFrameIfNeeded:statusBarFrame];
  358. CGRect frame = self.webView.frame;
  359. frame.origin.y = statusBarFrame.size.height;
  360. frame.size.height -= statusBarFrame.size.height;
  361. self.webView.frame = frame;
  362. }
  363. } else {
  364. CGRect bounds = [[UIScreen mainScreen] applicationFrame];
  365. self.viewController.view.frame = bounds;
  366. }
  367. }
  368. - (void) dealloc
  369. {
  370. [[UIApplication sharedApplication] removeObserver:self forKeyPath:@"statusBarHidden"];
  371. [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  372. }
  373. #pragma mark - UIScrollViewDelegate
  374. - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
  375. {
  376. [self fireTappedEvent];
  377. return NO;
  378. }
  379. @end