1.先創(chuàng)建一個(gè)extension
創(chuàng)建后多出來一個(gè)文件夾
widget
2.給APP 添加一個(gè) URL schemes 用來喚醒
3.上代碼 在widgetcontroller里面寫好UI
跳轉(zhuǎn)到app邏輯
func openButtonPressed() -> Void {
let url : URL = URL.init(string: "widgetDemo://open")!
self.extensionContext?.open(url, completionHandler: {(isSucces) in
print("點(diǎn)擊了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: "點(diǎn)擊了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.數(shù)據(jù)共享
在ios 看來widget 就是一個(gè)單獨(dú)的APP 所以按照它的沙盒機(jī)智用普通的
open class var standard: UserDefaults { get }
是行不通的
要把兩個(gè)APP 加入到一個(gè)group中
創(chuàng)建一個(gè)group 在開發(fā)者賬號(hào)中
在X code中 把APP和widget 加入到一個(gè)group中
注意在development 賬戶中創(chuàng)建了group后 這里會(huì)自動(dòng)出現(xiàn) 若沒有出現(xiàn)請(qǐng)點(diǎn)擊刷新
在
urltype
中加入identifier
至此就可以共享數(shù)據(jù)了
1).第一種方式 userdefault suite
通過
UserDefaults.init(suiteName: "group.com.chunqi.widgetDemo")
- APP寫入數(shù)據(jù)
// 通過userdefault suite共享數(shù)據(jù)
let userDefault = UserDefaults.init(suiteName: "group.com.chunqi.widgetDemo")
userDefault?.set(textField.text, forKey: "widgetString")
userDefault?.synchronize()
- widget讀取數(shù)據(jù)
//通過userdefault 共享數(shù)據(jù)
self.actionLabel.text = UserDefaults.init(suiteName: "group.com.chunqi.widgetDemo")?.object(forKey: "widgetString") as! String?
2).第二種方式 filemanager
- 寫入數(shù)據(jù)
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)
}
}
- 讀取數(shù)據(jù)
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