iosswiftuiwidgetliveactivity

IOS - Live Activities In SwiftUI is reopening live activity when i close and reopen the app


I have an app in SwiftUI. When i enter the app i create a simple live activity. Everything works as expected. When I close the app the live activity still works and that's the intended behaviour but when I reopen the app another live activity is created.

How can I get a reference to the first live activity that i created so I would not have two?


Solution

  • You can access the currently running live activities on app start with

    Activity<ContentAttributes>.activities (optionally filter them or use the first one if you only have one). Only if there is no activity running you should start a new one.

    See Apple's documentation for more info.