Im trying to find a way to save data before my app enters multitasking, i know how to save data, but i dont know what method to use to save it before the app enters multitasking.
-(void)applicationDidEnterBackground:(UIApplication *)application{
this one? because i have everything set up to save, but it doesn't seem to be saving it, and im positive that its the multitasking part because it works on the ios 3 simulator. Do i have to save it in the delegate?
Thanks,
Jacob
EDIT: On the IOS 3 one i have it saving data in the
- (void)applicationWillTerminate:(UIApplication *)application {
Yes Jacob, You have to save it in the application delegate of your app.
The method -(void)applicationWillResignActive:(UIApplication *)application gets called when the device enters standby mode or when the app is switched to background.
So the saving part you can add to this method .
Also to make the app save data for iOS versions < 4.0 , add the saving code to applicationWillTerminate: also.
You should use the method what is the method Apple recommends
-(void)applicationDidEnterBackground:(UIApplication *)application
but you can also use
- (void)applicationWillResignActive:(UIApplication *)application
which is the method that is called instead of
- (void)applicationWillTerminate:(UIApplication *)application
Take a look at this Delegate method:
-(void)applicationWillResignActive:(UIApplication *)application
This allows you to react when the application becomes inactive. See the Docs for more information.
Happy coding :)
Related
is - (void)applicationDidEnterBackground:(UIApplication *)application capable of making calculations every day at 10AM, even when app is closed complitly (killed with task manager).
Sorry for stupid question but I don't want to start making something pointless. :)
Sort of. You can implement background fetching for your app in this method
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
However, you can't just implement the method. You have to be sure to enable background fetching for your application. Background fetching
Note that you can choose one of two options
1: Have the iOS schedule the fetch for you based on when the user usually opens your app
2: Request a time interval between fetches. Not guaranteed to execute when you want though, just a request.
Could any help in creating a Unity3D App which could request some data from some webservice (let say every 5 mins) and send a local notification while the game app is running in the background.
You can use the AppController class that the Unity player creates. You need to respond to some of these messages:
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
These are called when the application goes from the background to active and vice-versa. If you want to communicate with unity, you can pass some info with:
UnitySendMessage("GameObjectName1", "InBackground", "Message to send");
If you send the message in (void)applicationDidEnterBackground:(UIApplication *)application
, the method InBackground() will be called. You can have coroutine in that method with is called in every fixed time. This is a theory. Try to implement it.
I would like to show a userID/password view after the app is backgrounded for security purposes.
How should I implement it? Should I be using - (void)applicationDidEnterBackground:(UIApplication *)application?
I don't know about the security reasons but as I tested in both
- (void)applicationWillResignActive:(UIApplication *)application and
- (void)applicationDidEnterBackground:(UIApplication *)application
None of these can show UIComponent during application is have been resigned. May be local notification can help you with this but these too wouldn't be able to do exactly what you want.
I am working on an app that keeps track of user's location at a time-interval set by the user himself(e.g. every 5 minutes) and sends it to a server page by ASIHTTPRequest.
This app should be able to receive updates on foreground, either on background or even when the app is not running(location services).
Although my app successfully receives updates on the foreground and background, it does not seem to wake up when it is not running and do not send me up any requests to the server.
I am using CLLocationManager and its delegate to perform startMonitoringSignificantLocationChanges for when it is on the background.
On Settings, the icon for my app in Location Services appears with a purple arrow as expected.
On my info.plist, I have Required Background Modes set with an item locations as required and methods:
locationManager:didUpdateToLocation:fromLocation:
locationManager:didFailWithError:
implemented.
My method:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
is also implemented and checks whether launchOptions
contains UIApplicationLaunchOptions- LocationKey before starting the significant locations monitoring for when it should wake up / relaunch.
Is there any way I can find out whether my app is really being relaunched?
Is there any extra config that needs to be set in order this to work?
Please let me know if I should provide any additional info.
Specs I am using:
SDK Xcode 4.2.1
CLLocation is inside a Singleton class (read somewhere this might impact)
iOS deployment target: 4.3
Tested on Iphone 3GS,4 and Xcode's iOS 5 simulator, same behavior happens to all of these devices.
Devices:universal
UPDATE
I inserted my code inside -[UIApplication beginBackgroundTaskWithExpirationHandler:] and tried to check against errors with UIBackgroundTaskInvalid. However, it does not even seem to enter the (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
I am keeping track of the app by trying to save and retrieve data by using SQLite. When executing on both foreground and background, it records the data with no problem. When the app is not running, no data is saved at all.
How are you currently checking to see if the app relaunches? Are you logging anything?
The app will relaunch to the background, so you won't see your app come alive. It will actually shut down automatically after some time again. You can do some work and request more background time using -[UIApplication beginBackgroundTaskWithExpirationHandler:].
Another answer here on SO posted some example code. From your description I cannot see anything missing, compare your code to that project to see if your missing something.
I am making a client server application for the iPhone and would like to know which method is called when the iPhone application is terminated. Any help would be appreciated.
The method relating to application lifecycle are UIApplicationDelegate methods. The two you want are:
- (void)applicationWillTerminate:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
If on a multitasking device, applicationDidEnterBackground: will be called instead of applicationWillTerminate:. In most cases, you can perform the same code in both callbacks.
- (void)applicationWillTerminate:(UIApplication *)application
in your appdelegate
The method applicationWillTerminate gets called when your application is being shut down. But the applicationDidEnterBackground/applicationWillResignActive methods are (now) infinitely more useful.
-(void)applicationWillTerminate:(UIApplication *)application in your application delegate will be called. Check this blog post with chart that describes in detail what messages will be sent during launch, termination and when transitioning between background and foreground.