How to implement android's onResume() function in swift? - swift

I'm using an observer like below to run code when the app comes back to the app from the background
However, if the app is run after a day or so, the observer does not run.
Is there a way to make the observer run even if the app runs after a day or two, or to have the same effect as onResume() in android?
NotificationCenter.default.addObserver(self, selector: #selector(enterForeground), name: UIApplication.didBecomeActiveNotification, object: nil)

Related

how to reload NSTableView data from a different ViewController

I'm working on a simple notes app for macOS. I have a home page which is has a NSTableView that displays all your notes, when you click the new note button a new View appears where you can create a new note. Once you click the note it adds the new note to the database and should reload the table view data, but I need to stop the current run and run the program again to see the changes.
I used this post to achieve the same effect on iOS but it seems to not work on MacOS
So how do I adapt:
override func viewDidLoad() {
super.viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
In the home page VC
and the line:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
Inside of the saveNewNote IBAction to work in macOS? also are you even able to use the NotificationCenter in macOS apps or is it only on iOS?
NSNotificationCenter is part of Foundation framework, so it's definitely available on macOS.
You should be able to use it the same way you've been using it on iOS.
If you have an IBAction called saveNewNote, inside that method you can posy the notification the way you wrote.
In the view controller which owns the table, add the observer like you wrote, and reload the table...
If it doesn't work, we might need some code example of how you set it up on the Mac app the better understand what isn't working.

How to detect window resizing in Mac Catalyst?

How can I a get a notification when user resize the window while:
NotificationCenter.default.addObserver(self, selector: #selector(function), name: NSWindowDidResizeNotification, object: nil)
is unavailable in Mac Catalyst
Just like in iOS. Use windowScene(_:didUpdate:interfaceOrientation:traitCollection:) in your window scene delegate, or implement viewWillTransition(to:with:) in your view controllers.

Detect UIViewController changed in Swift

For analytics purposes, I would like to detect whenever the top UIViewController is modified.
I don't want to use inheritance, but rather delegate some event that I can use.
I see that Firebase have some type of mechanism for that but I couldn't figure how exactly.
Something like the following will be great:
NotificationCenter.default.addObserver(self, selector: #selector(topViewModified), name: .TopViewModified, object: nil)
Thanks.

UIKeyboardWillChangeFrameNotification swift implementation

For a long while now, I've been looking for a Swift implementation that utilizes UIKeyboardWillChangeFrameNotification so far I haven't come across anything. I'm trying to get this working so that I can handle the keyboard show and hide in one method as stated in the documentation here and here written in objective-c.
and replace these:
// Register your Notification, To know When Keyboard appears.
NotificationCenter.default.addObserver(self, selector: #selector(myKeyboardWillShowMethod), name: .UIKeyboardWillShow, object: nil)
// Register your Notification, To know When Keyboard hides.
NotificationCenter.default.addObserver(self, selector: #selector(myKeyboardWillHideMethod), name: .UIKeyboardWillHide, object: nil)

Application state and push notification

I have a server that successfully generates push notifications and sends them to devices. On the app side, if the user is already in the app or if the user enters the app from the notification I can easily 'find' the notification and run the necessary code to action the notification (app delegate gets invoked and the notification is stored in NSUserDefaults which I can then access throughout the app).
However, if the user has the app running in the background (for example, user was in the app, but then switched to another app), when the user reopens the app from the app icon, the app simply returns to the last state it was in and no code actually gets invoked (which is kind of what you expect).
What I am trying to figure out is how to either invoke AppDelegate again so that I can extract the notification before I return to the current state or how do I invoke some code so that I can intercept the notification.
Thoughts?
If I understood your question correctly, you want to be notified when coming out of background state. How the appdelegate method
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
Here is my solution.
On 'App Resume', In AppDelegate, I check the badge and store push if badge value is greater than 0:
func applicationDidBecomeActive(application: UIApplication) {
let installation = PFInstallation.currentInstallation()
if installation.badge != 0 {
NSUserDefaults.standardUserDefaults().setObject(true, forKey: "push")
NSNotificationCenter.defaultCenter().postNotificationName("indicatePush", object: nil)
installation.badge = 0
installation.saveEventually()
}}
The call to NSNotificationCenter, invokes a registered method in my base class, where I set the push variable to true
In the sub-class, on viewDidLoad I register two methods (you might only need one of them):
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appplicationIsActive:", name: UIApplicationDidBecomeActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationEnteredForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
Add the call to the 'pull from server' to one of the two methods. In my case:
func appplicationIsActive (notification: NSNotification){
refresh()
}
You're done!