此次的
Today Extension
预研,让我以为本身还有不少的不足,由于还有不少东西都没有去仔细的去研究,之后接下来会继续再接再砺。git最后: 若是你有更好的建议或者对这篇文章有不满的地方, 请联系我, 我会参考大家的意见再进行修改, 联系我时, 请备注
Today Extension
, 祝你们学习愉快~谢谢~github
Cain(罗家辉)objective-c
zhebushimengfei@qq.com: 联系方式app
350116542: 腾讯QQ性能
Today Extension
是在iOS 8
以后所推出来的重大更新之一,在此以前, 或许有人看过部分App
就已经实现过这些功能,但那种实现方式是并非系统所提供的,因此在性能方面须要打个问号。学习
开始建立Today Extension测试
选择
Today Extension
ui
激活
Today Extension
atom
在建立好
Today Extension
时,Xcode
会自动建立一个对应的MainInterface.storyboard
文件,而且与Today Extension
的Controller
关联,打开MainInterface.storyboard
, 咱们会看到有一个内容为Hello World
的UILabel
,废话少说如今咱们来看看运行效果。spa
选择你须要关联启动的App
不要怀疑,就是这么简单的,
Today Extension
就这么出来了。
不过,骚年郎们别着急,只是展现个
Hello World
而已,别高兴得太早,接下来咱们讲重头戏,也就是应用App
与Today Extension
的数据交互,在此以前, 咱们须要打开两个服务。首先是主程序里的
再者呢,就是
Today Extension
里的
作完这两个操做以后,咱们会看到多出来的两个
证书
。PS:这个证书是收费的, 若是没有去申请,一个帐号能够免费测试10个证书,主应用1个,Today Extension插件1个,也就是说一个应用须要两个。
接下来以前,咱们要把
MainInterface.storyboard
给干掉,毕竟代码才是王道(我的观点,不喜勿喷)若是喜欢用Storyboard
的朋友,也有一个Storyboard版本的,后面再补上,废话就很少说了,上教程。找到
TodayI Extension
中的Info.plist
文件,看到这小样就在这,先留着先
手动添加
NSExtensionPrincipalClass
字段 并设为TodayViewController
(这个Controller
你能够本身指定,我这里为了方便,就直接拿Xcode
生成的)
如今咱们能够把
storyboard
这小样给删掉了
再运行,你就会看到整个
Today Extension
是空的了,只有一个空图标和一个标题。
主应用中,咱们须要设置一下
NSUserDefault
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.todayExtensionCodeExample"];
[userDefault setObject:@"tips" forKey:@"group.todayExtensionCodeExample.tips"];
}复制代码
如今咱们进入
TodayViewController
开始写代码了
interface TodayViewController ()@property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UILabel *tipsLabel; @end @implementation TodayViewController - (void)viewDidLoad { [super viewDidLoad]; /** * 设置Today Extension的Size * * @param 0 Today Extension的宽度是不可变的,因此这里随便给个0就行了 * @param 150 高度是可控制的,这里我给了一个固定值150 * * @return CGSize */ self.preferredContentSize = CGSizeMake(0, 150); /** * 初始化一个UIView,且设置它的属性 */ self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.contentView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.contentView]; /** * 初始化一个Label,而且设置它的属性 * */ self.tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 30)]; self.tipsLabel.text = @"这是一个测试代码"; self.tipsLabel.numberOfLines = 0; self.tipsLabel.textColor = [UIColor blackColor]; self.tipsLabel.backgroundColor = [UIColor redColor]; [self.view addSubview:self.tipsLabel]; /** * 获取主应用传过来的数据 */ NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.todayExtensionCodeExample"]; NSString *nickName = [userDefault objectForKey:@"group.todayExtensionCodeExample.tips"]; if (nickName) { NSString *message = @"今天XX又给你准备了不少惊喜哦,快去看看吧!"; self.tipsLabel.text = [NSString stringWithFormat:@"%@,%@", nickName, message]; } } /** * 该方法是用来设置Today Extension的偏移,默认会像左偏移 * * @param defaultMarginInsets UIEdgeInsets * * @return UIEdgeInsets */ - (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets { return UIEdgeInsetsZero; } /** * 该方法是用来刷新Today Extension数据的 * * @param completionHandler */ - (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler { // Perform any setup necessary in order to update the view. // If an error is encountered, use NCUpdateResultFailed // If there's no update required, use NCUpdateResultNoData // If there's an update, use NCUpdateResultNewData completionHandler(NCUpdateResultNewData); } @end 复制代码
首先,咱们须要添加
PS:这里的Identifier
,以及URL Schemes
。Identifier
和URL Schemes
是你本身定义的,不能与其余Application
的Identifier
、URL Schemes
相同,不然会形成冲突。
而后呢,咱们去到主应用的
AppDelegate.m
文件中添加方法
最后,咱们去到
TodayViewController
里补上对应的方法就行了
PS:在保证代码正确的前提下,若是遇到Today Extension
没法加载数据,或者其余异常,能够把Application
删掉,插件也删掉,Clear
一下Project
,在运行便可。