对于开发者来说怎么开发扩展功能?
1.创建一个新工程,myExtension,下图是创建后的目录结构
这些文件都不用管里面不需要写代码,就是一个空工程
2.点击File->New->Target
3.可以看到有10个扩展功能(xcode7.3),xcode8更多
4.选择Custom Keyboard,以第三方输入法为例,填入jianpan
5.点Finish会弹框,点Activate
6.这时目录结构是:
7.KeyboardViewController.m这个文件里会自带一些代码,这是切换输入法的一个Button
- (void)viewDidLoad {
[super viewDidLoad];
// Perform custom UI setup here
self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
[self.nextKeyboardButton sizeToFit];
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.nextKeyboardButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.nextKeyboardButton];
[self.nextKeyboardButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[self.nextKeyboardButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
}
8. 运行代码时,按照下图设置,不运行空工程,只运行扩展
9.点击Run,会选择手机里的任意一个app来运行我们的扩展,这时候选择的app需要有输入框的,才能调起键盘
10.用Safari做测试,这时点击最上面输入框,切换键盘,还是没有
11.到设置页面->通用->键盘->添加新键盘,选择myExtension
12.重新Run一下,在KeyboardViewController.m打一个断点,切换键盘,可以看到程序停在了断点位置,跳过断点可以看到这个键盘只有一个切换按钮
13.在往界面上写按钮后一定要用自动布局,而且是全代码的否则写的按钮看不到
NSDictionary *views=NSDictionaryOfVariableBindings(butFront,butbehind);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[butFront(==26.5)]-5-[butbehind(==26.5)]" options:0 metrics:nil views:views]];