iOS lock event? - iphone

Is there a way to run an action when the iPhone gets locked?
I simply want to run a little bit of code when the iPhone gets locked by the user. I'm looking for a solution that works without a jailbreaked iPhone.

https://stackoverflow.com/a/7888507/267892
May be you need to implement following methods in AppDelegate:
Tells the delegate that the application is now in the background.
- (void)applicationDidEnterBackground:(UIApplication *)application
Tells the delegate that the application has become active.
- (void)applicationDidBecomeActive:(UIApplication *)application
Tells the delegate that the application is about to become inactive.
- (void)applicationWillResignActive:(UIApplication *)application
See UIApplicationDelegate Protocol Reference for more info on these.

Take a look at UIApplicationDelegate Protocol Reference
especially applicationProtectedDataDidBecomeAvailable:
and applicationProtectedDataWillBecomeUnavailable:

Related

Detect app opening?

I have a Tab bar app with 3 view controllers and I use "viewWillAppear" to detect which view is opend.
When I close the app, it still works "in the background" and when I open the app again "viewWillAppear" is not detecting this opening.
Is there any other option to detect this opening? Thanks
You can observe the UIApplicationWillEnterForegroundNotification or implement applicationWillEnterForeground: in your app delegate.
Firstly, You should see the necessary delegation method in UIApplicationDelegate
When you close application that currently open, It will call this method:
- (void)applicationDidEnterBackground:(UIApplication *)application
After the application has been closed but still in dock, you open them again. In the transition state before entering the application, It will call this method:
- (void)applicationWillEnterForeground:(UIApplication *)application
When the application completely presented on previous state before you closed them. It finally call thid method:
- (void)applicationDidBecomeActive:(UIApplication *)application
If you would like to do something in viewWillAppear you should implement in applicationDidBecomeActive to send some message to your current view or other to do what do you want to do after application became actived.
When your app is resumed from the background, it will receive the applicationWillEnterForground: method. It'll get the applicationDidEnterBackground: when it is suspended too.
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"app will enter foreground");
[viewController refresh:NULL];
}
i think this will work.
write this in your app delegate

How can I know if my apps come from background or are already in foreground

I want to handle APNs differently whether my application come from background or is already in the foreground. Do you know what methods could help me ?
These are the messages that handle the notification about a changing application state.
– application:didFinishLaunchingWithOptions:
– applicationDidBecomeActive:
– applicationWillResignActive:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
– applicationWillTerminate:
– applicationDidFinishLaunching:
You must implement the one's you need in the designated Delegate! Take a look at the "Task" section of the following link for further assistance, especially the "Monitoring application state changes" part ;-)
Apple's UIApplicationDelegate Protocol Reference
- (void)applicationDidBecomeActive:(UIApplication *)application
It is a method which is executed when app comes to foreground from background. So you can write your code here under the above method about whatever you want to do when app comes to foreground from being in background.
(void)applicationWillResignActive:(UIApplication *)application;
(void)applicationDidEnterBackground:(UIApplication *)application;
(void)applicationWillEnterForeground:(UIApplication *)application;
(void)applicationDidBecomeActive:(UIApplication *)application ;
Please have a look at these methods written in delegate.m file
You must implement the application:didReceiveLocalNotification: method. This will be called for both cases. You can differentiate between the cases by checking the applicationState property of [UIApplication sharedApplication]: if it's UIApplicationStateInactive then the app was in background, and if it's UIApplicationStateActive then the app was in foreground.

iPhone Application Themes

I tried using a multi-value settings bundle to change the view. I would do the if statements in the applicationdidfinishloading in the application delegate. Apparently the method isn't called every time the app is loaded, and it would not work correctly.
If anyone has done this, or has any suggestions, links to tutorials. I would really appreciate it. I'm just trying to load views (nibs) based on user preference.
I think you can put your code in
- (void)applicationDidBecomeActive:(UIApplication *)application
or
- (void)applicationWillEnterForeground:(UIApplication *)application
methods also because from iOS 4.0 due to multitasking your app is just in the background state so it wont call applicationdidfinishloading method when the user presses the icon of your app again.

Discovering if a component is being displayed (of if it's in background), is it possible?

I need to run a method everytime the user switch the screen or return to the application (after a call, or pressing the "Home Button" and returning to the app, for example).
Is there any way to recognize this, like an event or method that is always executed when this happens?
Thank you guys in advance.
Implement those 2 methods in the UIApplicationDelegate :
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application

ApplicationWillTerminate in iOS 4.0

The applicationWillTerminate delegate method is not getting called in iOS 4.0
When I hit the Home button I am seeing the applicationWillResignActive and applicationDidEnterBackground delegate methods getting called.
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
And when I double Tap the Home button and again launch the Application the i find the applicationWillEnterForeground and applicationDidBecomeActive delegate methods are getting called.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Application Will Enter Foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"Application Did Become Active");
}
But I want to know when the applicationWillTerminate delegate method will be called , where I do some DB/file backup routines.
- (void)applicationWillTerminate:(UIApplication *)application{
}
I even tried to hit the minus sign and deleted the App running in the Background , but still it did not call any delegate method.
Any Ideas ???
From the iPhone Application Programming Guide:
Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate: method of the application delegate. Your application cannot request additional background execution time from this method.
So yes, applicationWillTerminate: will generally not be called very often in iOS 4. If you have to save data, you should do so in both applicationWillTerminate: and applicationDidEnterBackground:.
The WWDC 2010 Session Adopting Multitasking on iPhone OS (Part 2) explains the application state transitions extremely well.
I got one solution for terminating apps when user hits the Home button in iOS4.
This will call the applicationWillTerminate delegate method instead of entering into background process.
Open your info.plist file
Add The Key UIApplicationExitsOnSuspend
Set the new key to YES
Actually you can also use this step to do so.
Open your info.plist file
Add The Key -> Application does not run in background
Set this key value to YES