When the application is sent to the background, i need the application to shutdown completely. Presently, when my app goes to the background, and when i call it back it shows the view i was working before it went to the background.
I am doing this to release memory.
How can i do this programatically ?
Add a key UIApplicationExitsOnSuspend to YES on info.plist
More info here
https://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html
Go to your configuration (e.g plist file) and add the following tag :
UIApplicationExitsOnSuspend and set it to true
Do not prevent your app from multitasking just because you have memory issues. What you should do is optimize your code and correct these issues.
Look at this guide and the methods that it mentions, they will allow you to know if your application is going to the background for example so that you can release some memory.
Related
My application triggers the iPhone Dialer and goes to background via the App Delegate.
Somewhere in the AppDelegate.m file this is called:
- (void) applicationDidEnterBackground: (UIApplication *) application
{
[self terminate];
}
When I am finished with my external app (Dialer) the application is launched again but most of the GUI parts are unresponsive.
Does it ring a bell? How could I debug this issue? Let me know if you need more info.
To be more helpful. The application is an address book that each entry is dialable and when click the iOS app is called. During the call I want to be able to open my app to browse information
I am assuming that your main issue here is that you do not consider the effects of being suspended. Please see this question: iOS 4 resume from background
Then consider reading up on an apps state cycle, which you can find here
Among the things of importance found in this article are:
To help reduce your app’s memory footprint, the system automatically purges some data allocated on behalf of your app when your app moves to the background.
The system purges the backing store for all Core Animation layers. This effort does not remove your app’s layer objects from memory, nor does it change the current layer properties. It simply prevents the contents of those layers from appearing onscreen, which given that the app is in the background should not happen anyway.
It removes any system references to cached images. (If your app does not have a strong reference to the images, they are subsequently removed from memory.)
It removes strong references to some other system-managed data caches
In other words, the state of your application might not be the one you had when you were put in the background. The entirety of this process is too large to elaborate here, and should instead be researched through the documentation provided by Apple, which I have linked to above.
You get applicationdidEnterBackground when you go into the background, but when that happens, you're trying to quit your app completely? Better than doing that there's a info.plist key that will tell the OS not to enter the background "application does not run in back ground." That will terminate your app much more gracefully.
When we play our app on the device(ios 4.0 and above) and leave mid workflow, and return later, we return mid workflow. I want that it should not replay from the same left point rather the main screen should load(i.e, the same thing that happens on calling viewDidLoad of our rootViewController).
Please give suggestions if it is possible and if not then please post the reason behind that.
Here's an alternative approach, disable background mode and restart your app every time it enters the foreground: add UIApplicationExitsOnSuspend key to your app's info.plist and set its value to YES.
UIApplicationExitsOnSuspend
UIApplicationExitsOnSuspend (Boolean -
iOS) specifies that the application
should be terminated rather than moved
to the background when it is quit.
Applications linked against iOS SDK
4.0 or later can include this key and set its value to YES to prevent being
automatically opted-in to background
execution and application suspension.
When the value of this key is YES, the
application is terminated and purged
from memory instead of moved to the
background. If this key is not
present, or is set to NO, the
application moves to the background as
usual.
This key is supported in iOS 4.0 and
later.
So actually you want to quit the app on Home button press and restart?i.e turn multitasking off?
Search for info on UIApplicationExitsOnSuspend
Yes you call your rootviewcontroller from applicationDidUnhide to do so. You'll have to twist your logic a little (have to reset your variables on this method call).
Edit: You should instead use applicationDidBecomeActive. Reference is on same link.
I've seen many news apps do this, where when you click on the app (on the iphone desktop) that has been in background mode, it starts again by showing a loading screen, etc. It basically looks like it starts again like it was never in background mode (I think what is called "refreshing the user interface). Does anybody know how you do this? I assume this is call from the app's delegate applicationWillEnterForeground or applicationDidBecomeActive method.
The application probably never went into the background. Try adding this to the Info.plist file.
UIApplicationExitsOnSuspend=YES
As a boolean value.
Here are my issues,
When I install my application on my test device it has the behaviour I want.
However if I close it with the IPhone main button and restart with the icon, it starts back from the view where I left it, whereas I would like it to restart from my main view controller (my start view).
In the same way, I load some animations with viewDidLoad in certain views. I want them to show only the first time the view is loaded each time the application is launched. Right now animations only works the first time the application is launched after installation, then they don't screen anymore when I launch again the application.
Does anyone have a clue ?
Thank you very much for your help.
(Sorry if this topic is a bit easy for you guys :D, I'm quite new at it !)
No problem about being new! This is happening because, for devices from iOS 4.0 on up, your app will support multitasking by default. To disable this features, Add the key
UIApplicationExitsOnSuspend
to your Info.plist file, and set its value to YES. Good luck!
In addition to Sam's answer above, you can also add a key named:
Application does not run in background and set the value to YES.
Both work fine, however.
Is there any way to cause an app to quit instead of going to background when the home button is pressed? For security reasons, it would be better if the app did not run in background but actually closed when home is pushed. This is not for users' security, but rather for company data on the app, so it is not a user's choice. I could not find any way to quit other than forcing an exit, which Apple discourages.
See opting out of background execution in the iphone application programming guide:
"If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES.
When an application opts out, it cycles between the not running, inactive, and active states and never enters the background or suspended states.
When the user taps the Home button to quit the application, the applicationWillTerminate: method of the application delegate is called and the application has approximately five seconds to clean up and exit before it is terminated and moved back to the not running state."
Just go to info.plist of your project and check "Application does not run in background" to YES.
Unfortunately after trying out everything, I was still not able to see my application exiting on pressing home button. It always went into background even though UIApplicationExitsOnSuspend was YES and of type boolean in plist file and I removed the application from Simulator, restarted Xcode and Simulator and tried everything suggested.
Finally I started debugging the application and found one function which was preventing the application from exiting. The function was fairly simple and was downloading some images from network and was getting called from applicationDidFinishLaunching of appDelegate file. This function was delegating the task of creating network connection and downloading data to some other reusable class where I had the below code:
if(isBackgroundProcessingSupported){
appDelegate.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{}];
[NSURLConnection connectionWithRequest:request delegate:self];
}
So finally it turned out that the above code was responsible for putting the application into background on press of home button. When I commented the above code, my application is exiting instead of going into background.
NOTE: The code was there earlier as initially application was supporting background processing.
Hope this helps someone who is also struggling to find the reason like me.