Shofiur
Shofiur

Reputation:

How can I display a splash screen for longer on an iPhone?

How can I display a splash screen for a longer period of time than the default time on an iPhone?

Upvotes: 49

Views: 72900

Answers (24)

sagaya martin
sagaya martin

Reputation: 116

I have done this as below:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil];
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"splashView"];
    self.window.rootViewController = viewController;

Note: LaunchScreen is the launch story borad and splashView is the storyboardIdentifier

Upvotes: 1

Stan
Stan

Reputation: 1583

Swift version:

Add this line in the AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    NSThread.sleepForTimeInterval(2.0)//in seconds

    return true
}

Upvotes: 0

hites
hites

Reputation: 159

There is default image (default.png) is shown when you start your app.

So you can add a new viewcontroller which will display the that default image in the application didFinishLoading.

So by this logic you display the default image for a bit longer.

Upvotes: 0

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

Even though it is against the guidelines but if you still want to do this than a better approach rather than sleeping thread will be

//Extend the splash screen for 3 seconds.
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];

this way the main thread is not blocked and if it is listening for any notifications and some other network related stuff, it still carries on.

UPDATE FOR SWIFT:

 NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow:3))

Upvotes: 15

Alvin George
Alvin George

Reputation: 14296

Swift 2.0

Use following line in didFinishLaunchingWithOptions: delegate method:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    NSThread.sleepForTimeInterval(5.0)
    return true
}

But I recommend this:

Put your image 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 some seconds (possibly with effects) now showing your application.

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: "Logo.png")
            self.view.addSubview(self.splashScreen)
    
            var removeSplashScreen = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: "removeSplashImage", userInfo: nil, repeats: false)
        }
        func removeSplashImage()
        { 
            self.splashScreen.removeFromSuperview()
        }
    }

Upvotes: 0

livingtech
livingtech

Reputation: 3660

There are many options already posted here, but I ran into cocoapod today that allows you to display the contents of your LaunchScreen.xib as the initial view controller:

https://github.com/granoff/LaunchScreen (Also see this blog post from the author with more implementation details.)

This seems like a fairly straight-forward way to do this, and better than the vast majority of answers posted here. (Of course it wasn't possible until the introduction of LaunchScreen files in the first place.) It is possible to display an activity indicator (or anything else you want) on top of the view.

As for why you would want to do this, I'm surprised that no one has mentioned that there are often publisher and/or partner requirements around this sort of thing. It's VERY common in games, but advertising-funded applications as well.

Also note that this does act counter to the HIG, but then so does waiting to load any content after your application launches. Remember that the HIG are guidelines, and not requirements.

One final note: It's my personal opinion, that any time an initial screen like this is implemented, you should be able to tap to dismiss it.

Upvotes: 0

ddnl
ddnl

Reputation: 505

In Xcode 6.3, you can show the launch screen.xib and even put a indicator on it, first it will show the default launch screen it is replaced by the nib so the user doesn't know it changed and then if everything is loaded hide it :-)

    func showLaunchScreen() {
    // show launchscreen
    launchView = NSBundle.mainBundle().loadNibNamed("LaunchScreen", owner: self, options: nil)[0] as! UIView
    launchView.frame = self.view.bounds;
    self.view.addSubview(launchView)

    // show indicator
    launchScreenIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) as UIActivityIndicatorView
    launchScreenIndicator.center = CGPointMake(self.view.center.x, self.view.center.y+100)
    launchScreenIndicator.hidesWhenStopped = true
    launchScreenIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    launchView.addSubview(launchScreenIndicator)
    self.view.addSubview(launchView)
    launchScreenIndicator.startAnimating()

    self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true) //or animated: false
}

func removeLaunchScreen() {
    println("remove launchscreen")
    self.launchView.removeFromSuperview()
    self.launchScreenIndicator.stopAnimating()
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Upvotes: 2

ZiggyZag
ZiggyZag

Reputation: 1

What I did is presented a modalview controller in the initial screen and then dissmiss it after several seconds

    - (void)viewDidLoad
{
    [super viewDidLoad];
    ....
  saSplash = [storyboard instantiateViewControllerWithIdentifier:@"SASplashViewController"];
    saSplash.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentModalViewController: saSplash animated:NO];
}

-(void) dismissSASplash {
    [saSplash dismissModalViewControllerAnimated:NO];

}

Upvotes: 0

Vinod Joshi
Vinod Joshi

Reputation: 7862

 Inside your AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Sleep is code to stop the slash screen for 5MoreSeconds
        sleep(5);

        [self initializeStoryBoardBasedOnScreenSize];
        return YES;
    }

//VKJ

Upvotes: 2

Charles Robertson
Charles Robertson

Reputation: 1820

Yes, the simplest way is (remember to add your 'default.png' to targets -> [yourProjectName]: launch images in 'xCode'):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NSThread sleepForTimeInterval:3.0];
}

Upvotes: 11

x4h1d
x4h1d

Reputation: 6092

According to the Apple HIG you should not do that. But if your application needs to do so for definite purpose, you can do:

  • import <unistd.h> in your AppDelegate.m
  • Write the following line at the first of the "application didFinishLaunchingWithOptions:" method

    sleep(//your time in sec goes here//);

Upvotes: 1

vshall
vshall

Reputation: 619

just make the window sleep for some seconds in applicationDidFininshLaunchings method

example: sleep(3)

Upvotes: 1

Taimur Ajmal
Taimur Ajmal

Reputation: 2855

in your appDelegate , theres a method called applicationDidFinishedLaunching use a sleep function. Pass a digit in the sleep function for the no. of seconds you want to hold screen.


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    [window makeKeyAndVisible];
    [window addSubview:viewController.view];
    sleep(5);
    return YES;
}

I searched so much for this thing and everybody gave their own complex point of view. I couldn't find a simple way that would just let me do it.

KISS ( Keep it simple and Smart :) I avoided the actual as its offensive.

Upvotes: 18

Mysliwy
Mysliwy

Reputation: 27

The simplest way is to put your application's main thread into a sleep mode for desired period of time. Provided that "Default.png" exists in your application's bundle it will be displayed for as long as the main thread is asleep:


-(void)applicationDidFinishLaunching:(UIApplication *)application {
    [NSThread sleepForTimeInterval:5];
    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    [window setBackgroundColor:[UIColor yellowColor]];
    [window makeKeyAndVisible];
}

As you are already aware, it's a horribly bad idea to do but it should work just fine...

Upvotes: 1

jimiHendrix
jimiHendrix

Reputation: 169

I did it pretty simply, by having my rootViewController push a modalViewController, loading from "Splash.nib" in a subclass of UIViewController I called "SplashViewController". The exact call was:

- (void) viewDidLoad {
SplashViewController *splashScreen = [[[SplashViewController alloc]    
        initWithNibName:@"SplashViewController" bundle:nil] autorelease];
[self presentModalViewController:splashScreen animated:NO];
//continue loading while MVC is over top...

When you launch the app, it pops right up, like a splash screen should. Then, the SplashViewController nib is just a full-screen UIImageView with a splash png, 320x480. After a 1-second NSTimer (anything more did seem to get in the way), it fires timerFireMethod, a custom method that just calls

[self dismissModalViewControllerAnimated:YES];

Then the modal VC just slides down and away, leaving my top tableView. The nice thing is, while the MVC is up, the underlying table can continue to load due to the independent nature of modal view controllers. So, I don't think this violates the HIGs, and actually does allow for faster launching. What would you rather look at, a cute picture, or an empty default view (snore)?

Upvotes: 14

petert
petert

Reputation: 6692

I agree it can make sense to have a splash screen when an app starts - especially if it needs to get some data from a web site first.

As far as following Apple HIG - take a look at the (MobileMe) iDisk app; until you register your member details the app shows a typical uitableview Default.png before very quickly showing a fullscreen view.

Upvotes: 0

Rahul Vyas
Rahul Vyas

Reputation: 28720

simply use sleep(time in seconds); in your applicationDidFinishedLaunching method

Upvotes: 3

wcochran
wcochran

Reputation: 10896

Here is my simple splash screen code. 'splashView' is an outlet for a view that contains an image logo, UIActivityIndicator, and a "Load.." label (added to my 'MainWIndow.xib' in IB). The activity indicator is set to 'animating' in IB, I then spawn a separate thread to load the data. When done, I remove the splashView and add my normal application view:

-(void)applicationDidFinishLaunching:(UIApplication *)application {
    [window addSubview:splashView];
    [NSThread detachNewThreadSelector:@selector(getInitialData:) 
                                 toTarget:self withObject:nil];
}

-(void)getInitialData:(id)obj {
    [NSThread sleepForTimeInterval:3.0]; // simulate waiting for server response
    [splashView removeFromSuperview];
    [window addSubview:tabBarController.view];
}

Upvotes: 2

Adeem Maqsood Basraa
Adeem Maqsood Basraa

Reputation: 193

For stylish splash screen tutorial check out this http://adeem.me/blog/2009/06/22/creating-splash-screen-tutorial-for-iphone/

Upvotes: 0

nevan king
nevan king

Reputation: 113757

I needed to do this to block showing a table view until the data was loaded over the network. I used a variation of one I found here:

http://michael.burford.net/2008/11/fading-defaultpng-when-iphone-app.html

In the interface of your App Delegate:


@interface AppDelegate : NSObject 
{
  UIImageView *splashView;
}

In the implementation:


@implementation AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {


  // After this line: [window addSubview:tabBarController.view];

  splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
  splashView.image = [UIImage imageNamed:@"Default.png"];
  [window addSubview:splashView];
  [window bringSubviewToFront:splashView];

  // Do your time consuming setup

  [splashView removeFromSuperview];
  [splashView release];
}

Make sure you have a Default.png in the resources

Upvotes: 21

Shannon A.
Shannon A.

Reputation: 786

Write an actual splash screen class.

Here's a freely usable splash screen that I recently posted in my iPhone in Action blog: http://iphoneinaction.manning.com/iphone_in_action/2009/03/creating-a-splash-screen-part-one.html

Upvotes: 1

Becca Royal-Gordon
Becca Royal-Gordon

Reputation: 17861

Make your app take longer to load.

In all seriousness, Paul Tomblin is correct that this usually isn't a good idea. Default.png is a mechanism intended to make your app appear to load faster by holding an "empty" screenshot. Using it for a splash screen is a minor abuse, but intentionally making that splash screen appear for longer than it needs to is almost sick. (It will also degrade your user experience. Remember, every second the splash screen is visible is a second that the user is impatiently staring at your logo, swearing they'll switch to the first decent competitor they can find.)

If you're trying to cover for some sort of secondary loading--for example, if the interface has loaded and you're just waiting to get some data from the network--then it's probably okay, and Ben Gottlieb's approach is fine. I'd suggest adding a progress bar or spinner to make it clear to the user that something really is going on.

Upvotes: 3

Paul Tomblin
Paul Tomblin

Reputation: 182782

Read the Apple iPhone Human Interface Guidelines (HIG). The "splash screen" isn't supposed to be for branding or displaying a logo, it's supposed to look like the default condition of the app so it appears to start up quickly.

Making it stay there for longer would be a violation of the HIG.

Upvotes: 37

Ben Gottlieb
Ben Gottlieb

Reputation: 85532

The simplest way to do this is to create a UIImageView who's image is your Default.png. In your applicationDidFinishLaunching: method, add that image view to your window, and hide it when you'd like your splash screen to go away.

Upvotes: 26

Related Questions