IOS应用程序扩展Application Extension是什么(包括第三方输入法以及来电防骚扰)

对于开发者来说怎么开发扩展功能?
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]];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值