此次的
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
,在運行便可。