0

How can I detect with watch os when iOS app is closed? I use swift and watchConnectivity.

Also I use swiftUI

2 Answers2

0

On Application Terminate

There is a method as such

applicationWillTerminate(_:)

*Note By Dávid Pásztor - You will only have 5 seconds to get your message to the watch or it will not work, so make sure the message you send to the watch will be small and fast.

This method will detect that the application is being closed and will be cleared from memory. You can then add some connectivity to notify the watch.

Documentation Here: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate


On Application Move To Background

Alternatively, if you want to simply notify the watch whenever the app goes to background, aka goes inactive you can use this method.

applicationWillResignActive(_ application: UIApplication)

Documentation here: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622950-applicationwillresignactive

xTwisteDx
  • 2,152
  • 1
  • 9
  • 25
  • 2
    Be aware that `applicationWillTerminate` only allows 5sec of runtime for your iOS app, which might very well not be enough time to communicate over Bluetooth with your watch app. – Dávid Pásztor Apr 02 '20 at 13:29
  • 2
    It's not working. I now some app this problem solved but how? –  Apr 02 '20 at 17:11
0

If you are using SwiftUI

SceneDelegate.swift :

func sceneDidDisconnect(_ scene: UIScene) {
    sendMessageToWatch()
 }
dawis11
  • 820
  • 1
  • 9
  • 24