Browse Source

Use UIStoryboard methods for presenting a splash screen on iOS (#1453)

This uses the proper methods for instantiating the initial view on iOS.
By utilizing these methods, we can stand up the initial view controller
from the storyboard.

See also:
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW41
Eli Perkins 7 years ago
parent
commit
36d2c7e35c
1 changed files with 15 additions and 27 deletions
  1. 15
    27
      ios/RCCManager.m

+ 15
- 27
ios/RCCManager.m View File

134
 -(void)showSplashScreen
134
 -(void)showSplashScreen
135
 {
135
 {
136
   CGRect screenBounds = [UIScreen mainScreen].bounds;
136
   CGRect screenBounds = [UIScreen mainScreen].bounds;
137
-  UIView *splashView = nil;
138
-  
137
+  UIViewController *splashViewController = nil;
138
+
139
   NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
139
   NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
140
   if (launchStoryBoard != nil)
140
   if (launchStoryBoard != nil)
141
   {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
141
   {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
142
-    @try
143
-    {
144
-      splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
145
-      if (splashView != nil)
146
-      {
147
-        splashView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height);
148
-      }
149
-    }
150
-    @catch(NSException *e)
151
-    {
152
-      splashView = nil;
153
-    }
142
+    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:launchStoryBoard bundle:[NSBundle mainBundle]];
143
+    splashViewController = [storyboard instantiateInitialViewController];
154
   }
144
   }
155
   else
145
   else
156
   {//load the splash from the DEfault image or from LaunchImage in the xcassets
146
   {//load the splash from the DEfault image or from LaunchImage in the xcassets
157
     CGFloat screenHeight = screenBounds.size.height;
147
     CGFloat screenHeight = screenBounds.size.height;
158
-    
148
+
159
     NSString* imageName = @"Default";
149
     NSString* imageName = @"Default";
160
     if (screenHeight == 568)
150
     if (screenHeight == 568)
161
       imageName = [imageName stringByAppendingString:@"-568h"];
151
       imageName = [imageName stringByAppendingString:@"-568h"];
163
       imageName = [imageName stringByAppendingString:@"-667h"];
153
       imageName = [imageName stringByAppendingString:@"-667h"];
164
     else if (screenHeight == 736)
154
     else if (screenHeight == 736)
165
       imageName = [imageName stringByAppendingString:@"-736h"];
155
       imageName = [imageName stringByAppendingString:@"-736h"];
166
-    
156
+
167
     //xcassets LaunchImage files
157
     //xcassets LaunchImage files
168
     UIImage *image = [UIImage imageNamed:imageName];
158
     UIImage *image = [UIImage imageNamed:imageName];
169
     if (image == nil)
159
     if (image == nil)
170
     {
160
     {
171
       imageName = @"LaunchImage";
161
       imageName = @"LaunchImage";
172
-      
162
+
173
       if (screenHeight == 480)
163
       if (screenHeight == 480)
174
         imageName = [imageName stringByAppendingString:@"-700"];
164
         imageName = [imageName stringByAppendingString:@"-700"];
175
       if (screenHeight == 568)
165
       if (screenHeight == 568)
178
         imageName = [imageName stringByAppendingString:@"-800-667h"];
168
         imageName = [imageName stringByAppendingString:@"-800-667h"];
179
       else if (screenHeight == 736)
169
       else if (screenHeight == 736)
180
         imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
170
         imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
181
-      
171
+
182
       image = [UIImage imageNamed:imageName];
172
       image = [UIImage imageNamed:imageName];
183
     }
173
     }
184
-    
174
+
185
     if (image != nil)
175
     if (image != nil)
186
     {
176
     {
187
-      splashView = [[UIImageView alloc] initWithImage:image];
177
+      UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
178
+      splashViewController = [[UIViewController alloc] init];
179
+      splashViewController.view = imageView;
188
     }
180
     }
189
   }
181
   }
190
-  
191
-  if (splashView != nil)
192
-  {
193
-    UIViewController *splashVC = [[UIViewController alloc] init];
194
-    splashVC.view = splashView;
195
-    
182
+
183
+  if (splashViewController != nil) {
196
     id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
184
     id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
197
-    appDelegate.window.rootViewController = splashVC;
185
+    appDelegate.window.rootViewController = splashViewController;
198
     [appDelegate.window makeKeyAndVisible];
186
     [appDelegate.window makeKeyAndVisible];
199
   }
187
   }
200
 }
188
 }