iOS Recipes - Matt Drance [5]
BasicSplashScreen/iPhone/AppDelegate_iPhone.m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:self.navController.view];
self.splashScreen.showsStatusBarOnDismissal = YES;
self.splashScreen.modalTransitionStyle =
UIModalTransitionStyleCrossDissolve;
[self.navController presentModalViewController:splashScreen
animated:NO];
[self.window makeKeyAndVisible];
return YES;
}
If you open MainWindow_iPhone.xib, you’ll see a PRPSplashScreen object defined in the XIB. (See Figure 2, Connecting the splash screen in Interface Builder .) This object is connected to the app delegate’s splashScreen property in Interface Builder. The previous code references this property in order to kick off the splash transition.
The splash screen is initialized from the respective MainWindow XIB file and connected to the app delegate’s splashScreen property. The app delegate is also connected as the splash screen’s delegate.
Figure 2. Connecting the splash screen in Interface Builder
* * *
Once the window becomes visible, the splash screen view controller receives the standard UIViewController messages, including ‑viewDidAppear :. This is the cue to begin the transition, and it’s very simple. We first alert the delegate that the splash view appeared, in case the delegate needs to prepare for the transition. It’s important to first check whether the delegate has implemented the appropriate methods, because we declared them as optional in our delegate protocol. After messaging the delegate, we send ‑hide to perform the splash transition. Note that we use performSelector:withObject:afterDelay: here, which gives the UIKit run loop an opportunity to finalize the viewDidAppear machinery. Dismissing a view controller from within its own viewWillAppear: or viewDidAppear: method can confuse the system—each action needs to be separate and discrete.
BasicSplashScreen/PRPSplashScreen.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
SEL didAppearSelector = @selector(splashScreenDidAppear:);
if ([self.delegate respondsToSelector:didAppearSelector]) {
[self.delegate splashScreenDidAppear:self];
}
[self performSelector:@selector(hide) withObject:nil afterDelay:0];
}
The ‑hide method uses the standard ‑dismissModalViewControllerAnimated: method to perform the transition, after checking whether it should show the status bar while fading out. This is added in case you don’t want the status bar shown at launch but do want it on the UI. To enable this effect, set UIStatusBarHidden to YES in your app’s Info.plist file, and set the splash screen’s showsStatusBarOnDismissal property to YES. The splash screen manages the status bar’s reactivation so you don’t need to do it yourself in one of the delegate methods (see Figure 3, Hiding the status bar on launch ).
Set the UIStatusBarHidden key to YES to hide the status bar on launch. If you want to show it in your main UI, set the splash screen’s showsStatusBarOnDismissal property to YES.
Figure 3. Hiding the status bar on launch
* * *
BasicSplashScreen/PRPSplashScreen.m
- (void)hide {
if (self.showsStatusBarOnDismissal) {
UIApplication *app = [UIApplication sharedApplication];
[app setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
[self dismissModalViewControllerAnimated:YES];
}
The splash screen also keeps the delegate informed of the transition’s progress by relaying the standard ‑viewWillDisappear: and ‑viewDidDisappear: view controller methods. The app delegate uses the corresponding ‑splashScreenDidDisappear: delegate method to remove the splash screen once it’s not needed.
BasicSplashScreen/iPhone/AppDelegate_iPhone.m
- (void)splashScreenDidDisappear:(PRPSplashScreen *)splashScreen {
self.splashScreen = nil;
}
Run the BasicSplashScreen