views:

124

answers:

1

Hi,

I am working on an App for iphone OS 3.1. In the app I have 'settings' tab where user can update some values which are effective when the app terminates and re-launches.

I have now built this app with Base SDK set to 'Device 4.0' and the deployment target to 'iOS 3.1', since I do not want to loose the iOS 3.1 user base.

Running this version of the app on iphone simulator 3.2, I do not have any issues.

To test the app for the situation where an iPhone OS 3.1 has been upgraded to iOS4, I run this version on Device 'iPhone' and Version '4.0' in iphone Simulator. Due to Application Multitasking in iOS4, my app does not 'terminate' when I press the 'Home' button, instead (as expected) it goes in the background and pressing the app icon, it comes back to foreground.

All this is fine, the issue arrises when the user changes something in the settings within the app, pressing the 'home' will not terminate the app....and hence the new settings will not be effective as the app will not re-launch but will only be coming back in the 'foreground' .... in fact the app may never terminate unless user specifically terminates the app.

Without using the iOS4 APIs how do I identify when the app is 're-launched' and when it is coming in 'foreground', so that I may force the settings to be re-read when app is moving in 'foreground'?

Thanks in advance.

+1  A: 

In order to handle changes in the settings for your application while in the background, you will want to listen for NSUserDefaultsDidChangeNotification and respond to it by updating the portions of your application affected by the changed settings. When your application reenters the foreground, it will receive this notification if any of the settings for the application have changed while it was suspended.

For example, you can listen for this notification in one of your controllers using code like the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleChangeInUserSettings:) name:NSUserDefaultsDidChangeNotification object:nil];

where -handleChangeInUserSettings: is a method where you perform the updates needed to reflect the changed user settings.

Brad Larson
Hi Brad, apologies for coming back on this after a long time....Does that mean I will need to use iOS4 method UIApplicationDidEnterBackgroundNotification? As I am targeting the app for iOS3.2 and up, I do not want to use any of iOS4 methods. Also, I am not sure but wouldn't NSUserDefaultsDidChangeNotification will notify for setting changes done via iPhone's settings app? While I have settings tab within the app.
prd
@prd - NSUserDefaultsDidChangeNotification is a string constant, not a method, and has been present since iPhone OS 2.0. You don't need to be aware of your application entering the background, because as soon as it returns to active status it will receive all of the notifications that it otherwise would have while it was paused in the background. It will be triggered any time you update the NSUserDefaults, so it will be called if you change settings within your application. Because of that, I'd recommend placing all of your change-sensitive code in the -handleChangeInUserSettings: method.
Brad Larson