10

Is it just my app, or do all Mac apps made with Catalyst not undergo lifecycle changes except when launching or quitting?

Upon launch, the UIWindowSceneDelegate methods sceneWillEnterForeground: and sceneDidBecomeActive: are called. When quitting sceneWillResignActive and sceneDidEnterBackground are called.

But these never take place when the app is left open and I change to a new app, or let my computer sleep, or do other normal human usage patterns that I would expect to force the app into the background state. I'm left with the impression that the app never leaves the foreground state as long as it is not quit by the user. Is this correct?

If I implement support for automatic or sudden termination (as detailed here), might the app enter/exit app states more frequently?

Is there something I am misunderstanding?

Thank you for any help.

(Note: I built a system that logs app transitions – rather 'scene' transitions – so as to be able to test without running the app in Xcode.)

AVS
  • 373
  • 3
  • 19
  • 2
    Theoretically `sceneDidBecomeActive`/`sceneWillResignActive` must work, so submit feedback to Apple. – Asperi Jan 29 '20 at 07:02

2 Answers2

13

In my case, a workaround helped:

#if targetEnvironment(macCatalyst)
   nc.addObserver(self, selector: #selector(hold), name: NSNotification.Name("NSApplicationDidResignActiveNotification"),object: nil)
   nc.addObserver(self, selector: #selector(resume), name: NSNotification.Name("NSApplicationDidBecomeActiveNotification"),object: nil)
#endif

Hopefully in the future there will be a more elegant option using UIScene.

Jury Shortki
  • 151
  • 1
  • 4
  • I did this and I put it into SceneDelegate.swift in `sceneDidBecomeActive` and it worked perfectly – Kurt L. Dec 27 '20 at 01:00
  • I get this: "NSApplicationDidResignActiveNotification' is unavailable: not available on macCatalyst" when I tried to add notification to my AppDelegate of my Catalyst app. – Lewis Edward Garrett Nov 28 '22 at 22:15
4

My scenario is typical: Mac apps made with Catalyst simply do not transition through lifecycle stages as often as they do on iOS.

From this article:

  • State changes, however, do affect the Mac less, as are almost always Foreground + Active on macOS.
  • Apps only enter background during termination and inactive when launching in the background.
  • Your app should still take App Napp into account
AVS
  • 373
  • 3
  • 19
  • I also have a strange behaviour with mac Catalyst. Right after I run my app everything works fine (it's a VoiP app). I can send notifications, accept calls and so on. After I open a couple other applications (and send mine into the background), there is nothing happening (I added a infinite log and even it is not being written), it's like the app is frozen. When I set the focus back on the app everything works fine (and the log gets written too). I checked for "App Napp" but it is turned off. Is there anything else I could try? – rimes Apr 13 '21 at 09:56
  • One thing I also noticed when using activity manager and set it to "Full screen". My app gets down to 0.0% CPU and this is the time where I don't get any notifications. When I just focus the activity manager (but don't maximize it) and my app is minimized, I get 0.5% CPU and the app works as intended – rimes Apr 13 '21 at 10:01