Swift view method call on app exit - ios

I'm currently working on an iOS app with Swift and I've run into a bit of a problem with regards to the view appearing and disappearing handling.
I'm aware that by overriding viewWillDisappear you can handle when within the app the view is being exited out of.
I'm also aware that in the AppDelegate you can implement code in applicationWillTerminate to do certain things when the user exits out of / closes the app.
My question is the following: is there a way for me to know that the application will terminate from my view controller code?

You can have your controller register for a UIApplicationWillTerminate notification, which is delivered under the same conditions as applicationWillTerminate.
See the UIApplicationDelegate docs for more details about the timing of state transitions.

Related

How to know when the user has started an Apple Watch app?

I would like to know when the user starts my Apple Watch app (started from menu or from complication). WKInterfaceController's documentation states that the didAppear method is called when the interface controller content is on screen.
In my simple example project I'm logging all calls to the didAppear method, and I see that it gets called also when the app is not visible on screen.
override func didAppear() {
super.didAppear()
log("didAppear") // Triggered when app not visible
}
My guess is that this has to do with snapshot refreshing, but is there any way to know when the user (not the system) has started my app?
Use applicationDidBecomeActive for this.
From Apple Developer Documentation:
WatchKit calls this method to let you know that your app transitioned from the inactive to the active state. Use this method to start any tasks that were paused or not yet started while the app was inactive.

Perform action when opening app for the second time

When I open the app it fires the events viewDidLoad and viewDidAppear form my View Controller but when I close it and run it again it does not call any of them.
Any idea?
You need to read up on application states. Here is a link I found online outlining the different states:
http://www.techrepublic.com/blog/software-engineer/understand-the-states-and-transitions-of-an-ios-app/
What you really want is to be notified when your app becomes active.
Probably the easiest way is to implement the function applicationDidBecomeActive() in your app delegate. That will be called when your app becomes active as the foreground app either on launch, or when it returns to the foreground as the active app.
Note that if you want that notification sent to some object other than the app delegate you can listen for the UIApplicationDidBecomeActive notification.

Recovering from App Being Killed in Background - Which Methods Are Called on Restart?

I have an app which I want to recover from being killed while running in the background.
What seems to happen is that if the app is killed in the background due to memory pressure, on re-entering the app the app returns to the root view controller.
It appears from testing that neither viewWillAppear or viewDidLoad are called on the root view controller in this case, therefore I cannot execute any code this way on resume.
My question is which methods are called in the above scenario and, ultimately, how can I send a message to the root view controller to handle the case where the app is restarting after having been killed in the background ?
If your app is killed due to memory pressure then you will be re-launched fresh. This is certain.
If you want to restore your state in that case it's up to you.
You can use the state restoration facilities built into iOS 6. I haven't used them yet so I don't have specific tips for you.
You need some way to save your navigation state (modal VC that are on-screen and/or navigation stack) as well as the state data for each VC that's on-screen) plus any global application state data.
UIApplication Protocol reference
https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#jumpTo_9
delegate method didFinishLaunchingWithOptions is called when application restarts

Segue to a view controller when app opens from a NSLocalNotification

I need a way to segue way to a view controller when the app opens from a NSLocalNotification.
I don't really know where to start and I can't find any examples on how to do it online. Any suggestions are appreciated.
Where to start:
Do you know how to initiate a segue through code? If not, see the UIViewController docs and search around here for prepareForSegue and performSegueWithIdentifier There are lots of hits. Also this is a good tutorial: iOS 5 Storyboard: How To use Segues, Scenes and Static Content UITableViews but there are many out there.
Have you worked with UILocalNotifications? See the UILocalNotification Programming Guide for an overview.
In terms of handling the event, that is done by the UIApplicationDelegate See the event method: application:didReceiveLocalNotification:
Local notifications are similar to remote push notifications, but
differ in that they are scheduled, displayed, and received entirely on
the same device. An application can create and schedule a local
notification, and the operating system then delivers it at the
schedule date and time. If it delivers it when the application is not
active in the foreground, it displays an alert, badges the application
icon, or plays a sound—whatever is specified in the
UILocalNotification object. If the application is running in the
foreground, there is no alert, badging, or sound; instead, the
application:didReceiveLocalNotification: method is called if the
delegate implements it.
How you handle a UILocalNotification will depend on the state of your app: foreground, background, inactive.

what is the function that is called when the app is appearing?

Imagine the app is running and you press the iphone button (the phone button) and you exit the app. then you tap on the app again to enter the app. My problem is that when ever the user does this I want the viewWillAppear or viewDidAppear functions to be called, but unfortunately none of these functions gets called.
I want to know if these function won't get called, then what is the function that is called when the app is appearing again?
How about - (void)applicationDidBecomeActive:(UIApplication *)application in your UIApplicationDelegate?
Look at UIApplicationDelegate. -applicationDidBecomeActive: is what you are looking for.
You can also register for notifications in your classes (UIApplicationDidBecomeActiveNotification). This may be simpler to implement than having your app delegate handle everything since you can have, for example, each view controller manage itself.
(Use NSNotificationCenter's -addObserver:selector:name:object: to register, don't forget to unregister during object cleanup, typically in -dealloc.)

Resources