7

I am having issues communicating between my host app and its extensions through communicating NSUserDefaults changes.

I initialized NSUserDefaults using init(suiteName:), added KVO observer using the addObserver(...) method and overrided the method observeValueForKeyPath(...) but the method observeValueForKeyPath(...) is not called when I change the value corresponding to the observing key. It will be greatful if you help me to resolve this.

PS:here suite name is App groups name and NSUserDefaults created using suiteName as group identifier will be inside the private area for app groups.

Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
Rafeek
  • 523
  • 3
  • 16
  • http://stackoverflow.com/questions/5714161/nsuserdefaults-and-kvo-issues – Daniel Galasko Feb 26 '15 at 11:04
  • Hi @DanielGalasko. If its a `standardUserDefaults` KVO works fine but in my case its different Its shared among apps and `userdefaults` entry created in a separate location ie. created a separate bundle for group and created `userdefault` file there and sharing among apps. Its same as inter - App communication. – Rafeek Feb 26 '15 at 13:14
  • Then you can't use KVO but need to use the notification that's posted across the suite:) – Daniel Galasko Feb 26 '15 at 13:25
  • There is no `DistributedNotificationCenter` in iOS its only available in OSX. – Rafeek Feb 26 '15 at 13:46
  • 1
    see this post - http://stackoverflow.com/questions/28284989/nsuserdefaultsdidchangenotification-and-today-extensions – Daniel Galasko Feb 26 '15 at 13:50
  • We can register for `NSUserDifaultDidChageNotification` but that will notify only the current app if the current made any changes so It can't be applicable to app groups. If any app that is included in the `app group` made any changes , the other apps included in the `app groups` wont get notified. – Rafeek Feb 26 '15 at 13:50
  • 1
    Thats why you should look at MMWormhole and Darwin notifications. Alternatively you could just check if any of the keys have changed when your app becomes active:) – Daniel Galasko Feb 26 '15 at 13:53
  • @DanielGalasko: Thanks dude. I believe that will works for me. :) – Rafeek Feb 26 '15 at 13:56
  • anytime! I posted a solution so people don't need to peruse these comments:) – Daniel Galasko Feb 26 '15 at 14:07
  • Possible duplicate of [How to use KVO for UserDefaults in Swift?](https://stackoverflow.com/questions/43963220/how-to-use-kvo-for-userdefaults-in-swift) – pkamb Oct 13 '21 at 17:45

2 Answers2

3

Just tested, for iOS version later than 10.0, the KVO of the UserDefatuls works perfect fine across process.

angli1937
  • 53
  • 4
2

As of iOS 10 you can use KVO on User Defaults.

This has already been answered around here so I will not re-address it.

Old Answer (iOS 9 and older)

The short answer is that you can't use KVO or even NSNotificationCenter on NSUserDefaults to communicate changes between an App Extension and the containing App.

There's a great post by Atomic Bird that looks at the ways of coordinating communication. In particular its interesting to look at his analysis of communicating user defaults changes:

A possible alternative for app/extension notifications is to use the Darwin notification center via CFNotificationCenterGetDarwinNotifyCenter, which actually is a little bit like NSDistributedNotificationCenter. There's some discussion of this by Wade Spires at Apple's dev forums site.

I say "possible" because I'm not 100% confident of this continuing to work. In the documentation for this method, Apple notes that

An application has only one Darwin notification center, so this function returns the same value each time it is called.

So although this is apparently legal, it also sounds a lot like it violates the philosophy inherent in app extension restrictions, viz, that they can't access anything from the hosting app. This is why [UIApplication sharedApplication] is off limits in extensions. I can't help wonder if allowing CFNotificationCenterGetDarwinNotifyCenter is an oversight that might get "fixed" at some point.

So I guess for now a good solution might be to use MMWormhole as they implement the above solution.

Your other option is to use to check the user defaults every time your App becomes active and confirm whether any keys have changed, posting the relevant notifications etc.

Good luck

Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97