Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement settings bundle for custom keyboard?

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?

like image 540
Vasyl Khmil Avatar asked Aug 04 '14 14:08

Vasyl Khmil


1 Answers

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.

like image 165
phatblat Avatar answered Sep 25 '22 03:09

phatblat