0

I have a custom keyboard extension. And I need to know when my keyboard is shown/hidden. I read this article, this block:

- (void)registerForKeyboardNotifications
{

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{

}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{


}

But this code implements into app. The addObserver argument self means it. So, how can I get an object from my host app (it could be any app with text field) to pass for register notifications?

Or is three other options to register if keyboard is shown/hidden?

isfirstResponder also from extension - not an option, i suppose.

veitr
  • 111
  • 10

3 Answers3

1

This is same problem I faced with same problem when I was working with Extensions. I searched in lot of places and found out we can send from extension to the main app,

You can use it using CFNotificationCenterGetDarwinNotifyCenter

Here is link of blog I wrote related to this https://medium.com/@saberjack/ios-sending-notifications-between-your-apps-3fe7422d6a41

Hope this helps

Pavan
  • 443
  • 5
  • 21
0

This is maybe really too late for me to answer, but you can't get that information from NSNotifications, actually I guess this code won't ever work, because most of the NSNotifications are disabled inside the sandbox of the keyboard extension.

For example, I tried to use the OnCopyClipboard notification, but this never worked for me. Did you get this running? I'm really interested in those results.

Sorry I have to say no, this is because keyboard extension is well known for the sandbox.

Paul Oostenrijk
  • 659
  • 8
  • 16
0

You need to enable App Groups for both targets to be able to share NSUserDefaults between your extension and the containing app, here's a small guide:

  1. In Project Navigator,in the upper left corner click on the tiny arrow to show list of targets.
  2. Click on YOUR_PROJECT_NAME_TARGET, switch to Capabilities in the top menu, and scroll down to the App Groups.
  3. Turn on the App Groups switch.
  4. Select your Development Team to use for provisioning and click Choose (This will add the “App Groups” entitlement to your App ID).
  5. click + to add new App Group and call it group.YOUR_COMPANY.YOUR_TARGET_NAME
  6. Xcode will now add “App Groups containers” entitlement to your App ID. If everything went as it should, the new App group should be there and it should be checked. enter image description here
  7. Now you need to repeat these steps for the Extension target. (replace YOUR_PROJECT_NAME_TARGET with YOUR_EXTENSION_NAME_TARGET),this time you’ll use the same created App Group instead of creating new one and the same development team as in previous steps .

Back to Code : 8. In your Container App, the choose the view controller you want to share data with your extension and add this code block:

NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"YOUR_GROUP_NAME"];
[sharedDefaults setInteger:[self.textField.text integerValue] forKey:@"MyNumberKey"];
[sharedDefaults synchronize];
  1. Here, we just alloc/init new NSUserDefaults with our App Group, and store the number entered inside the textField under MyNumberKey.

In the Extension View Controller :

  1. In initialisation Method, add this code Block [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil];
  2. When you want to access your shared data : NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"YOUR_GROUP_NAME"]; NSInteger number = [defaults integerForKey:@"MyNumberKey"];

DONE !

Meseery
  • 1,845
  • 2
  • 22
  • 19