>图片1(创建今日扩展)

>图片2

 >图片3(设置大小)

 >图片4(绘画控件)

>图片5(设置共享文件)

 >图片6(设置群组ID)

>图片7(设置URL Schemes)

>扩展中的主要逻辑代码

class TodayViewController: UIViewController, NCWidgetProviding,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView! var newsList = [NewsModel]()
lazy var moreBtn:UIButton = { let btn = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 44))
btn.setTitle("查看更多", for:.normal)
btn.addTarget(self, action: #selector(gotoMainApp), for: .touchUpInside)
btn.backgroundColor = UIColor(red:245/255.0, green:74/255.0, blue:48/255.0, alpha: 1)
return btn }() func gotoMainApp(){
//跳转到主程序的代码(见图7)
self.extensionContext?.open(URL(string:"WidgetApp://action=GotoNewsListPage")!, completionHandler: { (suc:Bool) in })
} override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
updateView()
} //刷新界面
func updateView(){
if newsList.count != 0{
newsList.removeAll()
}
//通过UserDefaults从组里面获取共享数据(见图6)
let ud = UserDefaults(suiteName: "group.centaHouse");
if let nickName = ud?.array(forKey: "group.centaHouse.centaToday"){
for (_,element) in nickName.enumerated() {
if let edic = element as? [String : Any]{
let nm = NewsModel()
nm.setValuesForKeys(edic)
newsList.append(nm)
}
} self.tableView.reloadData()
if newsList.count != 0 {
self.tableView.tableFooterView = self.moreBtn
} //判断在不同数据下展示界面的视图大小(不处理的话,展开和折叠会出问题)
if #available(iOSApplicationExtension 10.0, *) {
if extensionContext?.widgetActiveDisplayMode == .compact{ //压缩状态
// if newsList.count != 0 {
// UIView.animate(withDuration: 0.1, animations: {
// self.preferredContentSize = CGSize(width: self.view.bounds.width, height: CGFloat(self.newsList.count*100 + 44))
// })
// }
}else{//展开状态
if newsList.count != 0 {
UIView.animate(withDuration: 0.1, animations: {
self.preferredContentSize = CGSize(width: self.view.bounds.width, height: CGFloat(self.newsList.count*100 + 44))
})
}
}
} else {
// Fallback on earlier versions
} }
} override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
//注册自定义cell
let nib = UINib(nibName: "NewsInfoCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "NewsInfoCell") //设置今日扩展模式为可张开收缩模式(IOS10以后支持,进入扩展右上角显示"展开"和"折叠")
if #available(iOSApplicationExtension 10.0, *) {
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
} else {
// Fallback on earlier versions
}; } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsList.count
} func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//新闻列表
let cell = tableView.dequeueReusableCell(withIdentifier: "NewsInfoCell") as! NewsInfoCell
cell.news = newsList[indexPath.row]
return cell
} func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let url = newsList[indexPath.row].ShowNewsUrl
extensionContext?.open(URL(string:"WidgetApp://action=Goto-\(url)")!, completionHandler: { (suc:Bool) in })
} func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 95;
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} //监听扩展的展开和收缩状态处理视图的大小
/**
* @maxSize 界面能够显示的最小和最大尺寸(最小状态下是110)
* @activeDisplayMode: 张开和收缩状态
**/
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if(activeDisplayMode == .compact){
self.preferredContentSize = CGSize(width: maxSize.width, height: maxSize.height)
}else{
self.preferredContentSize = CGSize(width: maxSize.width, height: CGFloat(newsList.count*100 + 44))
}
} //自行看官方注释
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view. // If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData if #available(iOSApplicationExtension 10.0, *) {
if extensionContext?.widgetActiveDisplayMode == .compact{
completionHandler(NCUpdateResult.newData)
}else{
completionHandler(NCUpdateResult.noData)
}
} else {
// Fallback on earlier versions
completionHandler(NCUpdateResult.newData)
} } }
#pragma mark  app跳转 (今日通知栏)
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ NSString* prefix = @"WidgetApp://action=";
if ([[url absoluteString] rangeOfString:prefix].location != NSNotFound) {
NSString* action = [[url absoluteString] substringFromIndex:prefix.length];
if ([action isEqualToString:@"Goto-"]) {
......
}else{
......
}
}
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
//在主程序保存需要展示的数据
func extentionUpdate(){
//group.cenXXX 组名
if let user = UserDefaults(suiteName: "group.cenXXX") {
//扩展标识符
user.set(objArray, forKey: "group.cenXXX.cenToday")
}
}

>收缩状态

>展开状态

  

  

Swift3 今日(TodayExtension)扩展图文笔记的更多相关文章

  1. firefox 扩展开发笔记(三):高级ui交互编程

    firefox 扩展开发笔记(三):高级ui交互编程 前言 前两篇链接 1:firefox 扩展开发笔记(一):jpm 使用实践以及调试 2:firefox 扩展开发笔记(二):进阶开发之移动设备模拟 ...

  2. 常见浏览器扩展开发笔记(chrome firefox 360 baidu qq sougou liebao uc opera)

    浏览器扩展开发貌似时下很冷门啊,但是不少企业还是有类似的应用,360的抢票插件啊,笔者最近在做的网页翻译扩展之类的.笔者在开发的过程中,遇到了不少坑,说是坑,说白了就是各个厂商支持的API不统一导致的 ...

  3. Swift3.0 基础学习梳理笔记(一)

    本篇是我在学完一遍基础语法知识的时候,第一遍复习,我一遍梳理一遍记录的笔记.同时分享给像我一样在学习swift 的猿友们. 本篇可能过于冗长.所以所有的参考资料都分模块的写在palyground 里, ...

  4. 高性能可扩展mysql 笔记(一)数据库表、索引、SQL语句设计规范

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 项目说明:该笔记的背景为电商平台项目,电商项目由于其高并发.多线程.高耗能等特性,在众多的项目类型中涉及 ...

  5. Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...

  6. 挑灯熬夜看《Build 2015 Keynote》图文笔记

    又是一年微软Build大会时间,网络上流传各种微软新品发布的消息终于也要揭晓了,一直熬夜到凌晨3点,好久没有这么兴奋了. 微软给力的很嘛! Satya nadella开始讲解 首先回顾微软的传统和技术 ...

  7. jQuery扩展方法笔记

    一.方式列表: 1.jQuery.extend(Object); // jQuery 本身的扩展方法 2.jQuery.fn.extend(Object); // jQuery 所选对象扩展方法 二. ...

  8. Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Openssl 1.1.0e + Mariadb 10.1.22 + Nginx 1.12.0 + PHP 7.1.4 + Laravel 5.4 )

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...

  9. Centos7 编译安装 Nginx PHP Mariadb Memcache扩展 ZendOpcache扩展 (实测 笔记 Centos 7.0 + Mariadb 10.1.9 + Nginx 1.9.9 + PHP 5.5.30)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1503-01.iso 安装步骤: 1.准备 1.1 ...

随机推荐

  1. 使用Trinity拼接以及分析差异表达一个小例子

    使用Trinity拼接以及分析差异表达一个小例子  2017-06-12 09:42:47     293     0     0 Trinity 将测序数据分为许多独立的de Brujin grap ...

  2. LibreOJ #6007. 「网络流 24 题」方格取数 最小割 最大点权独立集 最大流

    #6007. 「网络流 24 题」方格取数 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  3. sex在软件开发中的运用--SIX技术

    开篇:省略xxx字 keyword:sex . female, male .SIX ,sex integer extention technolgolsl 前言: 对于sex字段的研究,国内,国际尚为 ...

  4. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  5. LFI/RFI总结

    目录 0×01 文件包含简介 服务器执行PHP文件时,可以通过文件包含函数加载另一个文件中的PHP代码,并且当PHP来执行,这会为开发者节省大量的时间.这意味着您可以创建供所有网页引用的标准页眉或菜单 ...

  6. oracle 提示没有监听

    # listener.ora Network Configuration File: E:\Oracle10g\network\admin\listener.ora # Generated by Or ...

  7. VMware ESXi 配置小结

    VMware ESXi 配置小结------------------------------------------------------------------------------------ ...

  8. Spark学习笔记-GraphX-1

    Spark学习笔记-GraphX-1 标签: SparkGraphGraphX图计算 2014-09-29 13:04 2339人阅读 评论(0) 收藏 举报  分类: Spark(8)  版权声明: ...

  9. JQuery中after() append() appendTo()的区别

    首先 after() 是追加在元素外边而append() appendTo()是追加在元素里面. $(selector).after(content) $("span").afte ...

  10. svn 修改原来包名的方法和会报的错误

    SVN E200009 which is not part of the commit; both sides of the move must be committed together 在svn上 ...