玩转iOS:iOS 8 新特性《Today Extension》

目录


做者感言

此次的Today Extension预研,让我以为本身还有不少的不足,由于还有不少东西都没有去仔细的去研究,之后接下来会继续再接再砺。git

最后: 若是你有更好的建议或者对这篇文章有不满的地方, 请联系我, 我会参考大家的意见再进行修改, 联系我时, 请备注Today Extension, 祝你们学习愉快~谢谢~github

Cain(罗家辉)objective-c

zhebushimengfei@qq.com: 联系方式app

350116542: 腾讯QQ性能


简介

Today Extension是在iOS 8以后所推出来的重大更新之一,在此以前, 或许有人看过部分App就已经实现过这些功能,但那种实现方式是并非系统所提供的,因此在性能方面须要打个问号。学习


建立Today Extension

开始建立Today Extension测试

0

选择Today Extensionui

1

激活Today Extensionatom

2


使用Storyboard实现Today Extension

在建立好Today Extension时,Xcode会自动建立一个对应的MainInterface.storyboard文件,而且与Today ExtensionController关联,打开MainInterface.storyboard, 咱们会看到有一个内容为Hello WorldUILabel,废话少说如今咱们来看看运行效果。spa

9

选择你须要关联启动的App

10

11

不要怀疑,就是这么简单的,Today Extension就这么出来了。


打开数据共享服务

不过,骚年郎们别着急,只是展现个Hello World而已,别高兴得太早,接下来咱们讲重头戏,也就是应用AppToday Extension的数据交互,在此以前, 咱们须要打开两个服务。

首先是主程序里的

3

4

5

6

再者呢,就是Today Extension里的

7

8

作完这两个操做以后,咱们会看到多出来的两个证书

PS:这个证书是收费的, 若是没有去申请,一个帐号能够免费测试10个证书,主应用1个,Today Extension插件1个,也就是说一个应用须要两个。

12


删掉Storyboard

接下来以前,咱们要把MainInterface.storyboard给干掉,毕竟代码才是王道(我的观点,不喜勿喷)若是喜欢用Storyboard的朋友,也有一个Storyboard版本的,后面再补上,废话就很少说了,上教程。

找到TodayI Extension中的Info.plist文件,看到这小样就在这,先留着先

13

手动添加NSExtensionPrincipalClass字段 并设为TodayViewController(这个Controller你能够本身指定,我这里为了方便,就直接拿Xcode生成的)

14

如今咱们能够把storyboard这小样给删掉了

15

再运行,你就会看到整个Today Extension是空的了,只有一个空图标和一个标题。

16


代码实现

主应用中,咱们须要设置一下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 

 复制代码

从Today Extension跳转至App

首先,咱们须要添加Identifier,以及URL Schemes

PS:这里的IdentifierURL Schemes是你本身定义的,不能与其余ApplicationIdentifierURL Schemes相同,不然会形成冲突。

17

18

而后呢,咱们去到主应用的AppDelegate.m文件中添加方法

19

最后,咱们去到TodayViewController里补上对应的方法就行了

20

21


最终效果

22


注意点

PS:在保证代码正确的前提下,若是遇到Today Extension没法加载数据,或者其余异常,能够把Application删掉,插件也删掉,Clear一下Project,在运行便可。


补上几篇文章

iOS8Extension之Today插件

WWDC 2014 Session笔记 - iOS 通知中心扩展制做入门

如何用纯代码构建一个Widget(today extension)

相关文章
相关标签/搜索