1: 添加Today Extension
图1选中Xcode->点击左上角的File->New->Target
图2点击Todya Extension
图3这就是添加Today Extension之后的项目目录
运行项目: 图4
2:绘制UI(纯代码):
删除MainInterface.storyboard, 并按照图5修改info.plist文件
图5
3: 实现展开, 折叠的效果:
在viewWillAppear中设置widget的mode为展开:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
/** 设置右侧是否显示展开/折叠按钮 */
if (@available(iOS 10.0, *)) {
/** iOS10.0 之后NSExtensionContext中才添加了widgetLargestAvailableDisplayMode的属性 */
self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;
}
else {
self.preferredContentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width, 110);
}
}
/** 重写切换展开与折叠布局时的方法(iOS之后) */
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
//宽度是默认的, 设置也没用
if (activeDisplayMode == NCWidgetDisplayModeCompact) {
/** 设置折叠的高度 */
self.preferredContentSize = CGSizeMake(0, 110);
}
else {
/** 设置展开的高度 */
self.preferredContentSize = CGSizeMake(0, 300);
}
}
4: 调起app
- (void)callApp {
[self.extensionContext openURL:[NSURL URLWithString:@"Schemes://xxx"]
completionHandler:^(BOOL success) {
}];
}
可在AppDelegate中实现以下方法, 来处理传过来的action:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
if ([url.absoluteString hasPrefix:@"Schemes"]) {
NSLog(@"处理action");
return YES;
}
}
5: 数据共享
参考:https://www.jianshu.com/p/9b3d06236d19
https://www.jianshu.com/p/ca3e11d7686c
https://www.jianshu.com/p/012319813522
网友评论