Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the settings.bundle from today-extension

In the app I can choose in the settings which language of three should be used. If nothing is selected, the iPhone language is detected and selected for english, french or german. If none of this languages is the iPhone language, english is set to be used. Manually changing the language in the settings works as it should. Now I added a today-extension which works nearly well but I need to access the settings-bundle to get the NSUserDefaults for the languages (if manually changed). In both targets I activated the App-Groups with

group.com.companyname.appname

and selected it.

In the app I get the language with

NSString *manualLanguageSet = [[NSUserDefaults standardUserDefaults] valueForKey:SPRACHWAHL];

and in the today-extension I try to get it by:

NSString *manualLanguageSet = [[[NSUserDefaults alloc] initWithSuiteName:@"group.com.companyname.appname"] valueForKey:SPRACHWAHL];

For NSLog(@"Settings-Sprache: %@", manualLanguageSet); when running the today-extension the result is (doesn't matter if and which language is selected in the settings)

2014-09-13 16:48:36.331 HdB today[3734:284836] Settings-Sprache: (null)

What can I do it correct / how can I access to the settings (settings.bundle)?

like image 257
Christian Avatar asked Sep 13 '14 15:09

Christian


2 Answers

If you create a WatchKit preferences bundle, it also contains the key

ApplicationGroupContainerIdentifier

at the root level of dictionary of the bundle's Root.plist, with the value being the group identifier (what you provide to NSUserDefaults as suiteName).

I found that you can also use this key in the iOS settings bundle. If added, preferences will go to the respective container.

I verified this to work on iOS 8.3 across the iOS Settings app, the iPhone app and the WatchKit extension.

UPDATE: Apple lists this key as supported in iOS 8.2 and later at https://developer.apple.com/library/ios/documentation/PreferenceSettings/Conceptual/SettingsApplicationSchemaReference/Articles/RootContent.html

like image 99
Felix Schwarz Avatar answered Oct 13 '22 18:10

Felix Schwarz


I'm searching for it as well. The only way i know how to do it is via SharedContainer. In your example you should open your

[[[NSUserDefaults alloc] initWithSuiteName:@"group.com.companyname.appname"] valueForKey:SPRACHWAHL];

in container app and then copy the language value from NSUserDefaults of container app like this :

[[[NSUserDefaults alloc] initWithSuiteName:@"group.com.companyname.appname"] setObject:[NSUserDesaults standardUserDefaults] objectForKey:SPRACHWAHL] forKEY:@"Your Key in extension"]];

then you will be able to access your shared container in you extension and get those values.

If someone have a better way of doing it i would be happy to know as well.

like image 2
Piotr Avatar answered Oct 13 '22 19:10

Piotr