iOS8 NotificationCenter Extension 简介

在最新的WWDC14上面,苹果发布了iOS8的一些新特性,而其中最让程序员兴奋的特性莫过于Extension,或者称之为Widgetios

 

下面就来尝鲜试验一把。程序员

 

 

1、Extension简介web

 

首先,苹果只支持下面这种类型的Extension Point,也不支持第三方应用本身设置Extension Point 安全

Extension point网络

Example extension that helps users:app

Today (iOS and OS X)ide

Get a quick update or perform a quick task in the Today view of Notification Centerui

(A Today extension is called a widget)spa

Share (iOS and OS X)code

Post to a sharing website or share content with others

Action (iOS and OS X)

Manipulate or view content within the context of another app

Photo Editing (iOS)

Edit a photo or video within the Photos app

Finder (OS X)

Use a remote file storage service in OS X

Storage Provider (iOS)

Choose a document from among the set of documents the current iOS app can access

Custom keyboard (iOS)

Replace the iOS system keyboard with a custom keyboard for use in all apps

 

今天,咱们只聚焦于TodayExtensionNotification Center是在iOS5的时候推出的,在推出之时,开发者就在想是否能够进行定制,是否能够在上面添加一些应用相关的资讯,三年后,iOS8的推出终于完成了这个使命。

 

 

ExtensionApp不同,他至关于一个轻量的App。在每一个程序的安装包里面均可以带上多个Extension,每个Extension是一个target

 

Extension的生命周期是比较短的,基本以下同所示:

 

 

 

 

 

对于一个NoficationCenter Extension而言,当用户拉下NoficationCenter的时候开始运行,当用户关闭NoficationCenter的时候会结束,因此必须保证每个Extension必须是轻量并且快。因此在你完成你的更新操做以后,系统会使用上一次退出时的截图来作显示,这个逻辑和App是同样的。

 

ExtensionApp之间的通讯只能经过OpenURLShared Resources的方式来通讯,由于每每在运行Extension的时候App可能没有在运行,因此只能经过一共享资源池的文件进行交互,以下图:

 

 

 

2、一个简单的DEMO

 

 

下面咱们来写一个简单的Extension:

第一步,咱们来建立一个新的Target,而后选择Extension,再选择Today:

 

 

咱们能够看到,基本上一个Extension就是一个ViewController,因此ViewController中的ViewWillAppear等的回调在这里也是生效的,彻底能够当作一个ViewController来处理.

 

咱们运行一下而且在NotificationCenter添加咱们的Extension后能够看到,系统建立了Hello World的内容了:

 

 

若是你须要定制化你的ViewController的高度的话,可使用AutoLayout或者调用ViewControllerpreferredContentSize来设置你须要的大小。

 

 

第二步,获取内容

 

仅仅是这样一个Extension并不能作些什么,因此咱们须要一些必须的数据作展现,而数据的内容能够经过Extension自身去网络获取,也能够经过App来获取,这里说一下App获取怎么作。

 

首先,要通讯的AppExtension必须在同一个App Group里面,在Xcode的项目配置里面的Capabilities里面找到App Group这一项,打开,而且经过开发帐户登陆来生成一个App Group,而且将AppExtension都加入这同一个App Group

 

而后,在本地建立一个纯文本,里面打上须要在Extension上面显示文字,而后在启动的时候加入下面的代码,其中的GroupIdentifier是建立的App Group的标识符。

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSURL * fileURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.notificationcenter.extension.com" ] ;
    NSURL * sourceURL = [NSURL fileURLWithPath: [[ NSBundle mainBundle ] pathForResource:@"helloextension" ofType:@"txt"] ] ;
    NSURL * targetURL = [ NSURL URLWithString:[ [fileURL absoluteString] stringByAppendingString : @"helloextension.txt" ] ] ;
    [[ NSFileManager defaultManager] moveItemAtURL:sourceURL toURL:targetURL error:nil ] ;
    
    return YES;
}

 

而后在Extension里面加入如下代码:

 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    NSURL * fileURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.notificationcenter.extension.com" ] ;
    NSURL * targetURL = [ NSURL URLWithString:[ NSString stringWithFormat:@"%@/helloextension.txt" , [fileURL absoluteString] ] ] ;
    NSString* nsString = [ NSString stringWithContentsOfURL:targetURL encoding:NSUTF8StringEncoding error:nil] ;
    
    _label.text = nsString ;
    [_label sizeToFit ] ;
}

 

运行就获得以下效果:

 

 

 

 

 3、总结

     

     固然,这个DEMO只是简单地描述了怎么去完成一个Extension,在实际过程当中须要面对如何复用Framework,如何处理多进程同时读写同一个文件,和一些安全认证的问题,这里只是简单地试验了一把。

 

【参考资料】

1.App Extension Programming Guide

相关文章
相关标签/搜索