How to implement a set of settings for a custom keyboard?

I am writing my own keyboard. And I donโ€™t know how to connect my set of settings (settings in the phone) using the keyboard extension, so if someone changes the settings from the settings of the phone and after that open some text field to write something about which my the keyboard already knew what changes he made to the settings. I tried to create an application group to connect my application to my extension, and in my view the controller adds an observer for NSUserDefaultsDidChangeNotification something like:

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

When someone makes changes to the settings settingsDidChange: the method will be called, and I install everything that I need to read in my application group to read it from my extension. But this method will be called only when a person opens my application, so if someone changes the setting from the phone settings and does not open the application, my keyboard will not change. so how can i implement my preset for my keyboard?

+6
source share
3 answers

I have not worked with a keyboard extension yet, but I ran into a similar issue with the Today and WatchKit extensions. NSUserDefaultsDidChangeNotification only posts in the same process as making the change. Thus, a change within the settings of the iPhone application does not cause this notification to observers during the expansion process.

However, Darwin notifications ( CFNotificationCenterGetDarwinNotifyCenter ) post messages in other processes.

Here's the gist of catching NSUserDefaultsDidChangeNotification inside an iPhone application, sending Darwin notifications for extensions (s), capturing it inside an extension, and finally converting it to standard NSNotification for easier use: https://gist.github.com/phatblat/f640416c15e11b685511

Note that you cannot send userInfo in the Darwin notification, so you still need to provide the application group and create an instance of NSUserDefaults with initWithSuiteName: with your application group ID.

+1
source

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

The UIInputViewController subclass works when using the keyboard, and not just when starting the application. You can also just load NSUserDefaults inside the UIInputViewController every time you load a view, and this way you avoid using notifications. Essentially, you choose settings every time you boot the keyboard. Depending on what setting you have, this may or may not be optimal.

0
source

It seems that the error in iOS 8.0.2 is even on the device (I tested on iPad Air). Custom keyboard settings are not saved and cannot be read from the extension.

A solution requiring full access is described here: fooobar.com/questions/973323 / ...

0
source

Source: https://habr.com/ru/post/973321/


All Articles