user8403747
user8403747

Reputation:

Check if watch app is in frontmost state

Is there any way to find if watch app is in frontmost (from iPhone or not)? The problem is that when the watch app is in frontmost state, it accesses both the
applicationWillResignActive() and applicationDidEnterBackground() extensionDelegate methods.

So how to check if the watch app is in frontmost state or not?

Upvotes: 2

Views: 2304

Answers (2)

David Pasztor
David Pasztor

Reputation: 54706

According to the documentation of WKExtension, there is no built in method to check whether an app is the frontmost app or not. Even the WKApplicationState enum doesn't have a case for determining whether your app is the frontmost app or not.

Moreover, when an app becomes the foremost app (right after the user lowers their wrist when the app is running in the foreground), the app briefly goes to background mode, then becomes suspended by design. So it is the expected behaviour that the system functions applicationWillResignActive() and applicationDidEnterBackground() are called, since the application actually goes to the background mode.

In watchOS3, the foreground app has no benefits that developers could take advantage of other than the fact that the system resumes app state automatically for the foreground app.

In watchOS 4, the frontmost app gains the following additional advantages:

  • Can play haptic feedback from the background.
  • Wakes from the background to receive notifications.
  • Wakes immediately upon the completion of a background transfer from a URLSession task or Watch Connectivity session.
  • Receives increased runtime priority when using the ProcessInfo object's performExpiringActivity(withReason:using:) method to complete tasks in the background.

Upvotes: 1

Jonny
Jonny

Reputation: 2074

On iPhone, you can check isReachable value of WCSession.

According to WCSession documentation, isReachable is true when:

A paired and active Apple Watch is in range, the corresponding WatchKit extension is running, and the WatchKit extension’s isReachable property is true.

Upvotes: 1

Related Questions