Determine if iOS app was launched by WCSession.sendMessage

The documentation on WCSession sendMessage(_:replyHandler:errorHandler:) says this:


"Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable." See here.


My question: how can I determine if the iOS app was launched by an incoming WCSession.sendMessage request?


I checked the 'launchOptions' in didFinishLaunchingWithOptions, but launchOptions are always nil.


Thanks for any help.

Replies

Did you come up with anything on this? Is your objective to determine your launch reason before returning from didFinishLaunching?


My use case is to prompt the user to complete onboarding on their phone which includes several user agreements (i.e. Terms of Service and Privacy Statement). I was curious about this. Through experimentation with the simulator, I may have found a way to detect it, but I wouldn't rely on it if your timing or initialization depends on it.


watchOS

On InterfaceController X, Y, or Z (activate, load, appear) do:

1. Set the delegate to the WCSession.default

2. Activate the session

3. onActivation, send message (or on controlled event from interface)


iOS

In your app delegate, immediately do:

1. Set the WCSession delegate

2. Activate the session

3. Observe the message come in


In this test scenario, I observed that the didReceiveMessage is invoked right away in a different thread.


default

20:18:54.456641-0600

appname

Application-Delegate

subsystem

didFinishLaunching() options: nil

default

20:18:54.468903-0600

appname

Application-Delegate

subsystem

Received message ["OnboardingRequest": 1]

default

20:18:54.493954-0600

appname

Application-Delegate

subsystem

Finished launching


Note: The middle message was captured on a background thread (not the same thread invoking didFinishLaunching). Not ideal but it might work, as long as you account for a potential race condition nightmare.