热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何实现自定义键盘的设置包?-Howtoimplementsettingsbundleforcustomkeyboard?

Imwritingmyowncustomkeyboard.AndIdontknowhowtoconnectmysettingsbundle(settinginph

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:

我正在编写自己的定制键盘。我不知道如何将我的设置包(设置在电话中)和我的键盘扩展连接起来,所以如果有人从手机设置中改变设置,在打开某个文本框后,我的键盘就会比他在设置中所做的改变知道的更多。我尝试创建app组来连接我的应用程序和我的扩展名,在我的视图控制器中添加NSUserDefaultsDidChangeNotification的observer,比如:

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?

当有人在settings settingsDidChange中做了更改时,方法就会被调用,我将所有需要读到的东西都设置到我的app组中,从我的扩展中读取。但是这个方法只有在人们打开我的应用程序时才会被调用,所以如果有人改变了电话设置的设置并且不打开应用程序,我的键盘就不会改变。那么如何实现我的键盘设置包呢?

3 个解决方案

#1


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).

我还没有开发过键盘扩展,但遇到过类似的问题,包括a Today和WatchKit扩展。nsuserdefaultschangenotification只发布在进行更改的过程中。因此,iPhone应用程序设置中的一个更改不会触发对扩展过程中的观察者的通知。

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

然而,达尔文通知(cfnotificationcentergetdarwinfycenter)确实会发送到其他进程。

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

下面是在iPhone应用程序中捕获NSUserDefaultsDidChangeNotification的要点,它会发布一个关于扩展名的达尔文通知,在扩展名中捕获它,最后把它转换成标准的NSNotification以便更容易使用: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.

注意,您不能在Darwin通知中发送任何userInfo,因此仍然需要提供一个应用程序组,并使用initWithSuiteName:使用应用程序组标识符创建一个NSUserDefaults实例。

#2


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?

在你的类KeyboardViewController: UIInputViewController你的键盘本身,你应该添加你写的通知代码。你现在在哪里写?

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.

它是UIInputViewController子类,在使用键盘时运行,而不仅仅是在应用程序运行时。你可能还想每次视图加载时都在UIInputViewController中加载NSUserDefaults这些东西,这样你就不用使用通知了。基本上,每次加载键盘视图时,都要进行设置。取决于您有多少定制,这可能是最优的,也可能不是最优的。

#3


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.

在iOS 8.0.2中,它似乎是一个bug(我在iPad Air上测试过)。不保存自定义键盘设置,无法从扩展中读取。

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

需要完全访问的解决方案:https://stackoverflow.com/a/26172466/840742


推荐阅读
author-avatar
CL_LC的小屋花_344
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有