Related
Is it possible to display the Default.png for a specified number of seconds? I have a client that wants the splash screen displayed for longer than its current time.
They would like it displayed for 2 - 3 seconds.
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.
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];
}
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]];
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)
In Xcode 6.1, Swift 1.0 to delay the launch screen:
Add the below statement in e didFinishLaunchingWithOptions meth in AppDelegateod
NSThread.sleepForTimeInterval(3)
Here, time can be passed based on your requirement.
SWIFT 5
Thread.sleep(forTimeInterval: 3)
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...
This tutorial displays splash screen for 2 seconds. You can easily change it to suit your needs.
- (void)showSplash {
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view = modelView;
[self presentModalViewController:modalViewController animated:NO];
[self performSelector:#selector(hideSplash) withObject:nil afterDelay:yourDelay];
}
This worked for me in Xcode 6.3.2, Swift 1.2 :
import UIKit
class ViewController: UIViewController
{
var splashScreen:UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
self.splashScreen = UIImageView(frame: self.view.frame)
self.splashScreen.image = UIImage(named: "Default.png")
self.view.addSubview(self.splashScreen)
var removeSplashScreen = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "removeSP", userInfo: nil, repeats: false)
}
func removeSP()
{
println(" REMOVE SP")
self.splashScreen.removeFromSuperview()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
ViewController is the first app VC that is being loaded.
In Swift 4.2
For Delay 1 second after default launch time...
Thread.sleep(forTimeInterval: 1)
Use following line in didFinishLaunchingWithOptions: delegate method:
[NSThread sleepForTimeInterval:5.0];
It will stop splash screen for 5.0 seconds.
Swift 2.0:
1)
// AppDelegate.swift
import UIKit
import Foundation
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var splashTimer:NSTimer?
var splashImageView:UIImageView?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIApplication.sharedApplication().delegate!.window!
let splashImage: UIImage = UIImage(named: "ic_120x120.png")!
splashImageView = UIImageView(image: splashImage)
splashImageView!.frame = CGRectMake(0, 0, (window?.frame.width)!, (window?.frame.height)!)
window!.addSubview(splashImageView!)
window!.makeKeyAndVisible()
//Adding splash Image as UIWindow's subview.
window!.bringSubviewToFront(window!.subviews[0])
// Here specify the timer.
splashTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "splashTimerForLoadingScreen", userInfo: nil, repeats: true)
return true
}
func splashTimerForLoadingScreen() {
splashImageView!.removeFromSuperview()
splashTimer!.invalidate()
}
2)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSThread.sleepForTimeInterval(9)
OR
sleep(9)
return true
}
3) Using root view controller concept:
// AppDelegate.swift
import UIKit
import Foundation
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var splashTimer:NSTimer?
var storyboard:UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
storyboard = UIStoryboard(name: "Main", bundle: nil)
//Here set the splashScreen VC
let rootController = storyboard!.instantiateViewControllerWithIdentifier("secondVCID")
if let window = self.window {
window.rootViewController = rootController
}
//Set Timer
splashTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "splashTimerCrossedTimeLimit", userInfo: nil, repeats: true)
return true
}
func splashTimerCrossedTimeLimit(){
//Here change the root controller
let rootController = storyboard!.instantiateViewControllerWithIdentifier("firstVCID")
if let window = self.window {
window.rootViewController = rootController
}
splashTimer?.invalidate()
}
You can use following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableString *path = [[NSMutableString alloc]init];
[path setString:[[NSBundle mainBundle] resourcePath]];
[path setString:[path stringByAppendingPathComponent:#"Default.png"]];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
[path release];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
imageView.frame=CGRectMake(0, 0, 320, 480);
imageView.tag = 2;
[window addSubview:imageView];
[window makeKeyAndVisible];
// Here specify the time limit.
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(timerForLoadingScreen) userInfo:nil repeats:YES];
}
-(void)timerForLoadingScreen
{
[timer invalidate];
if ([window viewWithTag:2]!=nil)
{
[[window viewWithTag:2]removeFromSuperview];
}
// Your any other initialization code that you wish to have in didFinishLaunchingWithOptions
}
Put your default.png in a UIImageView full screen as a subview on the top of your main view thus covering your other UI. Set a timer to remove it after x seconds (possibly with effects) now showing your application.
The simplest way to achieve this is to creat an UIImageView with "Default.png" on the top of your first ViewController's UIView.
And add an Timer to remove the UIImageView after seconds you expected.
Write sleep(5.0)
in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions for 5 seconds splash screen will be displayed
This works...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Load Splash View Controller first
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Splash"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
// Load other stuff that requires time
// Now load the main View Controller that you want
}
1.Add a another view controller in “didFinishLaunchingWithOptions”
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *homeNav = [storyboard instantiateViewControllerWithIdentifier:#"NavigationControllerView"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"SplashViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = homeNav;
[self.window makeKeyAndVisible];
[(UINavigationController *)self.window.rootViewController pushViewController:viewController animated:NO];
}
2.In view did load of SplashView Controller
[self performSelector:#selector(removeSplashScreenAddViewController) withObject:nil afterDelay:2.0];
3.In removeSplashScreenAddViewController method you can add your main view controller for eg.-
- (void) removeSplashScreenAddViewController {` UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *homeNav = [storyboard instantiateViewControllerWithIdentifier:#"HomeNav"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerName];
UIWindow *window = [StaticHelper mainWindow];
window.rootViewController = homeNav;
[window makeKeyAndVisible];
[(UINavigationController *)window.rootViewController pushViewController:viewController animated:NO];`}
In swift 4.0
For Delay of 1 second after default launch time...
RunLoop.current.run(until: Date(timeIntervalSinceNow : 1.0))
You can simple specify number of seconds to sleep in the AppDelegate didFinishLaunchingWithOptions method.
Or alternatively use another ImageView to customize the splash screen.
See details for the latter at the following link by me:
Splash Screen Problem
Just go on project name. then Right Click/properties/Application Tab.
Find "view Application Events" near Slash form combobox.
copy this code in myApplication Class :
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
System.Threading.Thread.Sleep(3000) ' or other time
End Sub
You can create your own view and display it when application starts and hide it with timer. Please avoid delaying app start as its bad idea
The simplest solution here is to add sleep() to didFinishLaunchingWithOptions method in your AppDelegate class.
Swift 4:
sleep(1)
delays the LaunchScreen by 1 second.
If you wanna do something fancier you can also extend the current RunLoop in the same method:
Swift 4:
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
I want to make slide out menu. For this I have to create UINavigationViewController which will controlMenuViewController(Table View) and ProfileViewController(Content View)
I want to set UINavigationViewController like a rootViewController, for this I wrote this code in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let nav1 = UINavigationController()
let mainView = MainViewController(menuViewController: nil, contentViewController: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window?.rootViewController = nav1
self.window?.makeKeyAndVisible()
return true
}
MainViewController is my UINavigationViewController.
But here I have error
nil is not compatible with expected argument type 'UIViewController'
What I should do?
You pass nil here: MainViewController(menuViewController: nil, contentViewController: nil) instead of some view controllers. That's probably the reason it doesn't compile.
You embed your MainViewController instance, which is already UINavigationController, into another one UINavigationController. That looks wrong too.
Here is an example:
LoginViewController *loginController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;`
This error means you can't set menuViewController and contentViewController to nil, they are not optional
I changed "Launch Screen File" to my "Main.Storyboard" in settings. Before doing this app was working fine, and opened directly without any problem. But after adding this there was a black screen before the launch of storyboard. This is very bad user experience. Help me to solve this. AppDelegate and viewController has nothing big. I have a LaunchScreen.xib in project and also LaunchImage in image assets. I am using Storyboard in project. I don't want a launchScreen in my app, I just wanna directly go to my storyboard.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = utilitiesObjet.getViewController("MainVC", mainStoryBoardName: "Main")
self.window?.makeKeyAndVisible()
Fabric.with([Crashlytics.self()])
return true
}
I solved it by adding a LaunchScreen.StoryBoard, Instead of using a LaunchScreen.xib. Thank you All :)
use this code in appdelegate this code for objective c
.....did finish launching with option method...
self.winow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"<Your Storyboard name>" bundle:nil];
LoginViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"<your view controller storyboard identifer name>"];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:ivc];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
For Swift....
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Override point for customization after application launch.
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
return true
}
Launch Screen different Main.StoryBoard. In Launch Screen File, you must type: "LaunchScreen" and choose file Lscreen.storyboard (xcode7), or .xib if Xcode6
This is didFinishLaunchingWithOptions Method in AppDelegate.
Let me explain scenario, I have developed sideMenu like facebook in my app, but now I have to change the sideMenu list according to screens (ViewController)
Here the side Menu is SideMenuViewController, which is an argument in contain, which ultimately becomes window's rootViewController.
SO, The very basic question arises is "How to change the controller or variable which becomes rootViewController of windows"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init];
self.container = [ContainerOfSideMenuByVeerViewController
containerWithCenterViewController:[self navigationController]
leftMenuViewController:leftMenuViewController];
self.window.rootViewController = self.container;
[self.window makeKeyAndVisible];
return YES;
}
If any programmer wants to know more code or requirement, I do welcome to provide by editing my code or in comments.
Try this:
<YourAppDelegateClass> *app = [[UIApplication sharedApplication] delegate];
app.window.rootViewController = <YourRootViewController>;
Don't forget to include necessary headers (your AppDelegate, for example) or you'll get CE.
This one seems to work:
With Storyboard
Inside another ViewController you can use this code:
self.view.window.rootViewController = [self.view.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"**STORYBOARD ID**"];
Inside AppDelegate:
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"**STORYBOARD ID**"];
Knowledge Sharing Using Swift:
Changing root view controller from class other than app delegate.swift
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav
Changing rootviewcontroller With Animation can be achieve with:
UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
self.window?.rootViewController = anyViewController
}, completion: nil)
We can write generalise method too similar to this.
Hope this will helpful for someone.
Is it possible to display the Default.png for a specified number of seconds? I have a client that wants the splash screen displayed for longer than its current time.
They would like it displayed for 2 - 3 seconds.
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.
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];
}
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]];
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)
In Xcode 6.1, Swift 1.0 to delay the launch screen:
Add the below statement in e didFinishLaunchingWithOptions meth in AppDelegateod
NSThread.sleepForTimeInterval(3)
Here, time can be passed based on your requirement.
SWIFT 5
Thread.sleep(forTimeInterval: 3)
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...
This tutorial displays splash screen for 2 seconds. You can easily change it to suit your needs.
- (void)showSplash {
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view = modelView;
[self presentModalViewController:modalViewController animated:NO];
[self performSelector:#selector(hideSplash) withObject:nil afterDelay:yourDelay];
}
This worked for me in Xcode 6.3.2, Swift 1.2 :
import UIKit
class ViewController: UIViewController
{
var splashScreen:UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
self.splashScreen = UIImageView(frame: self.view.frame)
self.splashScreen.image = UIImage(named: "Default.png")
self.view.addSubview(self.splashScreen)
var removeSplashScreen = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "removeSP", userInfo: nil, repeats: false)
}
func removeSP()
{
println(" REMOVE SP")
self.splashScreen.removeFromSuperview()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
ViewController is the first app VC that is being loaded.
In Swift 4.2
For Delay 1 second after default launch time...
Thread.sleep(forTimeInterval: 1)
Use following line in didFinishLaunchingWithOptions: delegate method:
[NSThread sleepForTimeInterval:5.0];
It will stop splash screen for 5.0 seconds.
Swift 2.0:
1)
// AppDelegate.swift
import UIKit
import Foundation
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var splashTimer:NSTimer?
var splashImageView:UIImageView?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIApplication.sharedApplication().delegate!.window!
let splashImage: UIImage = UIImage(named: "ic_120x120.png")!
splashImageView = UIImageView(image: splashImage)
splashImageView!.frame = CGRectMake(0, 0, (window?.frame.width)!, (window?.frame.height)!)
window!.addSubview(splashImageView!)
window!.makeKeyAndVisible()
//Adding splash Image as UIWindow's subview.
window!.bringSubviewToFront(window!.subviews[0])
// Here specify the timer.
splashTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "splashTimerForLoadingScreen", userInfo: nil, repeats: true)
return true
}
func splashTimerForLoadingScreen() {
splashImageView!.removeFromSuperview()
splashTimer!.invalidate()
}
2)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSThread.sleepForTimeInterval(9)
OR
sleep(9)
return true
}
3) Using root view controller concept:
// AppDelegate.swift
import UIKit
import Foundation
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var splashTimer:NSTimer?
var storyboard:UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
storyboard = UIStoryboard(name: "Main", bundle: nil)
//Here set the splashScreen VC
let rootController = storyboard!.instantiateViewControllerWithIdentifier("secondVCID")
if let window = self.window {
window.rootViewController = rootController
}
//Set Timer
splashTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "splashTimerCrossedTimeLimit", userInfo: nil, repeats: true)
return true
}
func splashTimerCrossedTimeLimit(){
//Here change the root controller
let rootController = storyboard!.instantiateViewControllerWithIdentifier("firstVCID")
if let window = self.window {
window.rootViewController = rootController
}
splashTimer?.invalidate()
}
You can use following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableString *path = [[NSMutableString alloc]init];
[path setString:[[NSBundle mainBundle] resourcePath]];
[path setString:[path stringByAppendingPathComponent:#"Default.png"]];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
[path release];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
imageView.frame=CGRectMake(0, 0, 320, 480);
imageView.tag = 2;
[window addSubview:imageView];
[window makeKeyAndVisible];
// Here specify the time limit.
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(timerForLoadingScreen) userInfo:nil repeats:YES];
}
-(void)timerForLoadingScreen
{
[timer invalidate];
if ([window viewWithTag:2]!=nil)
{
[[window viewWithTag:2]removeFromSuperview];
}
// Your any other initialization code that you wish to have in didFinishLaunchingWithOptions
}
Put your default.png in a UIImageView full screen as a subview on the top of your main view thus covering your other UI. Set a timer to remove it after x seconds (possibly with effects) now showing your application.
The simplest way to achieve this is to creat an UIImageView with "Default.png" on the top of your first ViewController's UIView.
And add an Timer to remove the UIImageView after seconds you expected.
Write sleep(5.0)
in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions for 5 seconds splash screen will be displayed
This works...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Load Splash View Controller first
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Splash"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
// Load other stuff that requires time
// Now load the main View Controller that you want
}
1.Add a another view controller in “didFinishLaunchingWithOptions”
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *homeNav = [storyboard instantiateViewControllerWithIdentifier:#"NavigationControllerView"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"SplashViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = homeNav;
[self.window makeKeyAndVisible];
[(UINavigationController *)self.window.rootViewController pushViewController:viewController animated:NO];
}
2.In view did load of SplashView Controller
[self performSelector:#selector(removeSplashScreenAddViewController) withObject:nil afterDelay:2.0];
3.In removeSplashScreenAddViewController method you can add your main view controller for eg.-
- (void) removeSplashScreenAddViewController {` UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *homeNav = [storyboard instantiateViewControllerWithIdentifier:#"HomeNav"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerName];
UIWindow *window = [StaticHelper mainWindow];
window.rootViewController = homeNav;
[window makeKeyAndVisible];
[(UINavigationController *)window.rootViewController pushViewController:viewController animated:NO];`}
In swift 4.0
For Delay of 1 second after default launch time...
RunLoop.current.run(until: Date(timeIntervalSinceNow : 1.0))
You can simple specify number of seconds to sleep in the AppDelegate didFinishLaunchingWithOptions method.
Or alternatively use another ImageView to customize the splash screen.
See details for the latter at the following link by me:
Splash Screen Problem
Just go on project name. then Right Click/properties/Application Tab.
Find "view Application Events" near Slash form combobox.
copy this code in myApplication Class :
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
System.Threading.Thread.Sleep(3000) ' or other time
End Sub
You can create your own view and display it when application starts and hide it with timer. Please avoid delaying app start as its bad idea
The simplest solution here is to add sleep() to didFinishLaunchingWithOptions method in your AppDelegate class.
Swift 4:
sleep(1)
delays the LaunchScreen by 1 second.
If you wanna do something fancier you can also extend the current RunLoop in the same method:
Swift 4:
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))