How to add custom notification in iOS Custom Keyboard?

蹲街弑〆低调 提交于 2019-12-01 06:43:16

Create App Group from developer.apple.com

Group name must be like group.XXXXX

Enable App Group if it's disable for both app id and extension app id.

Update to existing provisioning profiles if it's look Invalid!

Go to Target and mention group name which you have created

Go to Keyboard extension and mention same group name

Now you done with all required settings.

In your project store value in NSUserDefaults

 let userd: NSUserDefaults = NSUserDefaults(suiteName: "group.XXXXX")!
 userd.setObject("test11", forKey: "key")
 userd.synchronize()

//Objective-C

NSUserDefaults *userd = [[NSUserDefaults alloc]initWithSuiteName:@"group.XXXXX"];
[userd setObject:@"test11" forKey:@"key"];//set the object you want to share
[userd synchronize];

For retrieving NSUserDefaults value in Extension class

 let userd: NSUserDefaults = NSUserDefaults(suiteName: "group.XXXXX")!
 print(userd.objectForKey("key"))

// Objective-C

NSUserDefaults *userd = [[NSUserDefaults alloc]initWithSuiteName:@"group.XXXXX"];
NSLog(@"%@",[userd objectForKey:@"key"]);

Happy Coding!

The method for a notification should take a single parameter, the triggering notification:

-(void)changeKeyboardColorNotice: (NSNotification *) theNotice
{
  NSLog(@"In %s", __PRETTY_FUNCTION__);
}

And you need to add a colon to the selector:

[[NSNotificationCenter defaultCenter] addObserver:self
  selector: @selector(changeKeyboardColorNotice:) 
  name: @"keyboard_color"  
  object: nil];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!