Browse Source

Support launch storyboards as splash screens on iOS (#1165)

* Support launch storyboards as splash screens
Ryan Davies 6 years ago
parent
commit
c02c5405fa
1 changed files with 131 additions and 63 deletions
  1. 131
    63
      ios/RCCManager.m

+ 131
- 63
ios/RCCManager.m View File

@@ -158,91 +158,159 @@
158 158
   self.bundleURL = bundleURL;
159 159
   self.sharedBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
160 160
   
161
-  [self showSplashScreen];
161
+  [[self class] showSplashScreen];
162 162
 }
163 163
 
164
--(void)showSplashScreen
164
+-(RCTBridge*)getBridge
165
+{
166
+  return self.sharedBridge;
167
+}
168
+
169
+-(UIWindow*)getAppWindow
170
+{
171
+  UIApplication *app = [UIApplication sharedApplication];
172
+  UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
173
+  return window;
174
+}
175
+
176
+#pragma mark - Splash Screen
177
+
178
++ (void)showSplashScreen
165 179
 {
180
+  
181
+  UIViewController* viewControllerFromLaunchStoryboard;
182
+  viewControllerFromLaunchStoryboard = [self viewControllerFromLaunchStoryboard];
183
+  if (viewControllerFromLaunchStoryboard)
184
+  {
185
+    [self showSplashScreenViewController:viewControllerFromLaunchStoryboard];
186
+    return;
187
+  }
188
+  
166 189
   CGRect screenBounds = [UIScreen mainScreen].bounds;
167
-  UIView *splashView = nil;
190
+  UIViewController* viewControllerFromLaunchNib = [self viewControllerFromLaunchNibForScreenBounds:screenBounds];
191
+  if (viewControllerFromLaunchNib)
192
+  {
193
+    [self showSplashScreenViewController:viewControllerFromLaunchNib];
194
+    return;
195
+  }
196
+  
197
+  UIViewController* viewControllerFromLaunchImage = [self viewControllerFromLaunchImageForScreenBounds:screenBounds];
198
+  if (viewControllerFromLaunchImage)
199
+  {
200
+    [self showSplashScreenViewController:viewControllerFromLaunchImage];
201
+    return;
202
+  }
203
+  
204
+  UIViewController* viewController = [[UIViewController alloc] init];
205
+  viewController.view.frame = screenBounds;
206
+  viewController.view.backgroundColor = [UIColor whiteColor];
207
+  
208
+  [self showSplashScreenViewController:viewController];
209
+}
210
+
211
++ (UIViewController *)viewControllerFromLaunchStoryboard
212
+{
213
+  NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
214
+  if (launchStoryboardName == nil)
215
+  {
216
+    return nil;
217
+  }
218
+  
219
+  UIStoryboard* storyboard = [UIStoryboard storyboardWithName:launchStoryboardName bundle:nil];
220
+  if (storyboard == nil)
221
+  {
222
+    return nil;
223
+  }
224
+  
225
+  return storyboard.instantiateInitialViewController;
226
+}
227
+
228
++ (UIViewController *)viewControllerFromLaunchNibForScreenBounds:(CGRect)screenBounds
229
+{
230
+  NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
231
+  if (launchStoryboardName == nil)
232
+  {
233
+    return nil;
234
+  }
168 235
   
169
-  NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
170
-  if (launchStoryBoard != nil)
171
-  {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
172
-    @try
236
+  @try {
237
+    id nibContents = [[NSBundle mainBundle] loadNibNamed:launchStoryboardName owner:self options:nil];
238
+    if (!nibContents || [nibContents count] == 0)
173 239
     {
174
-      splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
175
-      if (splashView != nil)
176
-      {
177
-        splashView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height);
178
-      }
240
+      return nil;
179 241
     }
180
-    @catch(NSException *e)
242
+    
243
+    id firstObject = [nibContents firstObject];
244
+    if (![firstObject isKindOfClass:[UIView class]])
181 245
     {
182
-      splashView = nil;
246
+      return nil;
183 247
     }
248
+    
249
+    UIViewController* viewController = [[UIViewController alloc] init];
250
+    viewController.view = (UIView *)firstObject;
251
+    viewController.view.frame = screenBounds;
252
+    return viewController;
253
+  }
254
+  @catch(NSException *exception) {
255
+    return nil;
184 256
   }
185
-  else
186
-  {//load the splash from the DEfault image or from LaunchImage in the xcassets
187
-    CGFloat screenHeight = screenBounds.size.height;
257
+}
258
+
259
++ (UIViewController *)viewControllerFromLaunchImageForScreenBounds:(CGRect)screenBounds
260
+{
261
+  //load the splash from the default image or from LaunchImage in the xcassets
262
+  
263
+  CGFloat screenHeight = screenBounds.size.height;
264
+  
265
+  NSString* imageName = @"Default";
266
+  if (screenHeight == 568)
267
+    imageName = [imageName stringByAppendingString:@"-568h"];
268
+  else if (screenHeight == 667)
269
+    imageName = [imageName stringByAppendingString:@"-667h"];
270
+  else if (screenHeight == 736)
271
+    imageName = [imageName stringByAppendingString:@"-736h"];
272
+  
273
+  //xcassets LaunchImage files
274
+  UIImage *image = [UIImage imageNamed:imageName];
275
+  if (image == nil)
276
+  {
277
+    imageName = @"LaunchImage";
188 278
     
189
-    NSString* imageName = @"Default";
279
+    if (screenHeight == 480)
280
+      imageName = [imageName stringByAppendingString:@"-700"];
190 281
     if (screenHeight == 568)
191
-      imageName = [imageName stringByAppendingString:@"-568h"];
282
+      imageName = [imageName stringByAppendingString:@"-700-568h"];
192 283
     else if (screenHeight == 667)
193
-      imageName = [imageName stringByAppendingString:@"-667h"];
284
+      imageName = [imageName stringByAppendingString:@"-800-667h"];
194 285
     else if (screenHeight == 736)
195
-      imageName = [imageName stringByAppendingString:@"-736h"];
286
+      imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
287
+    else if (screenHeight == 812)
288
+      imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"];
289
+    else if (screenHeight == 1024)
290
+      imageName = [imageName stringByAppendingString:@"-Portrait"];
196 291
     
197
-    //xcassets LaunchImage files
198
-    UIImage *image = [UIImage imageNamed:imageName];
199
-    if (image == nil)
200
-    {
201
-      imageName = @"LaunchImage";
202
-      
203
-      if (screenHeight == 480)
204
-        imageName = [imageName stringByAppendingString:@"-700"];
205
-      if (screenHeight == 568)
206
-        imageName = [imageName stringByAppendingString:@"-700-568h"];
207
-      else if (screenHeight == 667)
208
-        imageName = [imageName stringByAppendingString:@"-800-667h"];
209
-      else if (screenHeight == 736)
210
-        imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
211
-      else if (screenHeight == 812)
212
-        imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"];
213
-      else if (screenHeight == 1024)
214
-        imageName = [imageName stringByAppendingString:@"-Portrait"];
215
-
216
-      image = [UIImage imageNamed:imageName];
217
-    }
218
-    
219
-    if (image != nil)
220
-    {
221
-      splashView = [[UIImageView alloc] initWithImage:image];
222
-    }
292
+    image = [UIImage imageNamed:imageName];
223 293
   }
224 294
   
225
-  if (splashView != nil)
295
+  if (image == nil)
226 296
   {
227
-    UIViewController *splashVC = [[UIViewController alloc] init];
228
-    splashVC.view = splashView;
229
-    
230
-    id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
231
-    appDelegate.window.rootViewController = splashVC;
232
-    [appDelegate.window makeKeyAndVisible];
297
+    return nil;
233 298
   }
299
+  
300
+  UIViewController* viewController = [[UIViewController alloc] init];
301
+  
302
+  UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
303
+  viewController.view = imageView;
304
+  viewController.view.frame = screenBounds;
305
+  
306
+  return viewController;
234 307
 }
235 308
 
236
--(RCTBridge*)getBridge
237
-{
238
-  return self.sharedBridge;
239
-}
240
-
241
--(UIWindow*)getAppWindow
309
++ (void)showSplashScreenViewController:(UIViewController *)viewController
242 310
 {
243
-  UIApplication *app = [UIApplication sharedApplication];
244
-  UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
245
-  return window;
311
+  id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
312
+  appDelegate.window.rootViewController = viewController;
313
+  [appDelegate.window makeKeyAndVisible];
246 314
 }
247 315
 
248 316
 -(NSDictionary*)getAppStyle