In my application at some parts I am changing brightness of the screen, and turning back to default value on some pages but if the user presses home button and exits from my application, when the brightness of the screen was changed I can't return the screen brightness back to its value.
I put code to return brightness to default on these delegate calls but it also didn't work
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
In some sites I read it is a bug of IOS, actually not bug but it doesn't give you permission to change the brightness: OS takes the control of it after home button is pressed.
But passbook app can do that so does anyone know is there a solution for this or passbook can do that because it is written by apple?
If the issue is indeed that the brightness cannot be changed from the application delegate methods (and I'm not saying that that is the problem; I'm not sure), you could instead try changing it from view[Will/Did]Disappear on the relevant UIViewController.
Related
In my app I am increasing the brightness of the screen at some part and of course I need to return it back to its value before exiting my app. But I can't catch if the user uses the button and puts the app to the background. Is there any way to understand if the app will work on background so at that point I can change screen brightness to its old value.
You need to implement your code by using the following AppDelegate methods, and this is the way to go
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
For sake of completion I will add that you can do it in your application delegate like others have already said so.
But some time it don't make sense to do this in the application delegate, and for those case there is NSNotification for that like : UIApplicationWillEnterForegroundNotification. You will find those notification at the bottom of the UIApplication class reference.
Implement code in
- (void)applicationWillResignActive:(UIApplication *)application
and - (void)applicationDidBecomeActive:(UIApplication *)application
in your appDelegate class
I'm making an app that keeps track of some reminders that repeats with an user defined interval.
I've made it so when the alert displays, the action title says "Renew". When you click this, the app opens, and here I want to create the next reminder, but the problem is that I don't know if the app opens because the notification button was tapped or if the notification fired while the app was running.
Anyone got any ideas?
The 'correct' way to do this is to examine your NSApplication's applicationState property in the application:didReceiveRemoteNotification: method of your delegate.
From the documentation for handling local notifications:
iOS Note: In iOS, you can determine whether an application is launched
as a result of the user tapping the action button or whether the
notification was delivered to the already-running application by
examining the application state. In the delegate’s implementation of
the application:didReceiveRemoteNotification: or
application:didReceiveLocalNotification: method, get the value of the
applicationState property and evaluate it. If the value is
UIApplicationStateInactive, the user tapped the action button; if the
value is UIApplicationStateActive, the application was frontmost when
it received the notification.
This is similar to your solution using flags set in applicationWillEnterForeground and applicationDidBecomeActive but with system support.
I don't know if my question was unclear but it seems that I got 4 different answers that all misinterpreted my question :P
However, I discovered that the didReceiveLocalNotivication happens between willEnterForeground and didBecomeActive, so I did this to determine if the app was already open or not:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Opened from notification? %#", wasInactive ? #"yes!" : #"no!");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
wasInactive = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
wasInactive = NO;
}
Look up the documentation for UIApplication launch option keys. The last parameter to your application:didFinishLaunchingWithOptions: delegate method contains the information you need.
Also, look at application:didReceiveLocalNotification.
You're looking for
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
or
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Documentation
If your application is already running you'll get this delegate message on the app delegate
application:didReceiveLocalNotification:
If it wasn't running you'll have to use
application:didFinishLaunchingWithOptions:
You need to respond appropriately in both methods to cover all cases
UPDATED
To detect if the user activated the action button requires a little more complexity. We can tell that application:didFinishLaunchingWithOptions: will have the local notification as a launch option, but it's more difficult with the application:didReceiveLocalNotification:.
Since the application becomes active after the user taps the button, we have to defer until we see that message (or not). Set an NSTimer in application:didReceiveLocalNotification and cancel it in didBecomeActive. That means the user pressed the action button. If the timer isn't cancelled the user was inside the app when it fired.
In your app delegate in this method:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
You have to examine the launchOptions looking at this key:
UIApplicationLaunchOptionsLocalNotificationKey
When you are already active this will be called:
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
I already use the:
(void)applicationDidEnterBackground:(UIApplication *)application {}
method, but I can't differentiate if is because press the home button or the on/off button.
Thanks in advance,
For the on/off button(or an incoming call or SMS):
- (void)applicationWillResignActive:(UIApplication *)application
For the Home button:
- (void)applicationDidEnterBackground:(UIApplication *)application
With the notification of applicationWillResignActive, applicationDidBecomeActive will still enter while you are entering in background. But there is a way to differentiate by getting the state of the app, so try this in applicationDidEnterBackground.
- (void)appHasGoneInBackground {
bool inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground;
// lockScreen state
if (!inBackground) {
// do something
}
}
Apple's UIApplication-class reference
Use - (void)applicationDidEnterBackground:(UIApplication *)application {} when your app is entering the background (home button) and - (void)applicationWillTerminate:(UIApplication *)application when it's about to be closed (on/off button or iOS call to close after a random time in background).
My understanding is that when you lock or unlock your iOS device your application delegate will call - (void)applicationWillResignActive:(UIApplication *)application and - (void)applicationDidBecomeActive:(UIApplication *)application, respectively. Locking and unlocking are similar to receiving an interruption like a phone call. Sending your application to the background by hitting the home button calls different methods, namely - (void)applicationDidEnterBackground:(UIApplication *)application and - (void)applicationWillEnterForeground:(UIApplication *)application.
I have an iPhone application which I'm working on and so far i have created a login page for the application such that only the phone's user can access the application. The login works fine and the application works fine, but when i hit the home button, the app is "minimized". And if accessed again from the task switcher it doesn't prompt for a password.
What exactly would be the best way to go about getting the app to request a password. If it helps i use navigation controllers and i have one view dedicated for the login.
Thanks for the help.
Can't you check app activated in the appdelegate?
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self showLoginWindow];
}
You must be working on iOS4. When you hit on Home Button, your app goes into background mode, but still working. So next time, when you open it, it starts exactly from where you left it. Implement the
- (void)applicationDidBecomeActive:(UIApplication *)application
method, to check for login again. Or if you don't want the background processing at all, disable it or use following code
- (void)applicationDidEnterBackground:(UIApplication *)application {
exit(0);
}
EDIT -
Don't go for exit(0) approach, simply disable the background processing if you don't want it from info.plist file.
see this link
you need to prevent the app to load from background.
Is it possible for my app to "take over" when a system alert pops up? My app disables the idle timer, but when a system alert pops up, the alert seems be enabling the timer. What can I do about that?
Would it be possible for you to hook into one of the methods that gets called when your app goes in and out of the background? Say, one of these?
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"applicationDidBecomeActive");
}
I bet one of those gets called when that happens. You can probably disable the idle timer when your app gets the focus back.
EDIT: On re-reading the question, it looks like you're looking to suppress the alerts (i.e., not let them happen). Heh, I focused on the latter half of your question.
Are you trying to suppress the alerts? Then the answer is: you can't.
If you're trying to prevent the alerts from messing with your app's disabling of the idle timer, then I believe donkim is on the right trail.