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!

Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
nitsky
  • 221
  • 1
  • 2
  • 5

6 Answers6

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;
iMathieuB
  • 1,168
  • 10
  • 11
  • 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. – Victor Ronin Oct 16 '12 at 16:31
  • 1
    I tried to use a private method _setProximity:(BOOL) on UIDevice, but it didn't turn off screen. – Victor Ronin Oct 16 '12 at 17:17
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.

CiNN
  • 9,752
  • 6
  • 44
  • 57
Elias Limneos
  • 930
  • 9
  • 13
  • 2
    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. – Victor Ronin Oct 17 '12 at 22:48
  • 1
    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. – Victor Ronin Oct 17 '12 at 22:52
  • 1
    You're right about iOS6, just tested it. I added the code you need for iOS6 to make it work. – Elias Limneos Oct 18 '12 at 00:28
  • 1
    Then again, it stills seems to not work in processes other than SpringBoard...I'll look further and post back. – Elias Limneos Oct 18 '12 at 00:33
  • 1
    It appears you need the entitlement com.apple.backboard.client. – Elias Limneos Oct 18 '12 at 00:38
  • Yeah. I also found ScreenBlanked yesterday and found that it requires entitlement. – Victor Ronin Oct 18 '12 at 16:19
  • 1
    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! – ari gold Feb 08 '13 at 19:23
  • 1
    @arigold, see my [answer to this question](http://stackoverflow.com/a/11093442/119114) 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. – Nate Feb 13 '13 at 22:19
  • @Nate please help me set these entitlements.I can't understand your link and how do i download ldid? – zzzzz Feb 14 '13 at 07:08
  • @Nate I can't see my applications entitlements file.I am using xcode 4.6.and where do i place the custom entitlements file? – zzzzz Feb 14 '13 at 09:28
  • how to use this on ios7. – Jitendra Oct 19 '13 at 12:47
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

DShah
  • 9,768
  • 11
  • 71
  • 127
Adam
  • 1,486
  • 3
  • 20
  • 35
0

Have you tried:

[[UIScreen mainScreen] setBrightness: yourvalue];

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

Community
  • 1
  • 1
cleverbit
  • 5,514
  • 5
  • 28
  • 38
  • 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) – Victor Ronin Oct 15 '12 at 22:30
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.

James H
  • 1,824
  • 22
  • 23
-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.

Community
  • 1
  • 1
Chaitanya
  • 2,396
  • 4
  • 29
  • 45
  • GSEventSetBacklightFactor sounded like the right function, but unfortunately it's been removed from the SDK!! Any suggestions for a replacement? – nitsky Oct 18 '10 at 00:21
  • `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. – Tommy Oct 11 '12 at 20:22
  • GSEventSetBacklightFactor was completely removed (it's not even a private API anymore). – Victor Ronin Oct 16 '12 at 19:59