Quit code for watchOS - swift

In Xcode there is a place to set actions when the interface controller is dismissed in:
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
However, my watchOS app needs to perform an action when the app quits completely i.e. the home screen is visible. Not when the watch is simply lowered and the screen is dimmed because the app is still running as a workout app and is performing actions.
Is there a way to do this?

In ExtensionDelegate, you'll find the method applicationWillResignActive.
Sent when the application is about to move from active to inactive
state. This can occur for certain types of temporary interruptions
(such as an incoming phone call or SMS message) or when the user quits
the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, etc.
You can use this method to do what you need.

Related

how can we run GUI code when app is firing applicationWillResignActive:

i want to execute some GUI code ,let suppose I want to make the LED on when app fires
applicationWillResignActive: event ,
how can I do this ?
Can I delay the firing of this event until code executes or there is any other method available for doing this?
No you cannot delay this event being fired
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
You can respond by putting your code in the application delegate method
- (void)applicationWillResignActive:(UIApplication *)application;
or by observing for the UIApplicationWillResignActiveNotification notification

How do i find out what state the application is in as the result of a notification?

I have a UILocalNotification set up, and as far as i can see it i have 5 different scenarios:
The app is not running, the user chooses to view the notification, so it launches the app.
The app is not running, the user chooses to close the notification, then opens the app at a later date.
The app is running in the background, the user chooses to view the notification, so it brings the app to the foreground.
The app is running in the background, the user chooses to close the notification, then opens the app bringing it to the foreground at a later date.
The app is running in the foreground.
How do i deal with these 5 different scenarios?
Put your code into application:didFinishLaunchingWithOptions:. In
the actions NSDictionary you will find the information about the
notification.
You can again check in application:didFinishLaunchingWithOptions: if the local
notification is still active and take appropriate action.
Put your code into applicationWillEnterForeground:
Again the same spot, just check if there are active local notifications.
Here you can check in application:didReceiveLocalNotification: and either notify the user or not.
Not exactly sure what you're after, but the following might answer your question.
From the documentation:
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.

iPhone app startup

How do I make my iPhone app start at the same place each time, i.e. my 'home' screen? I do NOT want the user to return to where they were last time they played - right in the middle of gameplay - but that's what's happening.
Thanks in advance for any tips!
You need to set the UIApplicationExitsOnSuspend key in your info.plist file to YES. Then the app will quit when the home button is pressed, and launch fresh when it is opened again.
This is presumably only happening because your app isn't really being stopped - it's simply being backgrounded. (If you double click the home button whilst viewing springboard does it show up at the bottom? If so, it's still running.)
You can disable this behaviour (so your app quits when the user hits the home button) by setting the UIApplicationExitsOnSuspend key (known as "Application does not run in background" within Xcode) in your info.plist file. See Apple's Information Property List Key Reference docs for more information.
Alternatively, you could simply capture the applicationDidEnterBackground: UIApplicationDelegate method in your app's add delegate and handle the situation from there programatically. (i.e.: Do whatever you need to do to reset your app back to the 'home' screen, etc.)
- (void)applicationWillResignActive:(UIApplication *)application {
[self.navigationController popToRootViewControllerAnimated:YES];
}
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
Set UIApplicationExitsOnSuspend to YES int the Info.plist.

Detect Application States

I want to detect application states and send it to the server. In the new OS4, with multitasking there are some methods available to help detecting the states:
application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:
applicationWillResignActive:
applicationDidEnterBackground:
applicationWillEnterForeground:
applicationWillTerminate:
I read that now, we have to use applicationDidEnterBackground instead of applicationWillTerminate. My problem is that i need them both.
When the user send the app to the background, it has the state sleep. But when the user close the app ( from the multitask bar ) the state is closed. So i need to detect both, when the user send the app to the background and when the user ( or the system ) close it.
Is there anyway or workaround to make this?
I try subscribing to UIApplicationWillTerminateNotification but it doesn´t work.
Thanks in advice.
The application will quit notification is no longer fired on iOS 4 (as I am led to believe).
When the user hits the home button, the app is sent to the background, and you will get the did enter background notification. But when a user closes the app from the multitask bar, or if the system closes it, the app is sent a SIGKIL message and quits immediately, firing no notifications or delegate methods.

UIActivityIndicatorview freezes when application moves to foreground iOS4

I am experiencing an issue with activity indicator freezing when I move application to background and then bring it back to foreground. The application can be loading some data over the network when it is moved to background. Hence I display an activity indicator to the user. I have added the code to make sure that the task finishes in the background.
When the application is brought to foreground immediately and the data is not completely loaded, the spinner stays on the screen but it stops spinning. When the data is completely loaded, the spinner disappears.
Any idea why the activity indicator freezes and what could be a possible solution to it.
Thanks.
There's almost no reason for an app in the background to be updating its UI.
For one, who is there to see it? Another reason that the indicator freezes is that going to the background stops UI updates and animations that needlessly drain the device's battery and slow down the foreground app.
From Apple's excellent iPhone Application Programming Guide, the app delegate method -applicationWillEnterForeground: is called whenever the application comes into foreground.
You can override this method and add logic to wake up widgets here. Or you subscribe your class (view controller, etc.) to listen for the UIApplicationWillEnterForegroundNotification notification. If you listen for this notification, you could call a selector that updates the state of the UI, given whatever the application is doing at the time.