1. 設置URL Types
Paste_Image.png
在容器app的target-->info中如圖設置一個URL Types。
設置的目的:是為了告訴app這個url是合法的,是用來打開app的url。
2. 給Today添加tap手勢
Paste_Image.png
實現點擊手勢:
@IBAction func tap(sender: UITapGestureRecognizer) {
//申明要打開的url
let url = NSURL(string: "alert")
//打開對應的應用
extensionContext?.openURL(url!, completionHandler: nil)
}
3. 容器app捕獲url
選擇AppDelegate.swift文件。用下面這個方法捕獲url。
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if url.scheme == "alert" {
let alertView = UIAlertView(title: "提示", message: "從Today中打開", delegate: nil, cancelButtonTitle: "好的")
alertView.show()
return true
}
return false
}
4. 效果展示
這樣點擊Today之后,就可以啟動app啦!
Paste_Image.png