Do action upon exit of ios app - ios

How can I assign actions on iOS when a user presses the home button (exit)? I want this for an app where uses user a login feature and I want, upon exit, the user to log out. I dont want to use a button for this.

Use the method,
- (void)applicationDidEnterBackground:(UIApplication *)application
In your application's delegate.

You can't exit an application programmatically, you can use exit(1), however it's not a good practice, chances are there for your apps to get rejected from Apple.
applicationDidEnterBackground will be called for home button press. Try to handle everything here.

Write your actions in appDelegate's "applicationDidEnterBackground" method.

Related

greying out the view controller if the application did enter background

I have built an ios application that is quite data sensitive, if the user exit the app, I need to make sure the input data are all gone. for example , if the user keying in the username and password, and exit the app, it need to empty out the fields so that after user enter again he will have to reenter data. The problem now is the login view controller is in swift , while the app delegate is in objective c. how should i do this?
You could use the UIApplicationDelegate methods
precisely the - (void)applicationDidEnterBackground:(UIApplication *)application; will be called when the app is going into the background ( user clicks the home button)
Find the documentation on this page applicationDidEnterBackground

How to show alert when the app is being closed in ios

When my app is running in foreground, If user clicks on home button i want to show some alert like
Do u want to exit the app with Yes/No buttons. Is there any delegate methods called when home button is clicked…
Thank you….
There is no way to do this on iOS. I think you want a similar behavior to Android back button.
Think of iOS home button as Android home button, which doesn't let you prompt user either.
You can not stop the app to go to background, even if you show the alert message, it will go to the background anyways when you hit the home button and you won't see the alert message either.
There is no delegate method that would trigger when the app is going to the background, but there is a delegate method that will trigger when your app has totally gone to background.
Just to show you visually-
In the AppDelegate implementation file(.m), I added an alert message at the end of applicationDidEnterBackground method and put a break point just to check if it is entering that block as I expected. See the result--
You can use these methods in AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

Call action when home buttom pressed iOS

Is there any way to call action on home button ?
I need something like "are you sure to quit app" and wait for yes or no.
I don't need any code or so. I just need to make sure it is possible.
Thanks
In short, no. you can't block the home button action, neither the user is expecting this to happen.
If you have to perform any kind of saving, you can do it in the application delegate object in the method applicationDidEnterBackground:
Apple documentation here
As far as I know, Apple does not allow you to do this, as it would make the overall user experience of iOS horrible. Can you imagine if every developer added this functionality to their app?
You could attempt to implement applicationDidEnterBackground, then set up a local notification that instantly notifies the user with an alert, with a button that opens the app and one that does nothing. But you cannot intercept the home button press like you would with shouldPerformSegueWithIdentifier

Launch iOS App on long press of home button / volume button(s)

If the above scenario is possible then immediately I need to Send SMS/invoke service call after my app is launched.
The above scenario is not possible. It is an OS level functionality. But you can do what ever you want when your app is launched either in the application:didFinishLaunchingWithOptions method in your AppDelegate or the viewDidLoad method of your rootViewController.

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