Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing UserDefaults between extensions

Creating a Today widget and I am using UserDefaults(suiteName:) to persist some data. In the main application I am using UserDefaults.standard(). This can't be read (or can it?) by the extension which is why I use the suiteName: constructor.

Data that user persist to UserDefaults.standard() in the main app needs to be available in the extension.

At this time I am persisting to both so that the values can be shared

 UserDefaults.standard().set:...forKey:...  UserDefaults(suiteName:...)().set:...forKey:...  ... 

Question is should I drop UserDefaults.standard() all together and just use UserDefaults(suiteName:) in my application, or is this bad practice and if so why?

Edit: I am using an App group container. For clarification I am asking should I just replace standard() with suiteName: throughout my project?

like image 549
RyanTCB Avatar asked Aug 10 '17 08:08

RyanTCB


People also ask

Is UserDefaults synchronous?

Yes it is synchronous .

Where are UserDefaults stored?

Storing Data in User Defaults The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. At runtime, the UserDefaults class keeps the contents of the property list in memory to improve performance.

How much can I store in UserDefaults?

There is No Limit for storing values in NSUserDefaults..

Is UserDefaults secure?

UserDefaults and securityYou should never store any sensitive data in user defaults. This storage is not encrypted at all so if an app other than your own obtains access to your user defaults store, your user's data is compromised.


1 Answers

You cannot use UserDefaults.standard to share data between a host app and its app extension. You instead have to create a shared container with UserDefaults(suiteName:) to share data.

Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.

To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app.

After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences. To enable this sharing, use the initWithSuiteName: method to instantiate a new NSUserDefaults object, passing in the identifier of the shared group.

For more, refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

How to use App Groups: https://github.com/pgpt10/Today-Widget

Standard or SuiteName?

Use standard one for data that is only for Host App. Use suiteName for data that you want to share between Extension and Host App. Just don't persist the same data in both of them. Avoid data redundancy. Use both of them according to the context.

like image 196
PGDev Avatar answered Sep 28 '22 11:09

PGDev