Reputation: 141
I created a language selection screen, where the user is able to select the preferred language of the app (iPad).
I commit the language change like this:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"fr"] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
It works perfectly, except that it needs the app to be restarted for the UI to take the new language setting. According to Apple's docs:
To detect when changes to a preference value occur, apps can also register for the notification NSUserDefaultsDidChangeNotification. The shared NSUserDefaults object sends this notification to your app whenever it detects a change to a preference located in one of the persistent domains. You can use this notification to respond to changes that might impact your user interface. For example, you could use it to detect changes to the user’s preferred language and update your app content appropriately.
So it looks like I need to register for the notification NSUserDefaultsDidChangeNotification and update my UI. Which class shoult listen to the notification? Any idea what the code look like? My app is based on SKScenes and Sprite Kit objects, instead of xib and UIViewControllers.
Upvotes: 1
Views: 475
Reputation: 19872
All notifications can be detected by watching NSNotificationCenter:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(userDefaultsChange:)
name:NSUserDefaultsDidChangeNotification
object:nil];
- (void)userDefaultsChange:(NSNotification *)notification {
//update UI to new language here
}
Addobserver when you create your class and remember you have to remove it when class is removed.
[[NSNotificationCenter defaultCenter] removeObserver:self];
Upvotes: 1