How to add custom notification to iOS custom keyboard?

I added the Custom Keyboard extension inside my application and it works great.

I added a NSNotificationkeyboard extension to my class as follows:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardColor) name:@"keyboard_color"  object:nil];

Now I call this notification from my view controller class as follows:

[[NSNotificationCenter defaultCenter] postNotificationName:@"keyboard_color" object:self];

Now I have added my notification selection method to my keyboard extension class as follows:

-(void)changeKeyboardColor{
}

But it does not work. I am testing in a simulator and I do not know. How to check the keyboard extension in the simulator.

Thanks.

+4
source share
2 answers

developer.apple.com

enter image description here

group.XXXXX

, , .

enter image description here

, !

Target ,

enter image description here

enter image description here

.

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];

NSUserDefaults

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

//Objective-C

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

!

+2

, :

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

:

[[NSNotificationCenter defaultCenter] addObserver:self
  selector: @selector(changeKeyboardColorNotice:) 
  name: @"keyboard_color"  
  object: nil];
0

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


All Articles