Xcodian Solangi
Xcodian Solangi

Reputation: 2408

How can I delay splash launch screen programmatically in Swift Xcode iOS

I have put an image in imageView in LaunchStoreyboard. How can I delay the time of image programmatically?

Here is the Launch Screen Guideline from Apple.

Here is code for Launch Screen View controller:

import UIKit
class LaunshViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delay(0.4)
    }

    func delay(_ delay:Double, closure:@escaping ()->()) {
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
    }
}

Upvotes: 30

Views: 69066

Answers (9)

Furkan E
Furkan E

Reputation: 1

This is very easy to do.Follow the steps for this.Put one line of code in AppDelegate Class;

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            Thread.sleep(forTimeInterval: 3.0)
            return true
    }

Upvotes: 0

Jack
Jack

Reputation: 14329

As of today there is no predefine method from Apple to hold launch screen. Here are some Approaches which are not optimum but works

Approach #1 Create a Separate ViewController which has Launch logo & create a timer or perform some operation (Like Database/Loads some essential network call) depends on your app type this way you can ready with data before hand & hold the launch screen as well :)

Approach #2 Not Optimum

Use Sleep code which holds up the app for a while.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        Thread.sleep(forTimeInterval: 3.0)
        // Override point for customization after application launch.
        return true
    }

Upvotes: 85

Aamir Parwez
Aamir Parwez

Reputation: 49

Putting a thread to sleep is not a good idea.

I would suggest you go to SceneDelegate's "willConnectTo" function and paste this piece of code and you are good to go.

window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()

window?.makeKeyAndVisible()
    
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}
    
guard let _ = (scene as? UIWindowScene) else { return }

Upvotes: -1

adamsfamily
adamsfamily

Reputation: 1974

SwiftUI

For SwiftUI, you can put a very similar code to the accepted answer into ContentView.onAppearing:

struct ContentView: View {

    var body: some View {
        Text("Hello")
        .onAppear {
            Thread.sleep(forTimeInterval: 3.0)
        }
    }
}

Upvotes: 1

alvin
alvin

Reputation: 469

Swift 5.x, iOS 13.x.x

Modifying the following function in the AppDelegate class does not work in Swift 5.x/iOS 13.x.x.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

Instead, you will have to modify the scene function in SceneDelegate class as following. It will delay the LaunchSceen for 3 seconds.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
    window?.makeKeyAndVisible()

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
    }

    guard let _ = (scene as? UIWindowScene) else { return }
}

The window variable should already be there in SceneDelegate class like the following.

var window: UIWindow?

Upvotes: 24

yoowim
yoowim

Reputation: 259

Swift 4.x

It is Not a good practice to put your application to sleep!

Booting your App should be as fast as possible, so the Launch screen delay is something you do not want to use.

But, instead of sleeping you can run a loop during which the receiver processes data from all attached input sources:

This will prolong the launch-screen's visibility time.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    RunLoop.current.run(until: NSDate(timeIntervalSinceNow:1) as Date)

    return true
}

Upvotes: 25

raulmf9325
raulmf9325

Reputation: 31

Definitely your app should not be put to sleep as it may be killed by the OS for being unresponsive for so long.

If you're using a static image for your launch screen, what works for me is to use the image in the LaunchScreen.storyboard, and then when your main controller launches, modally present a VC with the same image as the background in the ViewDidAppear of your main controller (with animated set to false).

You can then use your logic to know when to dismiss the launch screen (dismiss method in the VC with animated set to false).

The transition from the actual LaunchScreen to my VC presenting the same screen looks to me imperceptible.

PS: the ViewDidAppear method might be called more than once, in which case you need to use logic to not present the VC with the launch screen a second time.

Upvotes: 3

oscar
oscar

Reputation: 647

Would not recommending setting the entire application in a waiting state. If the application needs to do more work before finishing the watchdog could kill the application for taking too long time to start up.

Instead you could do something like this to delay the launch screen.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
        window?.makeKeyAndVisible()

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        }
        return true
    }

Upvotes: 24

Saranjith
Saranjith

Reputation: 11567

Create a ViewController and use NSTimer to detect the delay time. and when the timer ends push the first UIViewcontroller.

In ViewDidLoad method..

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fireMethod) userInfo:nil repeats:NO];


-(void)fireMethod
{
// push view controller here..
}

Upvotes: 2

Related Questions