iosswifttoday-extensionopenurl

Check if app launched via today widget on iOS...?


When I open the App from my Today Extension with this method :

open func open(_ URL: URL, completionHandler: ((Bool) -> Void)? = nil)

My app's life cycle goes this way :

This annoys me because I want to set my launchState variable to widget and do custom logic in my initial ViewController which I can't if the openURL method is the last one triggered.

How can I handle this scenario ?

EDIT:

The user can open the App from the Today Extension, or the Classic launch. In the viewDidLoad of the rootViewController I am doing this :

fileprivate func prepareCityToLaunch() {

    switch launchState {
         case .widget:
             launchCity.getWidgetCity()
         case .classic:
             launchCity.getLaunchCity()
    }
}

Which is an enum, by default it is set to .classic, and I am setting it to .widget in the openURL method.

This methods return the appropriate City object to request for thank to delegation.

I can't wait for the Notification from openURL because in this viewController I don't know yet if the App is open from Widget or from Classic launch and because the launchState is set to classic by default, it will always get the city as if the App is launched in the classic way.

That is why I wanted to set the launchState first in the openURL method, then in my viewDidLoad everything would have been set correctly.

Is it understandable ?


Solution

  • You need to show a loader/empty value inside the vc , then using NSNotificationCenter from inside openURL to set needed value

    OR

    Set a suite shared user default between your app and the widget to know that and give it a value before the line that directs to the app , and check it's value inside viewDidLoad of the vc , then set it to nil