美文网首页
iOS Today Widget设置

iOS Today Widget设置

作者: barry | 来源:发表于2019-02-28 10:12 被阅读0次

环境: xcode10.1,iOS 12

效果图:


t.gif

demo地址,欢迎star

1.创建Target

  • File -> New ->target
创建Target
  • 创建完成之后的效果


    效果图
  • 默认使用Storyboard


    图片.png

2.Info.plist设置

如果想代码绘制UI,要进行以下操作

  • Bundle display name:Widget在通知栏显示的名称
  • NSExtension
    如果你是使用纯代码进行开发,请按照下面进行操作:
对于 OC

1)请删除NSExtensionMainStoryboard 的键值对和MainInterface.storyboard文件;
2)请添加NSExtensionPrincipalClass这个key,并将value设置为控制器(如TodayViewController)

对于 Swift

1)请删除NSExtensionMainStoryboard 的键值对和MainInterface.storyboard文件;
2)请添加NSExtensionPrincipalClass这个key,并将value设置为控制器(如$(PRODUCT_NAME).TodayViewController)

Swift有命名空间的问题,如果不加会报如下报错

图片.png

2.Widget细节处理

iOS 8
    1. iOS8下没有折叠和展开功能,默认的Widget高度为self.preferredContentSize设置的高度。
preferredContentSize = CGSize(width: UIScreen.main.bounds.size.width, height: 110)
  • 2.iOS8下所有组件默认右移30单位,可以通过下面的方法修改上下左右的距离
func widgetMarginInsets(forProposedMarginInsets defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
 }
iOS 10

iOS 10以后,有两种显示模式

NCWidgetDisplayModeCompact, // Fixed height,高度固定,最低高度为110
NCWidgetDisplayModeExpanded, // Variable height,高度可变
NCWidgetDisplayModeExpanded下高度一览表
模拟器测试结果(实际结果应以真机为准).png
. 修正:iPhoneX 和XS尺寸相同,但是最大的widget高度是不同的,上图上是模拟器的测试结果,XS的的最大高度应该是748。另外widget的最小高度也不一定是110(大部分情况下是),XS的高度是98,所以最好使用系统的方法动态的获取高度(extensionContext?.widgetMaximumSize(for: .compact).height)
  • 在设置最大高度的时候要考虑到具体机型和系统
  • Widget 的收起、展开 则是通过这个代理方法:
/**
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
    if(activeDisplayMode == NCWidgetDisplayModeCompact) {
        // 尺寸只设置高度即可,因为宽度是固定的,设置了也不会有效果
        self.preferredContentSize = CGSizeMake(0, 110);
    } else {
        self.preferredContentSize = CGSizeMake(0, 310);
    }
}
  • 在设置 UI 的过程中,若想使用本体 Target 中的类:


    图片.png

3.数据共享

  • 主App target设置Group
开启Group
添加Group标识
  • Widget target设置Group

    同上

  • Userdefault共享数据

设置数据

//主项目设置widget数据(通过Userdefaults)  AppDelegate.swift
    private func setWidgetData() {
        let userDafault = UserDefaults(suiteName: "group.tsc")
        userDafault?.set("主App名字(Userdefaults)", forKey: "FirstDataName")
        userDafault?.set("主App内容(Userdefaults)", forKey: "FirstDataContent")
        userDafault?.synchronize()
    }

取数据

//获取主项目设置widget数据(通过Userdefaults)TodayViewController.swift
    func fetchWidgetData() -> WidgetModel {
        let userDefault = UserDefaults(suiteName: "group.tsc")

        let model = WidgetModel(name: userDefault?.string(forKey: "FirstDataName") ?? "未获从主App取到名字", date: "Yesterday", content: userDefault?.string(forKey: "FirstDataContent") ?? "未从主App获取到内容", imageName: "Report_Success_icon_120x120_")
        return model
    }
  • FileManager共享数据

设置数据

    private var models: [WidgetModel] = {
        let imageNameArray = ["Report_Success_icon_120x120_", "Report_Success_icon_120x120_", "Report_Success_icon_120x120_", "Report_Success_icon_120x120_", "Report_Success_icon_120x120_"]
        let nameArray = ["Alex & Jessica(FileManager)", "Jessica(FileManager)", "Rocky(FileManager)", "Work Order(FileManager)", "Work Order(FileManager)"]
        let dateArray = ["5:30 pm", "Yesterday", "11/02/2018", "11/02/2018", "11/02/2018"]
        let conentArray = ["Rocky: Where are you Jessic?", "Rocky: Where are you Jessic?I will arrive station about 30n munites later", "Rocky: Where are you Jessic?I will arrive station about 30n munites later", "Rocky: Where are you Jessic?I will arrive station about 30n munites laterRocky: Where are you Jessic?I will arrive station about 30n munites later", "Rocky: Where are you Jessic?I will arrive station about 30n munites laterRocky: Where are you Jessic?I will arrive station about 30n munites later"]
        var array = [WidgetModel]()
        for i in 0..<nameArray.count {
            let model = WidgetModel(name: nameArray[i], date: dateArray[i], content: conentArray[i], imageName: imageNameArray[i])
            array.append(model)
        }
        return array
    }()
//主项目设置widget数据(通过FileManager)
    func setWidgetFileData() {
        let manager = FileManager.default
        var url = manager.containerURL(forSecurityApplicationGroupIdentifier: "group.tsc")
        //路径上多了个file:// 要去掉
        var urlString = url!.absoluteString.replacingOccurrences(of: "file://", with: "")
        urlString.append("Library/MainWidget")
        //1创建路径和文件
        if !manager.fileExists(atPath: urlString) {
            do {
                if let _:() = try? manager.createDirectory(atPath: urlString, withIntermediateDirectories: true, attributes: nil) {
                    print("路径创建成功")
                    urlString.append("/group.json")
                    do {
                        let result = manager.createFile(atPath: urlString, contents: nil, attributes: nil)
                        result ? print("文件创建成功") : print("文件创建失败")
                    }
                } else {
                    print("路径创建失败")
                }
            }
        }
        //2写入数据
        if let json = serialization() {
            url?.appendPathComponent("Library/MainWidget/group.json")
            print("Group路径----\(url!)")
            if let _:() = try? json.write(to: url!) {
                print("写入成功")
            } else {
                print("写入失败")
            }
        } else {
            print("序列化失败")
        }
    }

取数据

 //获取主项目设置widget数据(通过FileManager)
    func setWidgetFileData() {
        let manager = FileManager.default
        var url = manager.containerURL(forSecurityApplicationGroupIdentifier: "group.tsc")
        //路径上多了个file:// 要去掉
        var urlString = url!.absoluteString.replacingOccurrences(of: "file://", with: "")
        url?.appendPathComponent("Library/MainWidget/group.json")

        urlString.append("Library/MainWidget/group.json")
        //获取json数据
        if manager.fileExists(atPath: urlString) {
            do {
                let json = try! String(contentsOf: url!)
                let modelArray = try? JSONDecoder().decode([WidgetModel].self, from: json.data(using: .utf8)!)
                models = modelArray ?? []
            }
        } else {
            print("不存在")
        }
    }

4.Scheme配置

  • 宿主App配置Scheme
图片.png
  • Widget配置跳转
@objc func buttonDidClick(_ sender: UIButton) {
        var action = ""
        switch sender.tag {
        case 100:
            action = "scan"
        case 101:
            action = "pay"
        case 102:
            action = "rec"
        case 103:
            action = "zhuanzhang"
        case 104:
            action = "code"
        default:
            break
        }
        extensionContext?.open(URL(string: "MainWidgetScheme://action=\(action)")!, completionHandler: { (_) in
        })
    }
  • 宿主App处理跳转
 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        let urlString = url.absoluteString
        let startIndex = urlString.startIndex
        let string = "MainWidgetScheme://action="

        if urlString.contains(string) {
            let newStartIndex = urlString.index(startIndex, offsetBy: string.count)
            let code = urlString[newStartIndex..<urlString.endIndex]
            switch code {
            case "scan":
                tabBar.alert(title: "扫一扫", cancelString: "取消", confirmString: "确认")
            case "pay":
                tabBar.alert(title: "付款", cancelString: "取消", confirmString: "确认")
            case "rec":
                tabBar.alert(title: "收款", cancelString: "取消", confirmString: "确认")
            case "zhuanzhang":
                tabBar.alert(title: "转账", cancelString: "取消", confirmString: "确认")
            case "code":
                tabBar.alert(title: "乘车码", cancelString: "取消", confirmString: "确认")
            default:
                break
            }
        }
        return true
    }

demo地址,欢迎star

Extension 证书配置

相关文章

  • iOS Today Widget设置

    环境: xcode10.1,iOS 12 效果图: demo地址,欢迎star 1.创建Target File -...

  • iOS Widget开发小结

    从iOS8开始, 系统开始支持iOS Widget,就是如下图.Widget是Extension的一种Today,...

  • iOS Today Widget 扩展

    1.创建一个简单的app,主页面如下 2.添加扩展Target2.1点击File-->New-->Target.....

  • iOS Widgets

    iOS14 以前的 Widget 项目构成 The main app:项目原工程。 A Today extensi...

  • iOS Widget Extension

    iOS14 Apple 新引入了 Widget Extension 的功能 来代替之前的 Today extens...

  • iOS Widget(Today Extension)插件

    today extension 开发很简单,一下内容简单介绍: 首先在你的工程添加target 建完之后你会发现工...

  • iOS Extension Today Widget 开发

    由于项目中要用到,最近研究了下Today Widget 开发 1.什么是Extension? 扩展 (Extens...

  • iOS-widget-Today 扩展

    今天要分享的是通知中心扩展中的-Today扩展(ios8推出),ios目前可以使用的扩展有:today扩展(wid...

  • iOS开发------Widget(Today Extensio

    iOS10.0发布啦(貌似过去有点时间了吧 - -),在宏观带给我们使用体验的提升之外,更多的是带给iOS开发者一...

  • iOS Today Extension开发(Widget)

    前言: 自从iOS8API中开放iOS Today 扩展之后 很多朋友都在研究一下出来装个X,说实话这个东西做出来...

网友评论

      本文标题:iOS Today Widget设置

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