Turn off display in iPhone OS (iOS)
Asked Answered
B

6

20

is there a way to programmatically turn off the display in iOS? Not just turning brightness down, but off like the way the Phone App does. I am happy to use private API, since this is for personal use.

Thanks!

Babs answered 16/10, 2010 at 18:21 Comment(1)
can you say why the power button does not work for you?Paedo
S
26

You can turn off the display by enabling the proximity monitoring. It will automatically turn off the screen, like in the Phone app, by placing the phone near your ears or by placing a finger over the IR sensor at the top of the phone.

[UIDevice currentDevice].proximityMonitoringEnabled = YES;
Skied answered 15/10, 2012 at 18:53 Comment(2)
Can I set a proximity some way? I mean I can't programmatically force a user to a phone near ears :) And I need completely programmatically turn it black.Impel
I tried to use a private method _setProximity:(BOOL) on UIDevice, but it didn't turn off screen.Impel
C
15

You can do this, (obviously, using Private APIs of course) :

on iOS5:

#include <stdio.h>
#include <dlfcn.h>

int (*SBSSpringBoardServerPort)() = (int (*)())dlsym(RTLD_DEFAULT, "SBSSpringBoardServerPort");
int port = SBSSpringBoardServerPort(); 
void (*SBDimScreen)(int _port,BOOL shouldDim) = (void (*)(int _port,BOOL shouldDim))dlsym(RTLD_DEFAULT, "SBDimScreen");

and then use

SBDimScreen(port,YES); 

whenever you want to dim, and

SBDimScreen(port,NO);

whenever you want to undim.

On iOS6:

void (*BKSDisplayServicesSetScreenBlanked)(BOOL blanked) = (void (*)(BOOL blanked))dlsym(RTLD_DEFAULT, "BKSDisplayServicesSetScreenBlanked");

and then use:

BKSDisplayServicesSetScreenBlanked(1); // 1 to dim, 0 to undim

"Dim" here means totally turn off the screen. This is what the system uses when e.g. a proximity event occurs while in a call.

Callery answered 17/10, 2012 at 22:28 Comment(11)
Did you try it on iOS 6? It silently fails (doesn't do anything). I am under impression that internally it checks for some entitlement.Impel
BTW. I saw that you posted some interesting stuff on Proximity Sensor and at iPhoneDevWiki. Do you mind sending me email (my email is in my profile). I would like you to ask one thing about it.Impel
You're right about iOS6, just tested it. I added the code you need for iOS6 to make it work.Callery
Then again, it stills seems to not work in processes other than SpringBoard...I'll look further and post back.Callery
It appears you need the entitlement com.apple.backboard.client.Callery
Yeah. I also found ScreenBlanked yesterday and found that it requires entitlement.Impel
I tried the above code (for both iOS 5 & 6) and it didn't work. How do I go about setting the entitlement com.apple.backboard.client? Thanks!Diffract
@arigold, see my answer to this question for an example of setting the entitlements. Obviously, that uses a different entitlement, so change it to com.apple.backboard.client in your entitlements.xml file.Haunting
@Haunting please help me set these entitlements.I can't understand your link and how do i download ldid?Cure
@Haunting I can't see my applications entitlements file.I am using xcode 4.6.and where do i place the custom entitlements file?Cure
how to use this on ios7.Hyposensitize
F
4

The only way I know of, public or private, is using the power button.

You might look at -[UIApplication setProximitySensingEnabled:(BOOL)], or -[UIApplication setIdleTimerDisabled:YES], this might lead to something useful

Foreignborn answered 16/10, 2010 at 19:55 Comment(0)
V
0

Have you tried:

[[UIScreen mainScreen] setBrightness: yourvalue];

SO question 8936999: iPhone: How can we programmatically change the brightness of the screen?

Vikki answered 15/10, 2012 at 21:24 Comment(1)
Thanks. I will try it tomorrow. However, my guess it will able to control brightness only in the same range as Preferences application (which doesn't turn display off completely, but just may be way darker)Impel
A
0

Proximity doesn't work on all devices. There's a much simpler solution to this problem without resorting to private APIs.

Swift

UIScreen.main.wantsSoftwareDimming = true
UIScreen.main.brightness = 0.0

Without wantsSoftwareDimming, the backlight will never completely turn off. The docs have this cautionary sentence:

The default value is false. Enabling it may cause a loss in performance.

Avaria answered 27/11, 2017 at 3:13 Comment(0)
G
-1

I do not think there is any to turn off the display (simulating iphone sleep button) except changing the brightness.

This link might help.

Gatehouse answered 16/10, 2010 at 20:4 Comment(3)
GSEventSetBacklightFactor sounded like the right function, but unfortunately it's been removed from the SDK!! Any suggestions for a replacement?Babs
GSEventSetBacklightFactor has never been a part of the official SDK for the simple reason that Apple don't want you to be able to adjust the screen brightness or to switch it off any more than they want you to be able to make phone calls or app store purchases on the user's behalf.Redintegrate
GSEventSetBacklightFactor was completely removed (it's not even a private API anymore).Impel

© 2022 - 2024 — McMap. All rights reserved.