Communicate with Custom Keyboard extension with Ho

2019-04-12 16:59发布

问题:

I am working with custom keyboard extension. that almost done but i just facing issue with device when i communicate data with extension and my host app that not woking in device but same thing this working in simulator. My code is following:

HostApp View controller:

 - (void)viewDidLoad {

    _defaultvalue = [[NSUserDefaults alloc] initWithSuiteName:@"group.myapp.myappname.targetKeyboard"];
    [_defaultvalue setBool:YES forKey:@"Layout"];
    [_defaultvalue synchronize];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)RightAction:(id)sender {


    _defaultvalue = [[NSUserDefaults alloc] initWithSuiteName:@"group.myapp.myappname.targetKeyboard"];
    [_defaultvalue setBool:YES forKey:@"Layout"];
    [_defaultvalue synchronize];

}

- (IBAction)leftAction:(id)sender {

    _defaultvalue = [[NSUserDefaults alloc] initWithSuiteName:@"group.myapp.myappname.targetKeyboard"];
    [_defaultvalue setBool:NO forKey:@"Layout"];
    [_defaultvalue synchronize];
}

And i check this BOOL value in my extension target using following code and change keyBoard layout according to my need:

-(void)LoadKeyboardview
{

_defaultvalue = [[NSUserDefaults alloc] initWithSuiteName:@"group.myapp.myappname.targetKeyboard"];

        if([_defaultvalue boolForKey:@"Layout"])
        {
            self.ObjKeyLayout=[[[NSBundle mainBundle]loadNibNamed:@"keyboardLayout" owner:nil options:nil]objectAtIndex:0];
            [self addGesturToKeyBoard];
            self.inputView =self.ObjKeyLayout;

        }
        else{

            self.ObjKeyLayout=[[[NSBundle mainBundle]loadNibNamed:@"leftLayout" owner:nil options:nil]objectAtIndex:0];
            [self addGesturToKeyBoard];

            self.inputView =self.ObjKeyLayout;

        }

}

That same code working well in simulator ios8 device iPhone5. please is there any more code needed for this i read in apple doc that said NSUserDefaults initWithSuiteName help me in this thank you.

UPDATE:

I have already set RequestsOpenAccess in plist and at setting->keyboard-customkeyboar->Full Access ON

My extention Plist:

回答1:

I got solution that i create new provisional profile and enable app-groups and setting app group from my Xcode project from target-->capability--Expand App Group and by tap on + add app group like following screenshot:

And doing same thing for my keyboard extension target. Then i used this setting app groups identifier in NSuserDefault:

 _defaultvalue = [[NSUserDefaults alloc] initWithSuiteName:@"group.keyboaredExtention"];
     [_defaultvalue setBool:NO forKey:@"Layout"];
    [_defaultvalue synchronize];

now that working fine in my device and simulator. hope that helps to others.



回答2:

In iOS8, a custom keyboard and the containing app can communicate using a shared container.

This is achieved by asking the user to allow full access, which is a setting in the iOS settings app under General->Keyboard->your keyboard->Full Access (ON/OFF).

You should add this setting to your keyboard's Info.plist:

RequestsOpenAccess

For further info, read Apple's iOS8 Custom Keyboards API Documentation: https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html#//apple_ref/doc/uid/TP40014214-CH16-SW11