XaiJu
junesiphone
junesiphone

patreon


Lets make a tweak

The tweak is you press the power button to turn off the flashlight.

Note: I'm not going into how to setup theos. It's usually a pain to setup but here is a place to start. Setting up theos 

I use my flashlight often at night once i'm done I just wanted to press the power button to disable it.  

To start this all off I find a method that handles the power button event. I went to Limneos header site and opened the SpringBoard header I then searched the page for event.  I found volume buttons, home button, all kinds of buttons. I also found 

-(BOOL)_handlePhysicalButtonEvent:(id)arg1 ; 

Looked promising so I created a tweak in theos. Looked like this.

The point was to log out what this UIPressesEvent was and see if it was in fact triggered when I pressed a button.

It did and logged something like this.

Test button pressed <UIPressesEvent : 0x2834e80c0 physical buttons {(
<UIPress: 0x283b35030 phase=0 type=104 force=1.000000> )}>

Test button pressed <UIPressesEvent : 0x2834e80c0 physical buttons {(  <UIPress: 0x283b35030 phase=0 type=104 force=0.000000> )}>

 This is what was shown just by hitting the power button once. It was odd it sent two logs, but when I looked closely force was changed. I'm guessing the 1 is pressed and 0 is released.

The class was a UIPressesEvent which we knew from the springboard header, but this verifies that. I then look up the UIPressesEvent to see what it was.

While looking through the header I see -(NSSet *)allPresses; I want to see what that is so I log it.

Which gave me  

TestConsole presses {(<UIPress: 0x282c0bf70 phase=3 type=104 force=0.000000>)}

Not surprising. NSSet is much like an array and its contains objects. By getting the object we can get the type and force. The phase doesn't change here so we don't need it.

I make sure there is an event passed, just to be sure. Also check it gives presses. More than likely you could get away without this. I also get the type and force. Here i'm checking if type is 104 and force is 0. I know it's 104 as the log above tells me and I want it to happen on release so force is 0.

Now that we have the trigger we want to turn off the flash light. I know from other tweaks how to disable the flashlight but I will go through them here.

I would start by going to Limneos header site and searching for flashlight. I would find the SBUIFlashLightController  you will see that it has a +(id)sharedInstance; which will give me direct access to an instance of SBUIFlashLightController. With the instance I could then call it's methods.

In this case
-(void)turnFlashlightOffForReason:(id)arg1 ; sounds like something we could use. I log the arg1 to see what it is and it's a NSString.

We don't need this code, it was only to see what this returned. If you go to the control center and press the flashlight button, then turn it off this is called and the arg1 says Control Center.

With this knowledge we can now turn the flashlight off, but we also want to see if it's on. No need to turn it off if it isn't on. The SBUIFlashlightController also has a level. I check the level then turn off the flashlight.

Here is the entire code up until now. The @interface is simply there for the compiler. Without it, the compile would throw an error saying something to the extent of SBUIFlashlightController doesn't define sharedInstance.

The code is done and I could easily run this on my device. It is not ready for the public though. We use SBUIFlashlightController turnFlashlightOffForReason, which is only available on iOS12 and 13. If a user installed this on anything under iOS12 it would send the phone to safemode when the user hit the power button.

To fix this we need to make sure turnFlashlightOffForReason is available as well as level.

By using respondsToSelector we see if this class responds to these methods. If they do not it will stop the code from running in the if statement which in turn will stop a safemode. This doesn't make the code work on other firmwares, just stops a safemode if installed on a version that isn't supported.

If you were to make this support more firmwares you would simply find an alternate way of turning the flashlight on or off by looking at headers for the firmware you are trying to support.

That's pretty much it. We now have a tweak that will turn the flashlight off by hitting the power button. Here is a Video 



More Creators