|
| 1 | +// |
| 2 | +// LaunchScreenStoryboard.m |
| 3 | +// |
| 4 | +// Created by Todd Bluhm on 11/1/16. |
| 5 | +// |
| 6 | + |
| 7 | +#import "LaunchScreenStoryboard.h" |
| 8 | +#import <Cordova/CDVViewController.h> |
| 9 | + |
| 10 | +#define kDefaultStoryboardName @"LaunchScreen" |
| 11 | +#define kDefaultFadeOut YES |
| 12 | +#define kDefaultFadeOutDuration 0.5f |
| 13 | + |
| 14 | +@implementation LaunchScreenStoryboard |
| 15 | + |
| 16 | +- (id)settingForKey:(NSString*)key |
| 17 | +{ |
| 18 | + return [self.commandDelegate.settings objectForKey:[key lowercaseString]]; |
| 19 | +} |
| 20 | + |
| 21 | +- (void)pluginInitialize |
| 22 | +{ |
| 23 | + // Get all the plugin settings |
| 24 | + _storyboardName = [self settingForKey:@"StoryboardName"]; |
| 25 | + if (_storyboardName == nil) { |
| 26 | + _storyboardName = kDefaultStoryboardName; |
| 27 | + } |
| 28 | + |
| 29 | + NSString* fadeOut = [self settingForKey:@"FadeOut"]; |
| 30 | + if (fadeOut == nil) { |
| 31 | + _performFadeOut = YES; |
| 32 | + } else { |
| 33 | + _performFadeOut = [fadeOut boolValue]; |
| 34 | + } |
| 35 | + |
| 36 | + NSString* fadeOutDuration = [self settingForKey:@"FadeOutDuration"]; |
| 37 | + if (fadeOutDuration == nil) { |
| 38 | + _fadeOutDuration = kDefaultFadeOutDuration; |
| 39 | + } else { |
| 40 | + _fadeOutDuration = [fadeOutDuration floatValue]; |
| 41 | + } |
| 42 | + |
| 43 | + UIStoryboard* sb = [UIStoryboard storyboardWithName:_storyboardName |
| 44 | + bundle:nil]; |
| 45 | + _launchScreenViewController = [sb instantiateInitialViewController]; |
| 46 | + _launchScreenStartAlpha = _launchScreenViewController.view.alpha; |
| 47 | + |
| 48 | + [self show:nil]; |
| 49 | +} |
| 50 | + |
| 51 | +- (void)show:(CDVInvokedUrlCommand*)command |
| 52 | +{ |
| 53 | + _launchScreenViewController.view.alpha = _launchScreenStartAlpha; |
| 54 | + [self.viewController addChildViewController:_launchScreenViewController]; |
| 55 | + _launchScreenViewController.view.frame = self.viewController.view.frame; |
| 56 | + [self.viewController.view addSubview:_launchScreenViewController.view]; |
| 57 | + [_launchScreenViewController didMoveToParentViewController:self.viewController]; |
| 58 | + |
| 59 | + [self returnSuccess: command]; |
| 60 | +} |
| 61 | + |
| 62 | +- (void)hide:(CDVInvokedUrlCommand*)command |
| 63 | +{ |
| 64 | + if (_performFadeOut) { |
| 65 | + [UIView animateWithDuration:_fadeOutDuration |
| 66 | + delay:0 |
| 67 | + options:UIViewAnimationOptionCurveEaseOut |
| 68 | + animations:^{ |
| 69 | + _launchScreenViewController.view.alpha = 0.0; |
| 70 | + } |
| 71 | + completion:^(BOOL finished) { |
| 72 | + [self removeLaunchScreen]; |
| 73 | + [self returnSuccess: command]; |
| 74 | + }]; |
| 75 | + } else { |
| 76 | + [self removeLaunchScreen]; |
| 77 | + [self returnSuccess: command]; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +- (void)removeLaunchScreen |
| 82 | +{ |
| 83 | + [_launchScreenViewController willMoveToParentViewController:nil]; |
| 84 | + [_launchScreenViewController.view removeFromSuperview]; |
| 85 | + [_launchScreenViewController removeFromParentViewController]; |
| 86 | +} |
| 87 | + |
| 88 | +- (void)returnSuccess:(CDVInvokedUrlCommand*)command |
| 89 | +{ |
| 90 | + if (command != nil) { |
| 91 | + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] |
| 92 | + callbackId:command.callbackId]; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +@end |
0 commit comments