Today Extension

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"]);

Demo地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值