PageRenderTime 134ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/cordova-plugin-splashscreen/src/ios/CDVSplashScreen.m

https://gitlab.com/blocknotary/IonicInterviews
Objective C | 506 lines | 369 code | 70 blank | 67 comment | 107 complexity | f21c99e0b2166cc481766ab53f1430f7 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. #import "CDVSplashScreen.h"
  18. #import <Cordova/CDVViewController.h>
  19. #import <Cordova/CDVScreenOrientationDelegate.h>
  20. #import "CDVViewController+SplashScreen.h"
  21. #define kSplashScreenDurationDefault 3000.0f
  22. #define kFadeDurationDefault 500.0f
  23. @implementation CDVSplashScreen
  24. - (void)pluginInitialize
  25. {
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageDidLoad) name:CDVPageDidLoadNotification object:nil];
  27. [self setVisible:YES];
  28. }
  29. - (void)show:(CDVInvokedUrlCommand*)command
  30. {
  31. [self setVisible:YES];
  32. }
  33. - (void)hide:(CDVInvokedUrlCommand*)command
  34. {
  35. [self setVisible:NO andForce:YES];
  36. }
  37. - (void)pageDidLoad
  38. {
  39. id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"AutoHideSplashScreen" lowercaseString]];
  40. // if value is missing, default to yes
  41. if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
  42. [self setVisible:NO];
  43. }
  44. }
  45. - (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
  46. {
  47. [self updateImage];
  48. }
  49. - (void)createViews
  50. {
  51. /*
  52. * The Activity View is the top spinning throbber in the status/battery bar. We init it with the default Grey Style.
  53. *
  54. * whiteLarge = UIActivityIndicatorViewStyleWhiteLarge
  55. * white = UIActivityIndicatorViewStyleWhite
  56. * gray = UIActivityIndicatorViewStyleGray
  57. *
  58. */
  59. // Determine whether rotation should be enabled for this device
  60. // Per iOS HIG, landscape is only supported on iPad and iPhone 6+
  61. CDV_iOSDevice device = [self getCurrentDevice];
  62. BOOL autorotateValue = (device.iPad || device.iPhone6Plus) ?
  63. [(CDVViewController *)self.viewController shouldAutorotateDefaultValue] :
  64. NO;
  65. [(CDVViewController *)self.viewController setEnabledAutorotation:autorotateValue];
  66. NSString* topActivityIndicator = [self.commandDelegate.settings objectForKey:[@"TopActivityIndicator" lowercaseString]];
  67. UIActivityIndicatorViewStyle topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
  68. if ([topActivityIndicator isEqualToString:@"whiteLarge"])
  69. {
  70. topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
  71. }
  72. else if ([topActivityIndicator isEqualToString:@"white"])
  73. {
  74. topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
  75. }
  76. else if ([topActivityIndicator isEqualToString:@"gray"])
  77. {
  78. topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
  79. }
  80. UIView* parentView = self.viewController.view;
  81. parentView.userInteractionEnabled = NO; // disable user interaction while splashscreen is shown
  82. _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
  83. _activityView.center = CGPointMake(parentView.bounds.size.width / 2, parentView.bounds.size.height / 2);
  84. _activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin
  85. | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
  86. [_activityView startAnimating];
  87. // Set the frame & image later.
  88. _imageView = [[UIImageView alloc] init];
  89. [parentView addSubview:_imageView];
  90. id showSplashScreenSpinnerValue = [self.commandDelegate.settings objectForKey:[@"ShowSplashScreenSpinner" lowercaseString]];
  91. // backwards compatibility - if key is missing, default to true
  92. if ((showSplashScreenSpinnerValue == nil) || [showSplashScreenSpinnerValue boolValue])
  93. {
  94. [parentView addSubview:_activityView];
  95. }
  96. // Frame is required when launching in portrait mode.
  97. // Bounds for landscape since it captures the rotation.
  98. [parentView addObserver:self forKeyPath:@"frame" options:0 context:nil];
  99. [parentView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
  100. [self updateImage];
  101. _destroyed = NO;
  102. }
  103. - (void)hideViews
  104. {
  105. [_imageView setAlpha:0];
  106. [_activityView setAlpha:0];
  107. }
  108. - (void)destroyViews
  109. {
  110. _destroyed = YES;
  111. [(CDVViewController *)self.viewController setEnabledAutorotation:[(CDVViewController *)self.viewController shouldAutorotateDefaultValue]];
  112. [_imageView removeFromSuperview];
  113. [_activityView removeFromSuperview];
  114. _imageView = nil;
  115. _activityView = nil;
  116. _curImageName = nil;
  117. self.viewController.view.userInteractionEnabled = YES; // re-enable user interaction upon completion
  118. @try {
  119. [self.viewController.view removeObserver:self forKeyPath:@"frame"];
  120. [self.viewController.view removeObserver:self forKeyPath:@"bounds"];
  121. }
  122. @catch (NSException *exception) {
  123. // When reloading the page from a remotely connected Safari, there
  124. // are no observers, so the removeObserver method throws an exception,
  125. // that we can safely ignore.
  126. // Alternatively we can check whether there are observers before calling removeObserver
  127. }
  128. }
  129. - (CDV_iOSDevice) getCurrentDevice
  130. {
  131. CDV_iOSDevice device;
  132. UIScreen* mainScreen = [UIScreen mainScreen];
  133. CGFloat mainScreenHeight = mainScreen.bounds.size.height;
  134. CGFloat mainScreenWidth = mainScreen.bounds.size.width;
  135. int limit = MAX(mainScreenHeight,mainScreenWidth);
  136. device.iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
  137. device.iPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
  138. device.retina = ([mainScreen scale] == 2.0);
  139. device.iPhone4 = (device.iPhone && limit == 480.0);
  140. device.iPhone5 = (device.iPhone && limit == 568.0);
  141. // note these below is not a true device detect, for example if you are on an
  142. // iPhone 6/6+ but the app is scaled it will prob set iPhone5 as true, but
  143. // this is appropriate for detecting the runtime screen environment
  144. device.iPhone6 = (device.iPhone && limit == 667.0);
  145. device.iPhone6Plus = (device.iPhone && limit == 736.0);
  146. return device;
  147. }
  148. - (BOOL) isUsingCDVLaunchScreen {
  149. NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  150. if (launchStoryboardName) {
  151. return ([launchStoryboardName isEqualToString:@"CDVLaunchScreen"]);
  152. } else {
  153. return NO;
  154. }
  155. }
  156. - (NSString*)getImageName:(UIInterfaceOrientation)currentOrientation delegate:(id<CDVScreenOrientationDelegate>)orientationDelegate device:(CDV_iOSDevice)device
  157. {
  158. // Use UILaunchImageFile if specified in plist. Otherwise, use Default.
  159. NSString* imageName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchImageFile"];
  160. // detect if we are using CB-9762 Launch Storyboard; if so, return the associated image instead
  161. if ([self isUsingCDVLaunchScreen]) {
  162. imageName = @"LaunchStoryboard";
  163. return imageName;
  164. }
  165. NSUInteger supportedOrientations = [orientationDelegate supportedInterfaceOrientations];
  166. // Checks to see if the developer has locked the orientation to use only one of Portrait or Landscape
  167. BOOL supportsLandscape = (supportedOrientations & UIInterfaceOrientationMaskLandscape);
  168. BOOL supportsPortrait = (supportedOrientations & UIInterfaceOrientationMaskPortrait || supportedOrientations & UIInterfaceOrientationMaskPortraitUpsideDown);
  169. // this means there are no mixed orientations in there
  170. BOOL isOrientationLocked = !(supportsPortrait && supportsLandscape);
  171. if (imageName)
  172. {
  173. imageName = [imageName stringByDeletingPathExtension];
  174. }
  175. else
  176. {
  177. imageName = @"Default";
  178. }
  179. // Add Asset Catalog specific prefixes
  180. if ([imageName isEqualToString:@"LaunchImage"])
  181. {
  182. if (device.iPhone4 || device.iPhone5 || device.iPad) {
  183. imageName = [imageName stringByAppendingString:@"-700"];
  184. } else if(device.iPhone6) {
  185. imageName = [imageName stringByAppendingString:@"-800"];
  186. } else if(device.iPhone6Plus) {
  187. imageName = [imageName stringByAppendingString:@"-800"];
  188. if (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown)
  189. {
  190. imageName = [imageName stringByAppendingString:@"-Portrait"];
  191. }
  192. }
  193. }
  194. if (device.iPhone5)
  195. { // does not support landscape
  196. imageName = [imageName stringByAppendingString:@"-568h"];
  197. }
  198. else if (device.iPhone6)
  199. { // does not support landscape
  200. imageName = [imageName stringByAppendingString:@"-667h"];
  201. }
  202. else if (device.iPhone6Plus)
  203. { // supports landscape
  204. if (isOrientationLocked)
  205. {
  206. imageName = [imageName stringByAppendingString:(supportsLandscape ? @"-Landscape" : @"")];
  207. }
  208. else
  209. {
  210. switch (currentOrientation)
  211. {
  212. case UIInterfaceOrientationLandscapeLeft:
  213. case UIInterfaceOrientationLandscapeRight:
  214. imageName = [imageName stringByAppendingString:@"-Landscape"];
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. imageName = [imageName stringByAppendingString:@"-736h"];
  221. }
  222. else if (device.iPad)
  223. { // supports landscape
  224. if (isOrientationLocked)
  225. {
  226. imageName = [imageName stringByAppendingString:(supportsLandscape ? @"-Landscape" : @"-Portrait")];
  227. }
  228. else
  229. {
  230. switch (currentOrientation)
  231. {
  232. case UIInterfaceOrientationLandscapeLeft:
  233. case UIInterfaceOrientationLandscapeRight:
  234. imageName = [imageName stringByAppendingString:@"-Landscape"];
  235. break;
  236. case UIInterfaceOrientationPortrait:
  237. case UIInterfaceOrientationPortraitUpsideDown:
  238. default:
  239. imageName = [imageName stringByAppendingString:@"-Portrait"];
  240. break;
  241. }
  242. }
  243. }
  244. return imageName;
  245. }
  246. - (UIInterfaceOrientation)getCurrentOrientation
  247. {
  248. UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
  249. UIDeviceOrientation dOrientation = [UIDevice currentDevice].orientation;
  250. bool landscape;
  251. if (dOrientation == UIDeviceOrientationUnknown || dOrientation == UIDeviceOrientationFaceUp || dOrientation == UIDeviceOrientationFaceDown) {
  252. // If the device is laying down, use the UIInterfaceOrientation based on the status bar.
  253. landscape = UIInterfaceOrientationIsLandscape(iOrientation);
  254. } else {
  255. // If the device is not laying down, use UIDeviceOrientation.
  256. landscape = UIDeviceOrientationIsLandscape(dOrientation);
  257. // There's a bug in iOS!!!! http://openradar.appspot.com/7216046
  258. // So values needs to be reversed for landscape!
  259. if (dOrientation == UIDeviceOrientationLandscapeLeft)
  260. {
  261. iOrientation = UIInterfaceOrientationLandscapeRight;
  262. }
  263. else if (dOrientation == UIDeviceOrientationLandscapeRight)
  264. {
  265. iOrientation = UIInterfaceOrientationLandscapeLeft;
  266. }
  267. else if (dOrientation == UIDeviceOrientationPortrait)
  268. {
  269. iOrientation = UIInterfaceOrientationPortrait;
  270. }
  271. else if (dOrientation == UIDeviceOrientationPortraitUpsideDown)
  272. {
  273. iOrientation = UIInterfaceOrientationPortraitUpsideDown;
  274. }
  275. }
  276. return iOrientation;
  277. }
  278. // Sets the view's frame and image.
  279. - (void)updateImage
  280. {
  281. NSString* imageName = [self getImageName:[self getCurrentOrientation] delegate:(id<CDVScreenOrientationDelegate>)self.viewController device:[self getCurrentDevice]];
  282. if (![imageName isEqualToString:_curImageName])
  283. {
  284. UIImage* img = [UIImage imageNamed:imageName];
  285. _imageView.image = img;
  286. _curImageName = imageName;
  287. }
  288. // Check that splash screen's image exists before updating bounds
  289. if (_imageView.image)
  290. {
  291. [self updateBounds];
  292. }
  293. else
  294. {
  295. NSLog(@"WARNING: The splashscreen image named %@ was not found", imageName);
  296. }
  297. }
  298. - (void)updateBounds
  299. {
  300. if ([self isUsingCDVLaunchScreen]) {
  301. // CB-9762's launch screen expects the image to fill the screen and be scaled using AspectFill.
  302. CGSize viewportSize = [UIApplication sharedApplication].delegate.window.bounds.size;
  303. _imageView.frame = CGRectMake(0, 0, viewportSize.width, viewportSize.height);
  304. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  305. return;
  306. }
  307. UIImage* img = _imageView.image;
  308. CGRect imgBounds = (img) ? CGRectMake(0, 0, img.size.width, img.size.height) : CGRectZero;
  309. CGSize screenSize = [self.viewController.view convertRect:[UIScreen mainScreen].bounds fromView:nil].size;
  310. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  311. CGAffineTransform imgTransform = CGAffineTransformIdentity;
  312. /* If and only if an iPhone application is landscape-only as per
  313. * UISupportedInterfaceOrientations, the view controller's orientation is
  314. * landscape. In this case the image must be rotated in order to appear
  315. * correctly.
  316. */
  317. CDV_iOSDevice device = [self getCurrentDevice];
  318. if (UIInterfaceOrientationIsLandscape(orientation) && !device.iPhone6Plus && !device.iPad)
  319. {
  320. imgTransform = CGAffineTransformMakeRotation(M_PI / 2);
  321. imgBounds.size = CGSizeMake(imgBounds.size.height, imgBounds.size.width);
  322. }
  323. // There's a special case when the image is the size of the screen.
  324. if (CGSizeEqualToSize(screenSize, imgBounds.size))
  325. {
  326. CGRect statusFrame = [self.viewController.view convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
  327. if (!(IsAtLeastiOSVersion(@"7.0")))
  328. {
  329. imgBounds.origin.y -= statusFrame.size.height;
  330. }
  331. }
  332. else if (imgBounds.size.width > 0)
  333. {
  334. CGRect viewBounds = self.viewController.view.bounds;
  335. CGFloat imgAspect = imgBounds.size.width / imgBounds.size.height;
  336. CGFloat viewAspect = viewBounds.size.width / viewBounds.size.height;
  337. // This matches the behaviour of the native splash screen.
  338. CGFloat ratio;
  339. if (viewAspect > imgAspect)
  340. {
  341. ratio = viewBounds.size.width / imgBounds.size.width;
  342. }
  343. else
  344. {
  345. ratio = viewBounds.size.height / imgBounds.size.height;
  346. }
  347. imgBounds.size.height *= ratio;
  348. imgBounds.size.width *= ratio;
  349. }
  350. _imageView.transform = imgTransform;
  351. _imageView.frame = imgBounds;
  352. }
  353. - (void)setVisible:(BOOL)visible
  354. {
  355. [self setVisible:visible andForce:NO];
  356. }
  357. - (void)setVisible:(BOOL)visible andForce:(BOOL)force
  358. {
  359. if (visible != _visible || force)
  360. {
  361. _visible = visible;
  362. id fadeSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"FadeSplashScreen" lowercaseString]];
  363. id fadeSplashScreenDuration = [self.commandDelegate.settings objectForKey:[@"FadeSplashScreenDuration" lowercaseString]];
  364. float fadeDuration = fadeSplashScreenDuration == nil ? kFadeDurationDefault : [fadeSplashScreenDuration floatValue];
  365. id splashDurationString = [self.commandDelegate.settings objectForKey: [@"SplashScreenDelay" lowercaseString]];
  366. float splashDuration = splashDurationString == nil ? kSplashScreenDurationDefault : [splashDurationString floatValue];
  367. id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"AutoHideSplashScreen" lowercaseString]];
  368. BOOL autoHideSplashScreen = true;
  369. if (autoHideSplashScreenValue != nil) {
  370. autoHideSplashScreen = [autoHideSplashScreenValue boolValue];
  371. }
  372. if (!autoHideSplashScreen) {
  373. // CB-10412 SplashScreenDelay does not make sense if the splashscreen is hidden manually
  374. splashDuration = 0;
  375. }
  376. if (fadeSplashScreenValue == nil)
  377. {
  378. fadeSplashScreenValue = @"true";
  379. }
  380. if (![fadeSplashScreenValue boolValue])
  381. {
  382. fadeDuration = 0;
  383. }
  384. else if (fadeDuration < 30)
  385. {
  386. // [CB-9750] This value used to be in decimal seconds, so we will assume that if someone specifies 10
  387. // they mean 10 seconds, and not the meaningless 10ms
  388. fadeDuration *= 1000;
  389. }
  390. if (_visible)
  391. {
  392. if (_imageView == nil)
  393. {
  394. [self createViews];
  395. }
  396. }
  397. else if (fadeDuration == 0 && splashDuration == 0)
  398. {
  399. [self destroyViews];
  400. }
  401. else
  402. {
  403. __weak __typeof(self) weakSelf = self;
  404. float effectiveSplashDuration;
  405. // [CB-10562] AutoHideSplashScreen may be "true" but we should still be able to hide the splashscreen manually.
  406. if (!autoHideSplashScreen || force) {
  407. effectiveSplashDuration = (fadeDuration) / 1000;
  408. } else {
  409. effectiveSplashDuration = (splashDuration - fadeDuration) / 1000;
  410. }
  411. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (uint64_t) effectiveSplashDuration * NSEC_PER_SEC), dispatch_get_main_queue(), CFBridgingRelease(CFBridgingRetain(^(void) {
  412. if (!_destroyed) {
  413. [UIView transitionWithView:self.viewController.view
  414. duration:(fadeDuration / 1000)
  415. options:UIViewAnimationOptionTransitionNone
  416. animations:^(void) {
  417. [weakSelf hideViews];
  418. }
  419. completion:^(BOOL finished) {
  420. // Always destroy views, otherwise you could have an
  421. // invisible splashscreen that is overlayed over your active views
  422. // which causes that no touch events are passed
  423. if (!_destroyed) {
  424. [weakSelf destroyViews];
  425. // TODO: It might also be nice to have a js event happen here -jm
  426. }
  427. }
  428. ];
  429. }
  430. })));
  431. }
  432. }
  433. }
  434. @end