Crash At Splash Screen, iPhone - iphone

I have an app which when opened, displays a splash/loading screen. I had this set at 2.5 seconds before the app moved on.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
sleep(2.5);
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
I now want the app to pause at the splash screen for a minute (there is a very good reason for this) so I thought:
sleep(60.0);
could be used. When I use this though, my app opens and stays at the splash screen for about 20 seconds, before quitting/crashing back to the springboard.
Any idea why this is the case?
How should I do this?
Edit // It is worth noting both:
sleep(15.0);
and
sleep(19.0);
work.
sleep(20.0);
does not.
Solution // Do not use sleep, use timer. I followed tutorial here:
http://adeem.me/blog/2009/06/22/creating-splash-screen-tutorial-for-iphone/
Many thanks,
Stu

I'm purely guessing here, but it may be that, because you're blocking the main thread (using sleep instead of a timer), the iPhone OS is seeing that as an "unresponsive app" and killing it.
Check out NSTimer.

I agree with Joshua Nozzi, that the OS "thinks" that your app has crashed.
I'd remove the sleep() and do this instead:
[window performSelector:#selector(addSubview:) withObject:viewController.view afterDelay:60.0f];
[window performSelector:#selector(makeKeyAndVisible) withObject:nil afterDelay:60.0f];

If you look in your console you will probably see something like the following...
Warning: your application name failed to launch in time
Warning: Forcing crash report of your application name...
Warning: Finished crash reporting.
Basically, because you've put the main thread to sleep for too long the OS decides that the application failed to launch and forces the app to exit. You would be better of using a timer to do the delay so that the main thread remains active.

I would suggest you implement the Splash screen logic differently than the current cruel one :)
perhaps, you could create a UIView covers the whole screen, upon touch or after a timeout, self destructs (removeFromSuperview)??

Um, there is never a good reason for sleeping an iPhone app for 60s. Never. May your app in its current form never reach the App Store! :)

Have a look at this blog entry that describes how to create a splash screen that will fade out and you should be able to set the delay time for how log it will be visible. Look where the timer is created.
http://icodeblog.com/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/

[NSThread sleepForTimeInterval:0.85];
I think u can use this method.

Related

What mechanism checks the code for potential bugs?

If I put any code (eternal cycle like while(1) or sleep(xxxxx)) that potentially can hang out main thread:
- (void)viewDidLoad
{
[super viewDidLoad];
while (1) {
}
//[NSThread sleepForTimeInterval:10000];
}
I'm getting SIGABORT just after launch in approximately 50% of launches.
What is going on?
Though, if program is launched it is never (for at least 10 minutes) terminated as it should according to Synchronous Networking On The Main Thread. What about watchdog that should check application responsiveness and terminate the one that got stuck?
I presume that there's a question about this here but it doesn't contain meaningful answer though.
Tested both on simulator and device. The startup crash appears only on sim. Though the program is terminated neither on device nor on sim.
So there are actually two questions:
1. What about watchdog?
2. What is this startup crash on sim?
P.S. Sorry for my English I hope you've got what I mean.You're welcome to edit my post.
If your application is crashing then look at the code with the debugger. Add break point exceptions to the first view's -(void)viewDidLoad method as a starting point and see where it crashes. This normally exposes bugs quickly.
I suggested viewdidLoad as it is called the moment before the view comes to screen. If your very first screen is crashing - I'd start with its' viewDidLoad method.

applicationDidEnterBackground & applicationWillResignActive are not being called

I am creating a simple application which perform some task on main thread. I am printing process in NSLog so I can understand that my process is running or not.
Now when I press home button without starting the process (Process will be start when I tap on a button) application enters in background and my both of methods applicationDidEnterBackground & applicationWillResignActive are being called.
But when I first tap on my button and process starts on main thread after that if I press home button none of these two method being called. So my application can't know that app entered in background or not.
Even after that when I again active the app it shows me a black screen with status bar only.
Why this is happening?
Why app not entering in background?
Why apple's methods not being called?
Is there a way to solve it?
UPDATE
Here is my appdelegate class code
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
All methods have no implementation.
Thanks in advance.
I am creating a simple application which perform some task on main thread.
Don't perform long-running operations on the main thread.
The delegate callbacks happen on the main thread. If the main thread is busy, then the callbacks won't happen until you return to the "run loop".
When foregrounding your app, the OS actually displays a screenshot if available, falling back to the launch image (Default.png). The screenshot is taken after -applicationDidEnterBackground: returns, which allows you to customize what gets saved (you might want to do this for security reasons, or to hide UI elements which might not make sense to show when relaunching e.g. a countdown timer).
The black screen is probably because your app has no launch image. If your app takes more than about 10 seconds to enter the background (and it does, since the main thread is blocked), it gets killed. Except the debugger is attached and catches SIGKILL, so it's easy to miss unless you're watching Xcode.
there are some cases
if UIApplicationExitsOnSuspend key set to true in your app's Info.plist, the applicationWillResignActive method is not called when the user hits the home button. and may b some thing other. check keys here Apple keys and see if something new you added to plist. and there is no other case that you say your delegate method not calling. it may also some time due to project in appropriate behavior. try cleaning your project and rebuild.
this is going to sound strange but for those it helps. I had the same issue and cleaned my project and then it started working again.

App crashing in splash screen in iPhone 3G second time when i open

I'm developing an application for both iphone 3G and iPhone4,if i test my app in iPhone 4 everything works fine but if i test my app in iPhone 3G app works fine but if i click home button of my iPhone and soon if i click my app icon only splash screen shown and while showing splash screen only it is crashing if i wait for about 30 seconds after i close it is working, since in iPhone 4 it is having multitasking and more memory if i do the same process the app is not crashing.
I don't know what is the problem is this the problem of my code? or iPhone? or i need to handle any of appdelegate methods like
1.applicationDidBecomeActive:
2.applicationWillResignActive:
3.applicationDidEnterBackground:
4.applicationWillEnterForeground:
5.applicationDidFinishLaunching:
Any help is appreciated in advance.Thank You.
Make sure you are not doing any heavy processing in the following methods
applicationDidBecomeActive
applicationDidFinishLaunching
applicationWillEnterForeground
iOS will terminate your application if its taking time in the above methods, so keep these methods clean and do the processing in your RootViewController etc
I advise you using breakpoints during the following methods to know exactly what is happening in your program:
applicationDidBecomeActive
applicationDidEnterBackground
applicationDidFinishLaunching
viewDidUnload (and all previous methods just before the unloading of the
last view before you press your home button)
Allover your MainDelegate
Try making the application run in the background and see what happens.
Since the iPhone 3G is slower it is still closing your app when you try to start it again.
That it is not closed directly could be because you are doing some thing that take a lite longer then you would expect.
It is not really crashing, what you see it just the app closing it self. that is why if you try it after some second it will work.
Mainly these could be - (void)applicationWillTerminate:(UIApplication *)application or - (void)applicationDidEnterBackground:(UIApplication *)application.
Just make sure you exit you app as some a posible, by for example save any data before the user will close the app.

iPhone 4: when to save data?

I have an app (a game) which saves data: game state, high scores, achievements, etc. Currently the app delegate does this on applicationWillTerminate:. After playing around with iPhone 4 for a bit, it seems that applications pretty much never terminate: they just run in the background forever, unless the user goes out of their way to quit them, or restart the phone.
So my question is, should I find another place to save my data, and if so, when?
To minimize the amount of time spent in the delegate method call, you should find a place that makes sense to save during the game (level completion, checkpoints, etc). You can also add a new delegate method to your application delegate which will be called when your application transitions to the background where you can duplicate some of the things you may have done previously in applicationWillTerminate:. The new delegate method to implement is -applicationDidEnterBackground:.
You will also receive a notification that the user switched back to your app as applicationWillEnterForeground:.
you can do so in the views diddisappear delegate method
-(void)viewDidDisappear:(BOOL)animated
{
//CODE FOR SAVING
}
There are 2 App delegate methods you can use
applicationDidResignActive: //pausing the app, used when a msg comes up. if multitasking this wont help
applicationDidEnterBackground: // called in iOS 4 for when the app is in the background
you can see when it loads into the foreground using
applicationWillEnterForeground:
check out the reference for more info
You should save in applicationDidEnterBackground. Make sure to wrap your saving code with – beginBackgroundTaskWithExpirationHandler: and endBackgroundTask, since without that, you have less than a second (or something like that) before execution suspends.

iPhone. Shouldn't hitting the home button cause UIApplicationDelegate's dealloc to be called

I have put NSLogs in all my classes including my UIApplicationDelegate subclass. I am curious - and a bit nervous - about why I am not seeing them echo anything when I press the home button. I am running in the XCode simulator.
Since iPhone/iPad runs a single app at a time, doesn't hitting the home button discard all traces of the running app?
Thanks,
Doug
Chuck is correct, the dealloc's don't matter at that point. If you want to do something as your app is expiring, implement this in your app delegate class:
- (void)applicationWillTerminate:(UIApplication *)application {
// goodbye...
}
When an app is terminated, its memory is simply freed. Dealloc is not called, it does not pass go or collect $200. This is normal and intended.
You should implement -(void)applicationWillTerminate:(UIApplication*)application{
}
this method will get called when user hit home button.
You might also want to think about the additional multitasking support that has been announced. Obviously the details are still under NDA, but pushing Home does not necessarily stop your app.
And think about the various circumstances where your app may already be terminated quickly (e.g. if a call comes in and the system runs out of RAM). Do your cleanup with the application lifecycle messages.