iOS8 NotificationCenter Extension 簡介

來源:互聯網
上載者:User

標籤:class   code   http   tar   ext   com   

在最新的WWDC14上面,蘋果發布了iOS8的一些新特性,而其中最讓程式員興奮的特性莫過於Extension,或者稱之為Widget。

 

下面就來嘗鮮實驗一把。

 

 

一、Extension簡介

 

首先,蘋果只支援下面這種類型的Extension Point,也不支援第三方應用自己設定Extension Point 。

Extension point

Example extension that helps users:

Today (iOS and OS X)

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

(A Today extension is called a widget)

Share (iOS and OS X)

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

 

今天,我們只聚焦於Today的Extension。Notification Center是在iOS5的時候推出的,在推出之時,開發人員就在想是否可以進行定製,是否可以在上面添加一些應用相關的資訊,三年後,iOS8的推出終於完成了這個使命。

 

 

Extension和App不一樣,他相當於一個輕量的App。在每個程式的安裝包裡面都可以帶上多個Extension,每一個Extension是一個target。

 

Extension的生命週期是比較短的,基本如下同所示:

 

 

 

 

 

對於一個NoficationCenter Extension而言,當使用者拉下NoficationCenter的時候開始運行,當使用者關閉NoficationCenter的時候會結束,所以必須保證每一個Extension必須是輕量而且快。所以在你完成你的更新操作之後,系統會使用上一次退出時的來做顯示,這個邏輯和App是一樣的。

 

而Extension和App之間的通訊只能通過OpenURL和Shared Resources的方式來通訊,因為往往在運行Extension的時候App可能沒有在運行,所以只能通過一共用資源池的檔案進行互動,如:

 

 

 

二、一個簡單的DEMO

 

 

下面我們來寫一個簡單的Extension:

第一步,我們來建立一個新的Target,然後選擇Extension,再選擇Today:

 

 

我們可以看到,基本上一個Extension就是一個ViewController,所以ViewController中的ViewWillAppear等的回調在這裡也是生效的,完全可以當做一個ViewController來處理.

 

我們運行一下並且在NotificationCenter添加我們的Extension後可以看到,系統建立了Hello World的內容了:

 

 

如果你需要定製化你的ViewController的高度的話,可以使用AutoLayout或者調用ViewController的preferredContentSize來設定你需要的大小。

 

 

第二步,擷取內容

 

僅僅是這樣一個Extension並不能做些什麼,所以我們需要一些必須的資料做展示,而資料的內容可以通過Extension自身去網路擷取,也可以通過App來擷取,這裡說一下App擷取怎麼做。

 

首先,要通訊的App和Extension必須在同一個App Group裡面,在Xcode的項目配置裡面的Capabilities裡面找到App Group這一項,開啟,並且通過開發賬戶登入來產生一個App Group,並且將App和Extension都加入這同一個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 ] ;}

 

運行就得到如下效果:

 

 

 

 

 三、總結

     

     當然,這個DEMO只是簡單地描述了怎麼去完成一個Extension,在實際過程中需要面對如何複用Framework,如何處理多進程同時讀寫同一個檔案,和一些安全認證的問題,這裡只是簡單地實驗了一把。

 

【參考資料】

1.App Extension Programming Guide

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.