美文网首页
iOS Today Extensions 开发

iOS Today Extensions 开发

作者: 可惜你不是我的双子座 | 来源:发表于2018-08-27 10:55 被阅读20次

    一. 如何创建Today Extension

    1.1 首先添加扩展Target ,点击File-->New-->Target...,然后选择iOSApplication Extension中的Today Extension

    选择 --> Today Extension
    1.2 此时项目中,会是这样的。
    项目结构
    1.3 再次运行项目,可以看到一个默认的Hello World
    运行项目
    注意
    由于系统默认使用了xib,然而你又习惯使用纯代码,只需要这么干。首先删除MainInterface.storyboard,然后修改Info.plist文件。
    系统默认:
        <dict>
            <key>NSExtensionMainStoryboard</key>
            <string>MainInterface</string>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.widget-extension</string>
        </dict>
    

    纯代码:

        <dict>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.widget-extension</string>
            <key>NSExtensionPrincipalClass</key>
            <string>TodayViewController</string>
        </dict>
    
    纯代码

    二. 前期配置,包括图片,代码以及证书问题

    2.1 在应用和扩展间共享数据 - App Groups
    2.1.1首先需要去苹果开发者中心的Identifiers中创建一个App Groups,命名方式group.com.xxx.xxx(请记住它,项目中会用到),如下图:

    成功创建
    2.1.2APP ID的配置,如下图:
    APP ID的配置
    2.1.3:项目配置
    在项目中打开App Groups,如下图:
    打开App Groups.png
    打开开关,系统会自动列出所有的App group Id,这时选择对应的App group Id就可以了,第四步需要重复一二三步。
    2.2 图片共享
    虽然Extensions无法直接使用项目中Images.xcassets中的图片,但是可以共享文件,方法如下:
    图片共享.png
    2.3 部分代码共享
    1.对于有些项目可能没有使用cocoapods集成第三方库,那么我们怎么使用这些库呢,拿masonry来说,我们需要把每个.m文件勾选TodayViewController,如果没有勾选的话,也能使用这个库,而且xcode还不报错(这里是个坑),导致界面出现无法加载问题,如下图:
    msaonry.png
    无法载入.png
    2.如何使用cocoapods集成我们Extensions需要的第三方库呢。
    platform :ios,'9.0'
    target '项目名称' do
    pod 'Masonry'
    end
    
    target 'TodayViewController' do
    pod 'Masonry'
    end
    

    三. 代码部分(部分关键代码)

    3.1代码部分

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        
        // 设置widget展示视图的大小,默认折叠大小为110
        self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 110);
        [self.view addSubview:self.jumpBtn];
        [self addMasonry];
    }
    
    - (UIButton *)jumpBtn {
        
        if (!_jumpBtn) {
            
            _jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _jumpBtn.backgroundColor = [UIColor redColor];
            [_jumpBtn setTitle:@"跳转" forState:UIControlStateNormal];
            [_jumpBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }
        return _jumpBtn;
    }
    
    - (void)addMasonry {
        
        [self.jumpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.equalTo(self.view).offset(20);
            make.size.mas_equalTo(CGSizeMake(100, 40));
        }];
    }
    
    - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
    
        if (@available(iOS 10.0, *)) {
            if (activeDisplayMode == NCWidgetDisplayModeCompact) { // 叠起状态
    
                self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 110);
            }else {
    
                self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 110+0.5+44);
            }
        } else {
            // Fallback on earlier versions
        }
    }
    
    // 实现下面的协议,配置边距,否则在iOS 10之前,绘制的内容与左侧边界有一定距离。该方法在iOS 10之后被遗弃,iOS 10之后不存在间距。
    - (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets {
        
        return UIEdgeInsetsZero;
    }
    
    -(void)viewWillAppear:(BOOL)animated{
        
        [super viewWillAppear:animated];
        
        //设置NCWidgetDisplayModeExpanded,界面初始显示的时候,才有展开效果
        if (@available(iOS 10.0, *)) {
            self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;
        } else {
            // Fallback on earlier versions
        }
    }
    

    3.2 跳转到主界面
    3.2.1 添加URL Schemes

    添加URL Schemes.png
    3.2.2 跳转方法
    - (void)featuredBtnClick {
        
        [self.extensionContext openURL:[NSURL URLWithString:@"NDFWidgetNiaodaifu://action=featuredPost"] completionHandler:^(BOOL success) {
            
        }];
    }
    

    3.2.3 在Appdelegate实现如下代码

    //跳转处理 9_0之后
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options {
        
        NSString *prefix = @"NDFWidgetNiaodaifu://action=";
        if ([[url absoluteString] rangeOfString:prefix].location != NSNotFound) {
            
            NSString *action = [[url absoluteString] substringFromIndex:prefix.length];
    
            if ([action isEqualToString:@"featuredPost"]) { // 精选帖子
                
                UITabBarController *tabBarVC = (UITabBarController *)self.window.rootViewController;
                tabBarVC.selectedIndex = 2;
            }
        }
        return NO;
    }
    //跳转处理 老版 4_2, 9_0
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        
        NSString *prefix = @"NDFWidgetNiaodaifu://action=";
        if ([[url absoluteString] rangeOfString:prefix].location != NSNotFound) {
            
            NSString *action = [[url absoluteString] substringFromIndex:prefix.length];
            
            if ([action isEqualToString:@"featuredPost"]) { // 精选帖子
                
                UITabBarController *tabBarVC = (UITabBarController *)self.window.rootViewController;
                tabBarVC.selectedIndex = 2;
            }
        }
        return NO;
    }
    

    3.3 数据共享,需要拿到上面创建的group.com.xxx.xxx,对于数据共享,有两种方式,这里只介绍一种。
    3.3.1 通过NSUserDefaults共享数据
    1.主app中存储数据:

    #pragma mark -- 数据共享
    - (void)saveDataByNSUserDefaults {
        
        NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxx"];
        [shared setObject:[self.playListArray[self.indexCount] name] forKey:@"musicName"];
        [shared setObject:[self.playListArray[self.indexCount] songer] forKey:@"songerName"];
        
        [shared synchronize];
                                  
    }
    
    

    2.扩展中取出数据:

    NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxx"];
        NSString *musicName = [shared valueForKey:@"musicName"];
        NSString *songerName = [shared valueForKey:@"songerName"];
    

    3.3.2 通过NSFileManager共享数据(待完成。。。)

    参考

    iOS Today Extension iOS 10、iOS 9、8适配,以及CocoaPod的使用
    iOS Widget(App Extensions Today)证书配置

    相关文章

      网友评论

          本文标题:iOS Today Extensions 开发

          本文链接:https://www.haomeiwen.com/subject/urmdiftx.html