Goal
From Today Extension (Widget) open app and perform different actions according to button tap.
- target: iOS 10
Posts & articles readed
- 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
Already done
Create URLScheme
Target/MyApp/Info/URLTypes
- URLType identifier = com.myCompany.MyAppName
- URLType URLSchemes = MyAppName
- URLType role = Viewer
Open host application from Widget
TodayViewController (Widget)
@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")
}
}
}
The button works well when tapped. The Host application is launched.
Delegate func application(_:open:options:) not called
AppDelegate (Host Application)
// 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
}
This delegate function is never called when the host Application opens from button tap in the Widget. How can my app run some code when the application is opened from the widget. I tried using some other methods but they are deprecated.
I continue searching and it looks like that no AppDelegate
function can be call when the widget open the host application because all the other methods like applicationWillEnterForeground
is not called when the application lauches.
I tried on a real device but it's still not working.