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

相關文章
相關標籤/搜索