1.首先創建一個主體應用程序
2.創建Today Extension程序
給你的TodayExtension起一個名字,建好以後會變成下圖的文件結構
系統默認給你創建了一個storyboard,有好多人喜歡用純代碼開發,沒問題,接下來使用去掉storyboard
刪掉storyboard後打開plist文件,出現下圖
然後將NSExtensionMainStoryboard刪掉,添加NSExtensionPrincipalClass,屬性值爲你創建的類名,這裏是TodayViewController
3.UI佈局和打開app(這都是坑)
@implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// NSExtensionPrincipalClass NSExtensionMainStoryboard
if([[UIDevice currentDevice].systemVersion floatValue] > 10.0){
//最小爲110的高度,這個如果低於110則不改變
//設置NCWidgetDisplayModeExpanded可以展開摺疊
//設置NCWidgetDisplayModeCompact只有一種大小
self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;
}else{//iOS8 iOS9可以自己設置大小,不建議太大
self.preferredContentSize = CGSizeMake(0, 50);
}
UIButton *openBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
openBtn.center = CGPointMake(self.view.bounds.size.width/2.0, 20);
[openBtn setTitle:@"打開應用程序" forState:(UIControlStateNormal)];
[openBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[openBtn addTarget:self action:@selector(openApp) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:openBtn];
}
- (void)openApp {
NSLog(@"打開app");
[self.extensionContext openURL:[NSURL URLWithString:@"suntodayextension://"] completionHandler:^(BOOL success) {
if(success){
NSLog(@"成功");
}
}];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"appear");
//建議在這裏請求數據
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"disapear");
}
- (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);
}
//iOS10以後才能調用該方法
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
if (activeDisplayMode == NCWidgetDisplayModeExpanded) {//展開模式
self.preferredContentSize = CGSizeMake(0, 300);
} else if (activeDisplayMode == NCWidgetDisplayModeCompact) {//摺疊模式
self.preferredContentSize = maxSize;
}
}
//iOS10以後左邊不會有空白,該方法也就失去了作用
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets {
return UIEdgeInsetsZero;
}
@end
點擊打開需要在主體程序添加URL Scheme
然後再Today Extension項目的時間添加如下代碼
[self.extensionContext openURL:[NSURL URLWithString:@"suntodayextension://"] completionHandler:^(BOOL success) {
if(success){
NSLog(@"成功");
}
}];
在主體程序的AppDelegate.m文件中添加如下代碼
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if([url.absoluteString hasPrefix:@"suntodayextension://"] == YES){
//跳轉操作
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
[application.keyWindow.rootViewController presentViewController:vc animated:YES completion:NULL];
}
return YES;
}
4.共享數據
這個需要創建一個group的id
然後在主體app和today extension設置一下
然後開始共享數據,這裏使用NSUserDefault
//添加共享數據
NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.sun.appextension"];
[userDefault setValue:@"123" forKey:@"shareData"];
[userDefault synchronize];
//讀取共享數據
NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.sun.appextension"];
NSLog(@"%@",[userDefault objectForKey:@"shareData"]);