Displaying splash screen for longer than default seconds
Solution 1:
No, the default.png
is shown while your app starts up.
You can add a new viewcontroller which will display the default.png
in the application didFinishLoading
.
This way you display the default.png
a bit longer.
You should only show the default.png
if you are loading data, which could take some time.
As the appstore guidelines state, you should not delay starting of you are any longer than necessary.
Solution 2:
You can also use NSThread
:
[NSThread sleepForTimeInterval:(NSTimeInterval)];
You can put this code in to first line of applicationDidFinishLaunching
method.
For example, display default.png for 5 seconds.
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[NSThread sleepForTimeInterval:5.0];
}
Solution 3:
This is super hacky. Don’t do this in production.
Add this to your application:didFinishLaunchingWithOptions:
:
Swift:
// Delay 1 second
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1.0))
Objective C:
// Delay 1 second
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];
Solution 4:
If you are using the LaunchScreen.storyboard you can obtain the same view controller and present it: (remember to set the storyboard id, for example "LaunchScreen")
func applicationDidBecomeActive(application: UIApplication) {
let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("LaunchScreen")
self.window!.rootViewController!.presentViewController(vc, animated: false, completion: nil)
}
SWIFT 4
let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LaunchScreen")
self.window!.rootViewController!.present(vc, animated: false, completion: nil)
Solution 5:
Swift 3
This is doable in a safe way by presenting the splash controller for what ever time you specify then remove it and display your normal rootViewController.
- First in LaunchingScreen.storyboard give your controller a StoryBoard identifier let's say "splashController"
- In Main.storyboard give your initial viewController a StoryBoard identifier let's say "initController". -This could be nav or tab bar etc...-
In AppDelegate you can create these 2 methods:
private func extendSplashScreenPresentation(){ // Get a refernce to LaunchScreen.storyboard let launchStoryBoard = UIStoryboard.init(name: "LaunchScreen", bundle: nil) // Get the splash screen controller let splashController = launchStoryBoard.instantiateViewController(withIdentifier: "splashController") // Assign it to rootViewController self.window?.rootViewController = splashController self.window?.makeKeyAndVisible() // Setup a timer to remove it after n seconds Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(dismissSplashController), userInfo: nil, repeats: false) }
2.
@objc private func dismissSplashController() {
// Get a refernce to Main.storyboard
let mainStoryBoard = UIStoryboard.init(name: "Main", bundle: nil)
// Get initial viewController
let initController = mainStoryBoard.instantiateViewController(withIdentifier: "initController")
// Assign it to rootViewController
self.window?.rootViewController = initController
self.window?.makeKeyAndVisible()
}
Now you call
self.extendSplashScreenPresentation()
in didFinishLaunchingWithOptions.
You are set to go...