Unable to restore brightness on application exit - iphone

I have a feature in my app (a metronome app for musicians) to dim the screen in order to save on battery life. To set the brightness, I am using:
[UIScreen mainScreen].brightness = 0.1;
I am saving the original brightness on app start up in viewDidLoad(...) and saving that to my User Defaults.
When changing views within the app, I retrieve the original brightness from User Defaults and restore with a call:
[UIScreen mainScreen].brightness = originalBrightness;
This works fine. I have NSLog(...) messages showing the original value, etc... so the mechanism works.
The issue I am having is how to restore the original brightness on application exit as the Home button is pressed.
I have added similar code to my application delegate methods for:
applicationWillResignActive(...)
applicationDidEnterBackground(...)
applicationWillTerminate(...)
They each have a method call to set the screen brightness as before in the view with similar NSLog(...)s showing the retrieved original brightness...and it is all correct. I have also debugged in and the calls to set brightness are being made...but the app exits, and the device screen is still at the lower, dimmed level.
Finally, if you click the "lock" button on top, then press the Home button to wake it up...the brightness is correct.
My suspicion is that whatever action or event is triggered with my call to set the brightness when the app is exiting is not getting through, maybe due to an invalid state or similar.
Also, my app is set to NOT run in the background, set in the info.plist as:
Application does not run in background YES
Any help would be appreciated.
Thanks!

iOS allows that app a little bit of time before exiting. You might try a sleep for a fraction of a second after setting the brightness.

If you read https://devforums.apple.com/thread/139813 carefully it says that brightness changes are not permanent. The original user brightness is back, when you hit the lock button and unlock it again. Actually, I had to restore my app brightness if user hits lock button while my app is running:
- (void)applicationDidBecomeActive:(UIApplication *)application{
[self setBrightness];
}

Not had much joy with this. Workaround was to reset brightness on a ViewController viewWillDisappear. Not a great solution but the only one I have found so far to work (this has been broken for years...)

Related

Does iOS send notifications when the system changes the screen brightness?

Problem is, my reading app has a button that puts it into dark theme mode. The brightness gets reduced by 10%. When the user returns to the normal mode, it is set back to the remembered brightness. But in the meantime the actual system brightness could have changed due to auto-brightness adjustment or even the user going to Settings and changing it there.
The problem with the brightness property is that you only query actual screen brightness and whatever you set is only temporarily until the user locks the screen. upon unlock the system reverts to system brightness.
If iOS would send notifications when the system changes the brightness that would be helpful. Couldn't find anything on this in the docs.
Search for UIScreenBrightnessDidChangeNotification which is part of the UIScreen class. It is available from iOS 5.0 and above.
If you want to set the brightness back to a programatic value when the device is unlocked again, observe the UIApplicationDidBecomeActiveNotification notification and set the brightness back to you desired level in your selector.
I've done some more research on this whole brightness thing and here's what I found:
The UIScreenBrightnessDidChangeNotification gets called ONLY when the system changes the brightness or when the user changes the brightness from the control panel or settings. NOT when you change the brightness programatically.
What I assumed from this Apple Doc here (see below) is that brightness will not change anymore after you set it programatically.
Brightness changes made by an app remain in effect until the device is
locked, regardless of whether the app is closed. The system brightness
(which the user can set in Settings or Control Center) is restored the
next time the display is turned on.
However I found this not to be true (or I'm misinterpreting it). The brightness does change (and UIScreenBrightnessDidChangeNotification does get called) after you set the brightness programatically. However it ONLY gets called when the system thinks it should increase or decrease the brightness (due to changes in the environment brightness) *. If the brightness of the environment stays the same your screen will stay as bright as you set it programatically.
What does this mean? Well, 2 things:
If you want to keep the brightness at the level you set it programatically, no matter environment changes, you have to observe the UIScreenBrightnessDidChangeNotification and reset the brightness to your desired level every time it's called.
If you want to go back to the "system" brightness, well you can't really, because you simply can't tell what the system brightness would be if environment changes took place (because you are resetting it every time). You have 2 options for this.
Remember the brightness before you set your brightness programatically and set it back to that value. Or
Put the brightness to 0.5 and let the system to it's job.
The 'danger' in both situations is that it will stay on the value you set it until an environment brightness change is detected.
*There are 2 special cases... when you set the brightness to 0.0 and the system thinks it should decrease brightness nothing happens because it's already on 0.0. Secondly, when you set it to 1.0 it will stay on 1.0, no matter how the environment changes.

MKMapView showsuserlocation

In my app I have a rare bug that stops showing the userlocation. If I tone the app down and just have the map, and set everything up in the viewDidLoad, and at the end do the typical:
myMapView.showsUserLocation = YES;
everything works great 99% of the time. It always works when the App is starting from scratch, but like 1 time out of 100, when resuming from the background, the userlocation doesn't show. And even if I make a button to turn showsUserLocation back on, it still won't show (and upon doing NSLog's the property shows that it is set to YES in the MKMapView). If I kill the program, and start it again, it works fine again.
Everything is being done in the main thread as well.
Anyone encounter anything like this?
you can refer this image for fetch location in backgroud Mode

To restart the app from the home screen and not from the screen we left at

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.

Restart app when coming out of background

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.

Dim iPhone Screen, but don't let it sleep

Update
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/index.html#//apple_ref/occ/instp/UIScreen/brightness
That's the Apple Doc for controlling screen brightness. Below is the original question.
I have found by using Google that I can disable the iPhone going to sleep in an application by using:
application.idleTimerDisabled = YES;
so the code looks like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
// This disables the autosleep I added this to TEST,
// delete later if you want:-
application.idleTimerDisabled = YES;
[window addSubview:switchViewController.view];
[window makeKeyAndVisible];
Perfect, it works well. However, my question is, can I somehow disable the iPhone from going to sleep, while still allowing the screen to dim? Or perhaps dim the screen myself in the app as to save battery power?
I definitely don't want the iPhone sleeping, but I'd also like to be user friendly/battery friendly and dim the screen. (You know, like how you can set the iPhone to dim the screen X seconds before it goes to sleep.) Is there an effective way to do this?
I'm pretty sure there's nothing in the official SDK.
[(id)[UIApplication sharedApplication] setBacklightLevel:0.3f]; // or whatever value
works, but is of course undocumented. The recent experience with UIGetScreenImage indicates that perhaps the right strategy with useful but undocumented APIs is to use them and see what happens.
Failing that, has anybody ever measured if the phone's power consumption goes down if it's showing a black image, or does it not help unless you can turn down the backlight?
I use a "nightstand" type alarm clock app that, if you double-tap its clock screen, dims the screen by some amount.
I believe what it's probably really doing is laying a partially-opaque black UIView over its entire content. I think the backlight isn't really dimmed, but the black color laid over the screen makes it seem dimmer. It works.
Can you set proximityState to trick the iPhone into thinking that it is close to someone's ear? This would work with the iPhone, but not the iPod touch. There is no way to selectively turn adjust the brightness ... apps that dim the screen typically do so by putting a partly transparent image over the current one.
Starting with iOS 5 you can set UIScreen's brightness:
This property is only supported on the main screen. The value of this
property should be a number between 0.0 and 1.0, inclusive.
Brightness changes made by an app remain in effect only while the app
is active. The system restores the user-supplied brightness setting at
appropriate times when your app is not in the foreground. So if you
change the value of this property, you do not need to record the
previous value and restore it when your app moves to the background.
You can use this approach http://oleb.net/blog/2014/02/alarm-clock-apps-ios/
Just add a parameter into info.plist. And your app still run when your screen dim, your device locked.
When you debug, your app still run and never dim automatically, you just press lock button to test.
When you run your app without debug, you can see this feature do, like Music app of Apple, the music still play when device go to turn off screen.