How can I display a splash screen for a longer period of time than the default time on an iPhone?
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()
}
}
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];
}
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.
just make the window sleep for some seconds in applicationDidFininshLaunchings method
example: sleep(3)
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.
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))