Dim iPhone Screen, but don't let it sleep - iphone

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.

Related

iOS - How to show hints for gestures for iOS app?

I have seen some apps where when you launch them for the first time after downloading (e.g. Chrome app on iPhone), it shows you a list of animated gestures on the screen, kind of giving you a tour of the app.
How do I build one something like that? And how does the app know to launch only for the first time after download and not since then? For the second question, I am guessing a "shown=TRUE" value can be saved inside a PList file and checking the value each time when the app finished launching. But I am more curious about the mechanism involved in creating a guided app tour.
You can use transparent and semi-transparent images with a UIImageView, so you can make up an image with arrows and notes and put over the whole screen. You could fade it out when the user taps.
To know if it's the first time running the app, you should use NSUserDefaults instead of a plist; it's much easier, and you should be app to find a quick tutorial on that fairly easily.
Also, you could check around on this site for controls like this one. I haven't used any of them myself, so I'm not sure how much they differ from a regular UIImageView. They look nice though.

how to catch the power button and home button on iPhone?

Today I went to job interview, I encountered three questions now.they hope i provide some Solutions or
Thinking.
1.when user press Home button,app don't go back the main interface or delay 5 seconds to go back the
main interface.
2. when the screen is black ,how to auto activation screen(how to catch power button press event) .
3.how direct launch my app when iPhone is start-up
My english is very bad, i hope everyone can understand what i said above.Thank you very much
None of these are possible on an iOS device, unless it's jailbroken.
Q1 and Q2: not possible
Q3: The closest you can get to desired behaviour is kiosk (store demo) mode of operation.
See more here:
Lock-down iPhone/iPod/iPad so it can only run one app
It's not exactly what you're looking for - it limits the device to use only certain app - but to my knowledge the only way to auto-start an app without jailbraking the device.
I know that this question is old, but there are easy work arounds for every question on here that work for at least iOS 7+, although there is no way to do question 2 without using private APIs. You can successfully answer questions 1 and 3 with public API answers (although they are admittedly hacky)!!
1. When user presses home button, how do you delay 5 seconds before returning to the main screen?
Oscar Gomez's answer was spot on. While you cannot delay the UI from returning to the home screen without blocking the core run loop (which will get you rejected from the app store), you can use some background process techniques if needed to get your extra 5 seconds, just not with UI.
2. How do you catch the Power Button presses?
This is for sure using a private API, and while you can PROBABLY get into the App Store, you probably won't last long once Apple gets wind of you doing this. It is also a hack. You cannot listen directly to the power button, but you CAN listen to the screen going on or off, or both at once. Here is a small code snippet that I have in an Enterprise App:
Inside UIAppDelegate
static int const DisplayOnOffObserver = 54321876;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, &PLDisplayOnOff, CFSTR("com.apple.springboard.hasBlankedScreen"), NULL, 0);
...
return YES;
}
static void PLDisplayOnOff(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
...
DO SOME MAGIC
...
}
- (void)applicationWillTerminate:(UIApplication *)application {
...
CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), &DisplayOnOffObserver);
...
}
Obviously you have to have the application ALWAYS running in the background (which can get tricky) or you have to only care to do it within the time that you ask for to run in the background (which isn't hard)
First step is listening to the darwin notification for the screen being toggled on/off.
Second step is implementing the callback.
Third step is stopping listening for the call back.
NOTE: DisplayOnOffObserver is a random number (not very random in my case)
3. How can you directly launch your app when the iPhone starts up?
Assuming that by directly launch, you mean launch in the background, there is actually an Apple supported way of doing this. You first have to enable Background Services for location. Second, you have to start listening for significant location changes. I don't know how well this is documented, but as soon as the iPhone boots, it tries to get it's location. When it does this, it goes from not having a location to having one (which is a significant change). Your app will launch in the background which includes calling the application:didFinishLaunchingWithOptions: method, where you can ask for more time and start other processes to permanently run in the background until the user kills your application manually. If you do permanently run in the background using this method, you do stand a chance of being rejected by Apple.
Hope any of this helps someone! If anyone needs more information, just leave a comment and I will update my answer.
When user press Home button,app don't go back the main interface or delay 5 seconds to go back the
You can't, unless you don't want your app in the appstore... The only thing you CAN do is ask for more time to save your data before your application is terminated, but the UI experience will still be the same - the iphone will go back to the main interface.
when the screen is black ,how to auto activation screen(how to catch power button press event) .
Not possible.
3. How direct launch my app when iPhone is start-up
I don't think you can do this even, with private APIs, and of course with your app not accepted in the appstore.

Unable to restore brightness on application exit

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...)

Is there a way to always use the default.png when returning from background?

In one of my apps when returning from background I get a non consistent behavior:
Sometimes I get the default.png and sometimes I get a snapshot of the last screen which the app was in.
In both cases it takes the UI a good second or two to respond again.
Therefore I would rather show the default.png rather then "unresponsive UI"
Is there a way to make the app display the default.png always until the app becomes active again?
Currently the "stupid" way to do it I thought about is by displaying some Modal view with the default.png and removing it on return to foreground.
Few Clarification:
I am doing this to avoid unresponsive UI.
I am using the default.png as it looks like loading and gives a better experience then unresponsive UI
The app has to run in background.
(And to whoever asked - no it is not closed when I sometimes return and see the default.png and not the last UI state - App loading from the start has a very different path and I'm sure of that)
Thanks in advance.
This is not a correct behavior and you may experiencing a bug. Basically as long as your app is in the background, when you launch it, you should not see the default.png, unless you remove it from background (double click on home button and delete that app).
For future people interested in this you can use the fact the last view in the app is used to be displayed when the app loads back.
You can display a VC as your moving to background which will represent some loading - hence achieving the desired behavior.
I've already seen a few other apps using the same behavior in cases operations are ran when coming back into the app.
Most probably, you are taking too long (performing too many calculations) in methods such as applicationWillEnterForeground:, applicationDidBecomeActive:, etc. As a simple test, try commenting out the code in these methods and see if the problem occurs again.
Simply set in your Info.plist the property "Application doesn't run in background" to YES. The app will never go in background and when the home button is pressed it will be simply terminated. So you're back to the pre-iOS4 behavior.
Note that when you see now the default image at start-up it is simply because your app has been terminated while it was in background. This is normal especially for apps that take a lot of memory and then don't free it enough before going in the background (I think the threshold for the OS is about 18MB but I'm not sure)

How to use Sleep in the application in iphone

I have used to loading a default image in my appication. So i have set to,
Sleep(3); in my delegate.m class.
But sometimes it will take more than 6 to 7 minutes. So i want to display the image 3 seconds only and then it goes to my appilcation based on my requirements.
Which one is best way to do that?
Sleep(3) or [NSThread sleepForTimeInterval:3.0] or something else;
And i must display the image 3 seconds only. Please explain me.
(Note: And I declared setter and getter methods only in my deleagte class.)
Please explain me.
As Rob noted, Apple strongly recommends against a splash screen unless it hides some necessary behind the scenes process (like loading game graphics.) It is so strongly discouraged that some people have claimed that their apps have been rejected for using an unnecessary splash screen.
The default.png doesn't exist to create a splash screen. Instead it exist to allow you to create the illusion that your initial view loads faster than it does. You supposed to use it to provide an image of your initial view so that the enduser can begin to cognitively orient themselves to the interface. By the time they have oriented themselves to the interface and moved their finger to touch the interface, it is live.
Why? Because iPhone apps are supposed be quick in, quick out. People don't sit down to use them at a desk like a desktop. People use then on the go. Sometimes they use them in the middle of a conversation.
I tell my clients to test out the usability of their apps (except for games) while walking, riding an exercise bike etc as well as in the middle of a face-to-face and phone conversation. In those circumstances, a three second pause is a big deal and very noticeable especially if the app is a practical app. Imagine if every time you opened the Contact app you had to pause three seconds to see an Apple splash screen. You would get peeved in a hurry.
The key thing here is that an unnecessary splash screen doesn't add any value for the user. It is a selfish act on the part of the software publisher to eat the end users time so that the publisher can build brand recognition for the sole benefit of the publisher. Wasting three seconds of the users time every time they use the app adds up in a hurry. (In my experience, it also makes the user perceive that the overall app is slow and clunky.)
However, if you do want to shoot yourself in the foot or if you have a client hell bent on a splash screen, you do it like this:
The splash screen appears until the first view loads so you delay the loading of the first view. In the app delegates applicationDidFinishLaunching: method, remove all the code that loads the first view into the window. Replace it with a NSTimer. Put the code to load the first view in the timer's fire method.
With that setup the app will display the default.png as it launches, when it gets to applicationDidFinishLaunching:it will appear to pause from the end users perspective because no view will appear to replace the default.png.
You should note that the standard launch time for an app is 3-5 seconds. So you may not have to do anything to show the splash screen for 3 seconds. It might happen automatically.
Apple strictly recommends against this (using sleep in this way), especially in the scenario of showing a splash screen.
The best thing to do is create a view that looks like your Default.png file, then have that be the first NIB.. you could then set an NSTimer to transition (with animation if you want) to your main view/window/controller.