Today Extension
程序员文章站
2022-05-30 17:37:53
...
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"]);
上一篇: UGUI背包拖拽
下一篇: Today Extension
推荐阅读
-
vscode extension插件开发详解
-
IOS如何在Host App 与 App Extension 之间发送通知
-
C#基于Extension Method(扩展方法)获得文件大小的方法
-
Excel2010从函数库类别菜单中选择TODAY函数输入当天日期
-
PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
-
百度凤巢上线Extension,搜索可直接显示粉丝数
-
vscode extension插件开发详解
-
[Php] windows下使用composer出现SHA384 is not supported by your openssl extension
-
IOS如何在Host App 与 App Extension 之间发送通知
-
IF+TODAY实现键入数据后在其后自动记录当前输入日期