7

I'm writing my own custom keyboard. And I don't know how to connect my settings bundle(setting in phone) with my keyboard extension, so if somebody change settings from phone settings and after that open some text field to write something my keyboard already knew about than changes he made in settings. I've tried to create app group to connect my application with my extension, and in my view controller add observer for NSUserDefaultsDidChangeNotification something like:

var notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: "settingsDidChange:", name: NSUserDefaultsDidChangeNotification, object: nil)

When somebody makes changes in settings settingsDidChange: method will be called and there i'm setting all things i need to read into my app group to read it from my extension. But this method will be called only when person open my application, so if somebody change setting from phone settings and won't open application my keyboard won't change. so how can i implement my settings bundle for my keyboard?

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36
  • 1
    There seems to be a problem with Keyboard Extension not listening to `NSUserDefaultsDidChangeNotification`. I'm having the same problem. Container app writes to userDefaults, Keyboard Extension reads it correctly, BUT neither `NSNotificationCenter` nor `NSUserDefaults`'s `addObserver` worked here. This is becoming a serious problem for us here. – bauerMusic Sep 11 '14 at 20:06
  • @bauerMusic did you somehow solve this problem? – Vasyl Khmil Sep 12 '14 at 07:16
  • No.. But, I guess I can do without (for now) since whenever the keyboard will come up again it will read from the updated `userDefaults`. Rereading your post, I believe your problem is that writing to `userDefaults` from the _iPhone's_ Settings is not taking affect? If so, it has nothing to do with notifications. My problem is that no kind of notification KVO worked. `userDefaults` gets written, but non of the listeners gets called. Read similar complaints from users copy/pasting code from a working extension (non keyboard) to keyboard and notification stops working. – bauerMusic Sep 12 '14 at 11:25
  • for anyone still looking to the solution: http://stackoverflow.com/questions/25121071/how-to-implement-settings-bundle-for-custom-keyboard has the gist example of how to receive notifications for NSUserDefaults changes between the app and the extension. The key is to use Darwin notifications – medvedNick Feb 17 '16 at 03:32

3 Answers3

1

I haven't worked with a keyboard extension yet, but ran into a similar issue with both a Today and a WatchKit extension. The NSUserDefaultsDidChangeNotification only posts in the same process that made the change. So, a change inside the iPhone app settings doesn't trigger that notification to observers in the extension process(es).

However, Darwin notifications (CFNotificationCenterGetDarwinNotifyCenter) do post to other processes.

Here's a gist of catching NSUserDefaultsDidChangeNotification inside the iPhone app, posting a Darwin notification for the extension(s), catching it inside the extension and finally converting it to a standard NSNotification for easier consumption: https://gist.github.com/phatblat/f640416c15e11b685511

Note, that you can't send any userInfo in a Darwin notification, so you still need to provision an app group and stand up an NSUserDefaults instance with initWithSuiteName: with your app group identifier.

phatblat
  • 3,804
  • 3
  • 33
  • 31
0

In your class KeyboardViewController: UIInputViewController of your keyboard itself, you should add the notification code that you wrote. Where are you writing it now?

It is the UIInputViewController subclass that runs when the keyboard is used, not just when the app is run. Also you might want to just load the NSUserDefaults stuff inside the UIInputViewController every time the view loads, and this way you avoid having to use notifications. Essentially you're picking up the settings every time the keyboard view is loaded. Depending on how much customization you have, this may or may not be optimal.

Rikkles
  • 3,372
  • 1
  • 18
  • 24
0

It seems to be a bug in iOS 8.0.2 even on the device (I've tested on iPad Air). Custom keyboard setting are not saved and cannot be read from extension.

Solution that requires full access explained here: https://stackoverflow.com/a/26172466/840742

Community
  • 1
  • 1
Renatus
  • 1,123
  • 13
  • 20