I work on a project on iPhone iOS 4 with Xcode 4.
I have a view with a UIButton. The title of the UIButton is set in viewDidLoad. So when app starts, the Button has a title.
However, my app has a Settings bundle and the button title can be also changed in Settings app. So I clic on the home button, my app quits, I go to the Settings app, and set a new button title.
When I quit Settings and restart my app, the button title is not refreshed, it is the old button title. Everything is as I had left.
Only if I turn off the iPhone, then turn on and relaunch my app (i.e. at full restart) the button has at the new title.
How to make sure that the button title changes when the app is (not full) restarted?
Thank you.
You want to update the settings in applicationDidEnterForeground. That gets called when it resumes.
Edit: Had a typo, it should be
- (void)applicationWillEnterForeground:(UIApplication *)application
see docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
I was mistaken in thinking that the viewWillAppear message would be sent to the ViewController when the app became active again after switching back from the Settings app. There is a notification posted when the app becomes active, however, so you should be able to accomplish what you want by registering your ViewController to receive UIApplicationDidBecomeActiveNotification notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refreshButtonTitle:) name:UIApplicationDidBecomeActiveNotification object:nil];
and then create the appropriate method in your ViewController to be called when the notification is fired:
- (void)refreshButtonTitle:(NSNotification *)notification {
// update the title of your button here
}
You can register to receive NSUserDefaultsDidChangeNotifications and then act accordingly to update your UI.
Related
If I get an alert in my application and if I put application in background and press application icon to enter the application, a splash screen is displayed and then the alert pop up. Why splash screen appears?
And if alert is not present and I put application in background and press application icon to enter the application, splash screen is not displayed.
First you have to make the UIAlertView a property in your class.
In your AppDelegate Class you can implement the applicationDidEnterBackground: Method in wich you can put something like this:
[yourViewController.yourAlert dismissWithClickedButtonIndex:0 animated:NO];
This should dismiss the alert if your app enters the background.
Hope this helps!
The splash screen is as #akshay1188 mentions, is the Default.png in your project file. The reason for it being displayed, based on my best assumption, is because the OS has not managed to take a screenshot of your App before you go back to it. See this answer to a StackOverflow question where it was discussed.
As for the UIAlertView, #pKoul's anwser got my upvote.
Maybe you can use the notification posted UIApplicationDidEnterBackgroundNotification in any class that needs some cleanup before entering bg. Do not forget to remove the observer in dealloc.
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(cleanup:)
name: UIApplicationDidEnterBackgroundNotification
object: nil]
The splash screen is actually the launch image that you might be using as Default.png
If you want to dismiss all AlertViews programmatically you have to Remember a Reference to the currently shown Alterviews. I recommend a Singleton class where you ask for an AlertView and wich saves a Reference to the AlertView.
then you could use the
`- (void)applicationDidEnterBackground:(UIApplication *)application`
in your AppDelegate and call a Function on the Singleton Class, wich itself calls
[alert dismissWithClickedButtonIndex:0 animated:YES];
at all Alertviews.
I have a button ON/OFF in my viewcontroller that plays some music when it is turned on by the user. Now if the user clicks the iPhone home button and re-launches my app again, the button is shown as "ON" but there is no music playing. So the user has to hit ON-OFF-ON again for music to start playing again.
Anyone know how can I call my ON/OFF view controller button so that I can set it to OFF when app enters background and set it to ON and play the music when it enters forground in these app delegates?
I know I need to write to a plist file the button & music state on applicationDidEnterBackground. I don't know how can I get to those action from appdelegate since they are defined in my viewcontroller.
Similary, when the app enters the foreground I will read the saved plist file and then set the state of music and button again. Again, I don’t know how to call my controller methods from delegate.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Inside applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Inside applicationWillEnterForeground");
}
One possible methodology is to send a message from the app delegate to the view controllers to save or restore state. Then the view controller, which have access to their internal state, can implement any needed save and restore methods.
If the app delegate does not directly contain a reference to the active view controller, it can send the message down the chain, to root view controller, which can then send a message to the next view controller, and etc., each controller saving anything it considers important on the way down.
All you need to do is subscribe to the UIApplicationDidEnterBackgroundNotification notification in your view controller
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleEnteredBackground:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
Theres also a notification for DidEnterForeground too.
In my app I have a button, when the user clicks it, it brings up a modal view controller which gets the user's location (with a UIActivityIndicator and image on). When it has there location it will send them off to another application.
This works well on older devices (without multi-tasking) because the app simply re-launches back to it's previous state after I go back to it.
However on devices that have multi-tasking, when I resume the app, the view that tells the user it's getting their location is still there - which is not at all desired.
My first idea was to set a BOOL when it was about to fire them off to the other app, and then in the viewDidAppear, if the BOOL is true, dismiss the modal view.
That would work, if viewDidAppear got called when an app resumes active. As I have just learnt, it doesn't.
Is there a method that view controllers can respond to when the app resumes active to that view? Or, will I have to set up delegation etc with the app delegate? If so, can you explain how I would do that?
You can send a notification when the app becomes active and listen to it into your modalviewcontroller. I think this is the easiest way.
// Into the app delegate
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"ApplicationDidBecomeActive" object:nil];
}
// Into your modal view controller register it for the given notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationDidBecomeActive:) name:#"ApplicationDidBecomeActive" object:nil];
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
//...
}
Otherwise you can disable the background mode of your application by setting the "UIApplicationExitsOnSuspend" key to YES in the info.plist file.
How to know the termination of app?
I added this code in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification
object : nil];
and if the app ends, it will notify me of termination.
- (void)applicationWillTerminate:(NSNotification*) notif{
NSLog(#"program will end");
}
But it doesn't work...
I terminated the app, by clicking home button and pressing home button in 2sec, followed by clicking app icon's '-'button.
I want to be notified of the termination of app.
And also intereseted in what function to be called when the app terminates.(is it viewDidLoad?)
And the termination of app by clicking the '-'button is to send the app SIGKILL?
I terminated the app, by clicking home button and pressing home button in 2sec, followed by clicking app icon's '-'button.
If you press the home button, your app will be sent to the background. When you then kill it (by pressing the - button), it likely does not get the notification because it is not running anymore.
applicationWillTerminate is called on iOS < 4.0, when no multitasking (background) is available or when UIApplicationExitsOnSuspend is set in the info.plist file.
On iOS 4.x, when your app is sent to the background, it receives applicationWillEnterForeground:. Look at the UIApplicationDelegate Protocol Reference for more info:
You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
Keep in mind that you need to do this both in applicationWillEnterForeground and applicationWillTerminate if you also support iOS < 4
Have a look also at this post from S.O.
That should work, did you try adding logging into the application delegate method applicationWillTerminate: method too?
Edit:
For iOS 4+ devices, you need to do whatever work for shutdown in applicationWillEnterBackground instead... that always gets called as you are being suspended and is the right time to do final work.
you should implement applicationDidEnterBackground and do any preparation for termination there. If your app is terminated in the background, you will receive no other notification.
I have an old app which did many UI initialization work in viewDidLoad of various views. In iOS4 home button just put the app in background so viewDidLoad won't get called when re-launching the app.
I don't want to put those initialization procedures in viewWillAppear as it is unnecessary to re-initialize the data every time the view appear.
How can I completely quit my app when user press the home button? or any simple way to reload the view controllers that sit inside a tabBarController?
thanks.
There is a key in your app's Info.plist file called UIApplicationExitsOnSuspend, set it to the boolean YES, which will essentially revert the home button's functionality to pre-iOS4 and completely exit your app when the home button is tapped.
Try disabling backgrounding. Apple has this in their documentation
Try to check.
UIApplicationExitsOnSuspend
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html
you have to look up to the UIApplicationDidBecomeActiveNotification i think, but in the uiapplicationdelegate-protocol, there are even some other interesting things for you
or perfectly for you:
UIApplicationWillEnterForegroundNotification as a new notification in ios4 for this problem