Skip to content
This repository has been archived by the owner on Nov 17, 2019. It is now read-only.

中文文档

smx edited this page Sep 27, 2017 · 4 revisions

Today Wideget (Extenstion)

实现

developer apple certificate (苹果开发者 证书网站)

1、App IDS 创建 App ID srxboys_today_cer0

2、App Groups 创建 共享组 srxboys_today_cer1

3、App IDS 修改 项目app ID Today widget ID共享组 srxboys_today_cer2

4、iOS Provisioning Profiles 最后一步创建 真机证书 srxboys_today_cer3


Xcode 设置一下

1、 Schemes 配置项目target 可被其他app跳转的 Schemes srxboys_Xcode

srxboys_Xcode2

2、 App Groups 开启App的共享组 srxboys_Xcode2


其他代码说明

1、编写在 - (void)viewDidLoad

#ifdef __IPHONE_10_0 // 因为是iOS10才有的,还请记得适配
                     // 如果需要折叠
    self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;
#endif

/// 如果需要折叠效果的,不要在 - (void)viewDidLoad 写高度。否则造成【折叠/展开】按钮样式bug
    self.preferredContentSize = CGSizeMake(0, 110);

2、折叠、展开的 的处理方式

- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
    if(activeDisplayMode == NCWidgetDisplayModeCompact) {
        // 110
        self.preferredContentSize = CGSizeMake(0, 110);
    }
    else {
        // 最高,根据设备机型(自行查看maxSize)
        self.preferredContentSize = CGSizeMake(0, 400);
    }
}

3、Today Wideget刷新界面时机

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
     // 请求网络/获取<共享组>   数据
    
      //不要在这里写高度。否则造成【折叠/展开】按钮样式bug
     // completionHandler(NCUpdateResultNewData); // 刷新页面UI
     // completionHandler(NCUpdateResultNoData); // 没有数据,不刷新
}

4、取消widget默认的inset,让应用靠左

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
    return UIEdgeInsetsZero;
}