【发布时间】:2018-04-21 15:05:53
【问题描述】:
目标
从今日扩展(Widget)打开应用并根据按钮点击执行不同的操作。
- 目标:iOS 10
已阅读的帖子和文章
- Today App Extension Widget Tap To Open Containing App
- Open Safari from my Today Extension (widget) within my app
- http://atinyfish.com/2014/06/26/adding-a-url-scheme-to-your-app
- https://www.raywenderlich.com/150953/today-extension-tutorial-getting-started
- https://developer.apple.com/documentation/uikit/core_app/communicating_with_other_apps_using_custom_urls
已经完成
创建 URLScheme
目标/MyApp/Info/URLTypes
- URLType 标识符 = com.myCompany.MyAppName
- URLType URLSchemes = MyAppName
- URLType 角色 = 查看者
从 Widget 打开宿主应用程序
TodayViewController(小部件)
@IBAction func addButtonPressed(_ sender: UIButton) {
let url = URL(string: "MyAppName://")!
extensionContext?.open(url) { isSuccess in
switch isSuccess {
case true: print("Open URL Success") // It print when opening app
case false: print("Open URL Failed")
}
}
}
点击按钮时效果很好。 Host 应用程序已启动。
委托 func 应用程序(_:open:options:) 未调用
AppDelegate(宿主应用程序)
// This delegate function is never called when the host application launches
func application(_ app: UIApplication, // breakpoint doesn't trigger
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("FUNCTION CALLED ????????????") // Never print
return true
}
当通过 Widget 中的按钮打开主机应用程序时,永远不会调用此委托函数。从小部件打开应用程序时,我的应用程序如何运行一些代码。 我尝试使用其他一些方法,但它们已被弃用。
我继续搜索,当小部件打开主机应用程序时,似乎无法调用 AppDelegate 函数,因为在应用程序启动时不会调用所有其他方法,例如 applicationWillEnterForeground。
我在真机上试过,但还是不行。
【问题讨论】:
标签: ios swift widget url-scheme today-extension