1.先创建一个extension
创建后多出来一个文件夹
widget
2.给APP 添加一个 URL schemes 用来唤醒
3.上代码 在widgetcontroller里面写好UI
跳转到app逻辑
func openButtonPressed() -> Void {
let url : URL = URL.init(string: "widgetDemo://open")!
self.extensionContext?.open(url, completionHandler: {(isSucces) in
print("点击了open按钮,来唤醒APP,是否成功 : \(isSucces)")
})
}
widget 折叠逻辑
override func viewDidLoad() {
super.viewDidLoad()
if ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10, minorVersion: 0, patchVersion: 0)) {
//在ios10 中支持折叠
self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded
}
self.preferredContentSize = CGSize(width: UIScreen.main.bounds.width, height: 100)
self.actionButton.addTarget(self, action: #selector(openButtonPressed), for: UIControlEvents.touchUpInside)
}
//折叠change size
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
print("maxWidth %f maxHeight %f",maxSize.width,maxSize.height)
if activeDisplayMode == NCWidgetDisplayMode.compact {
self.preferredContentSize = CGSize(width:maxSize.width,height: 110);
}else{
self.preferredContentSize = CGSize(width: maxSize.width,height: 200);
}
}
4.在Appdelegate 中写好接收widget 的操作
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.absoluteString.hasPrefix("WidgetDemo") {
let alert = UIAlertController.init(title: "Tip", message: "点击了open按钮", preferredStyle: UIAlertControllerStyle.alert)
let tureAction = UIAlertAction.init(title: "Yes", style: UIAlertActionStyle.cancel, handler: { (cancle ) in
})
alert.addAction(tureAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated:true, completion: {
})
}
return true
}
5.数据共享
在ios 看来widget 就是一个单独的APP 所以按照它的沙盒机智用普通的
open class var standard: UserDefaults { get }
是行不通的
要把两个APP 加入到一个group中
创建一个group 在开发者账号中
在X code中 把APP和widget 加入到一个group中
注意在development 账户中创建了group后 这里会自动出现 若没有出现请点击刷新
在
urltype
中加入identifier
至此就可以共享数据了
1).第一种方式 userdefault suite
通过
UserDefaults.init(suiteName: "group.com.chunqi.widgetDemo")
- APP写入数据
// 通过userdefault suite共享数据
let userDefault = UserDefaults.init(suiteName: "group.com.chunqi.widgetDemo")
userDefault?.set(textField.text, forKey: "widgetString")
userDefault?.synchronize()
- widget读取数据
//通过userdefault 共享数据
self.actionLabel.text = UserDefaults.init(suiteName: "group.com.chunqi.widgetDemo")?.object(forKey: "widgetString") as! String?
2).第二种方式 filemanager
- 写入数据
func shareDataByFileManger() -> Void {
var url : URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.chunqi.widgetDemo")!
url = url.appendingPathComponent("Library/Caches/widget", isDirectory: true)
let str : String? = self.textField.text
do{
_ = try str?.write(to: url, atomically: true, encoding: String.Encoding.utf8)
} catch let error {
print(error)
}
}
- 读取数据
func shareDataByFileManager() -> String {
var url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.chunqi.widgetDemo")
url = url?.appendingPathComponent("Library/Caches/widget", isDirectory: true)
var str = ""
do {
str = try String.init(contentsOf: url!, encoding: String.Encoding.utf8)
} catch let error {
print(error)
}
return str
}
git源码示例 https://github.com/Liuchunqi3240/demo/tree/master/Swift/widgetDemo