magwyg
magwyg

Reputation: 41

iOS How to receive and store system NSNotification in a SwiftUI application?

I want to write an app where I create a database of all the changes made in one setting from UIKit. The flow in SwiftUI:

  1. UIKit posts a notification (static let Name_StatusDidChangeNotification: NSNotifiaction.Name)
  2. My Swift App saves current DateTime in Core Data - both while in foreground and background mode

How should I design my app to be able to do this?

This is my first time creating an iOS app. From my research I think that:

  1. I need to create DataController : ObservableObject -> to controll the data being loaded and saved to the device.

  2. I need to create AppDelegate: NSObject, UIApplicationDelegate {}

  3. In my @main struct I need to add

@UIApplicationDelegateAdaptor(Appdelegate.self) var appDelegate 

I got stuck in the middle because I do not know how to configure my notification center. Where it should be added?

NotificationCenter.default
                    .addObserver(self,
                      selector:#selector(mySelector),
                 name: NSNotification.Name("Name_StatusDidChangeNotification"),
                      object:nil)


@objc func mySelector(_ notification: Notification) {
//saving DateTime into Storage
}

Upvotes: 2

Views: 227

Answers (0)

Related Questions