Jump to content
Vanar

Screen Brightness in iOS

Recommended Posts

Tell me, how can I programmatically control the screen brightness under iOS?

Share this post


Link to post

To increase the chances for a positive response, I would recommend to first check if its possible at all with XCode and Objective-C. If it's possible there then its usually also possible in Delphi, and then you can begin figuring out how its done and eventually ask about it here.

  • Like 1

Share this post


Link to post
3 hours ago, Hans♫ said:

To increase the chances for a positive response, I would recommend to first check if its possible at all with XCode and Objective-C. If it's possible there then its usually also possible in Delphi, and then you can begin figuring out how its done and eventually ask about it here.

I know for sure that it is possible.
In most applications from stores, when calling (showing) QR, the brightness of the iPhone screen increases ...

Share this post


Link to post

This was written by Copilot. I can not test the code as I do not have the iOS units installed on my system.

 

To change the screen brightness on iOS using Delphi, you can use the UIScreen class from the iOS API. Here's a basic example of how you can achieve this:

1. 
Import the necessary iOS units:

uses
iOSapi.UIKit, iOSapi.Foundation;

1. 
Set the screen brightness:

procedure SetScreenBrightness(Brightness: CGFloat);
begin
UIScreen.mainScreen.setBrightness(Brightness);
end;

1. 
Call the procedure with the desired brightness level:

begin
SetScreenBrightness(0.5); // Set brightness to 50%
end;

In this example, UIScreen.mainScreen.setBrightness is used to set the brightness level. The brightness value should be between 0.0 (minimum brightness) and 1.0 (maximum brightness)https://www.reddit.com/r/iOSProgramming/comments/whtu5q/is_there_a_way_to_control_screen_brightness/https://forums.developer.apple.com/forums/thread/717234.

Would you like more details on integrating this into a full application?

  • Thanks 1

Share this post


Link to post
6 hours ago, Vanar said:

Tell me, how can I programmatically control the screen brightness under iOS?

uses
  iOSapi.Helpers;

function GetBrightness: Single;
begin
  Result := TiOSHelper.MainScreen.brightness;
end;

procedure SetBrightness(const AValue: Single);
begin
  if (AValue >= 0) and (AValue <= 1) then
    TiOSHelper.MainScreen.setBrightness(AValue);
end;



 

  • Thanks 1

Share this post


Link to post

Thanks everyone!
I applied Dave's solution.
Thread closed.

  • Like 1

Share this post


Link to post
On 12/5/2024 at 6:51 PM, Vanar said:

I know for sure that it is possible.
In most applications from stores, when calling (showing) QR, the brightness of the iPhone screen increases ...

Ok, thats great. So let me elaborate:

When you have found out what native functions are used in Objective-C to get the wanted result, then you can search the source code of Delphi for those functions. This will tell you exactly what function to call in Delphi to access the feature. Its of course nice to be able to get help here in DelphiPraxis, but its even better if you know how to find the solution on your own - and its much faster 🙂

Share this post


Link to post

Jut for the fun, 
I programmed a little app for my daughter when she was young, to let her know whether she could wake up (day) or not (night), keep silent or not in the morning... 

Meaning she doesn't wake us up at 5am on Sundays, but has to wait til 7...

 

To adjust contrast I just put a TRectangle filling the form and added a ContrastEffect to it. Then adjusting ContrastEffect.Brightness values depending on time

It was managing contrast just for the app on the desk tablet, not the entire iPad or Android tablet.

Edited by sjordi

Share this post


Link to post
5 hours ago, sjordi said:

To adjust contrast I just put a TRectangle filling the form and added a ContrastEffect to it.

Nice idea.
I understand this is for your personal phone, that will be fine,
but if this is for the AppStore and the apps whole purpose is like this, then I'm afraid the reviewer will have a few questions ... :classic_biggrin:

Edited by Rollo62

Share this post


Link to post

Of course the app had something else: images with a kid saying to shush with his finger on his mouth, the moon, the sun to explain a toddler what time of day it is.

Actually, the brightness was just controlling the background to dim the screen at night.

Could make it a bit more configurable and actually have it in the AppStore for others.

Was intended as a sleep trainer like the image here: eyes closed you keep sleeping, eyes open, you can disturb us.
 

sam-the-sleep-sleep-trainer-1.jpeg

Edited by sjordi
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×