Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared UserDefaults between app and extension not working correctly

Tags:

So I've been looking around and following all the steps to setup shared UserDefaults correctly but I should be missing something.

I have App Groups capability activated on both my app and my extension. Both use the same suite name ("group.TestSharedPreferences") and I write this way:

struct Preferences {     static let shared = UserDefaults(suiteName: "group.TestSharedPreferences")! } 

On viewDidLoad:

Preferences.shared.set(1, forKey: "INT") 

And to read:

Preferences.shared.integer(forKey: "INT") // Returns 1 in Container App Preferences.shared.integer(forKey: "INT") // Returns 0 in Today Extension 

Even using synchronize() just after setting "INT", the value retrieved in the extension is not the one saved in the container App. Any ideas on what might I be missing? Thank you!

like image 495
iDec Avatar asked Oct 16 '16 00:10

iDec


People also ask

What is NSUserDefaults?

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.

Where are UserDefaults saved?

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?

Currently, there is only a size limit for data stored to local user defaults on tvOS, which posts a warning notification when user defaults storage reaches 512kB in size, and terminates apps when user defaults storage reaches 1MB in size. For ubiquitous defaults, the limit depends on the logged in iCloud user.

Is NSUserDefaults encrypted?

NSUserDefaults is easy to incorporate into your application and unfortunately, that means it is frequently misused by developers of all skill levels. Because NSUserDefaults stores all data in an unencrypted .


1 Answers

I would recommend to dig down step by step here.

First, make sure that both the main app and the widget extension have app group capability enabled and use the same and activated (the checkmark must be set) app group name:

Main App: Main App

Today Widget Extension:

Today Widget Extension

Then make a simple test with direct set/get access. In your main app's AppDelegate.didFinishLaunchingWithOptions method (change the app group name and the keys to your needs):

if let userDefaults = UserDefaults(suiteName: "group.de.zisoft.GPS-Track") {     userDefaults.set("test 1" as AnyObject, forKey: "key1")     userDefaults.set("test 2" as AnyObject, forKey: "key2")     userDefaults.synchronize() } 

In your Today Widget Extension's ViewController:

if let userDefaults = UserDefaults(suiteName: "group.de.zisoft.GPS-Track") {     let value1 = userDefaults.string(forKey: "key1")     let value2 = userDefaults.string(forKey: "key2")     ... } 

If this works, the problem must be related in your Preferences singleton.

like image 83
zisoft Avatar answered Sep 17 '22 17:09

zisoft