Home iOS Tweak Development : 2
Post
Cancel

iOS Tweak Development : 2

Note

This series of posts is not made as strictly a tutorial While I will be explaining how things work and giving instruction on iOS tweak development, the purpose of this posting is to explain and document my journey/path of learning to develop iOS tweaks. If you desire for strictly a tutorial in iOS Tweak Development check out these links here : Zane Helton or r/Jailbreak Tutorial

Studying Open Source Tweaks

In order to begin learning the methods of developing tweaks I began by looking at open source tweaks. This would allow me to understand how properly developed tweaks should look before I begin developing my own.

A list of tweaks that are open source can be found on iPhoneDevWiki. The tweak I decided to focus on for this post is counter. While that tweak is not listed on iPhoneDevWiki’s list I found ConorTheDev’s Github from another tweak listed on that page.

On the Github for counter you will be greeted with a few files, the notable ones to tweak development are: CounterManager.h, CounterManager.m, Makefile, Tweak.x, control, and counter.plist. I will go through these one by one and explain the importance of each.

Note: You can also find a folder called Module. This folder contains the code that modifies the control center to display the unlock count. I will potentially cover those files in a later post as the files in the main directory suit the needs I have with this specific post.

CounterManager.h

This file is an Objective-C Interface file. It contains the declaration of two classes, NSDistributedNotificationCenter and CounterManager. A more in depth explanation of this filetype can be found on Tutorials Point. The CounterManager class contains the properties and methods used by the tweak to hold and update the value of unlocks.

CounterManager.m

This file is an Objective-C Implementation file. It contains the definition of the previously defined class CounterManager. An more in depth explanation of this filetype can be also be found on Tutorials Point. Within this file you can review the different methods of CounterManager, these methods allow the count to be incremented, reset, and synchronized. The code for these is quite simple and easy to understand. This count value will then be accessed and used later on after hooking into the control center process.

Makefile

This file is automatically generated by the nic.pl component of the Theos tool discussed in the previous post. Makefiles are very commonly used files that assist in the building/compiling and cleaning of programs. You can find more information on the usefulness and functionality of these files here

Tweak.x

This file is a Logos file. This is where the magic happens, or just where the existing code gets hooked and modified. I’m going to break this file down more specifically and focus on the specifics of this code.

1
2
3
4
#import "CounterManager.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
BOOL previouslyLocked = YES;

In this block of code you can find the importing of the interface file discussed earlier, as well as a #define and declaration of a variable to be used later in the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%hook Controller
- (void)setAuthenticated:(BOOL)authenticated {
    %orig;

    if(authenticated && previouslyLocked) {
        [[CounterManager sharedInstance] incrementUnlockCount];
    }

    previouslyLocked = !authenticated;
}

- (void)setInScreenOffMode:(BOOL)screenOff {
    %orig;
    previouslyLocked = screenOff ? YES : previouslyLocked;
}
%end

This block of code is where you find most of the information that we came to learn from this tweak. At the top of the block you can find where it hooks into the Controller Header with %hook Controller. From there it accesses the method setAuthenticated with - (void)setAuthenticated:(BOOL)authenticated {. If you are unfamiliar with Object Oriented Programming, a method is simply a function that specifically belongs to a class. So here we can see that the setAuthenticated method returns a (void) value, and accepts a boolean authenticated value; (BOOL)authenticated.

Within this method the Logos code calls the original code of the method with %orig;, and then adds a statement to the method after that. This statement checks if the device was authenticated, or unlocked, and ensures that it was previously locked. If both of those were true the Unlock count would be incremented by calling the incrementUnlockCount method within the CounterManager class. It then sets the previouslyLocked value to the opposite of the authenticated value. I believe this is to ensure the proper value of previouslyLocked in any chance of faulty scheduling.

After this the logos code accesses the setInScreenOffode method where it calls the original method code and then sets the value previouslyLocked to true if the screen is currently off.

The file then ends the hook with %end

1
2
3
%ctor {    
    %init(Controller = NSClassFromString(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13") ? @"CSCoverSheetViewController" : @"SBDashBoardViewController"));
}

%ctor generates an anonymous constructor. A constructor is a subroutine that is used to create an object and will often take arguments to properly set the member variables of said objects. Here you can see the constructor generate a view controller object.

control

This file is also generated by nic.pl however it is modified with the text according to the developers specificities. This is used to provide package managers with the information about the package.

counter.plist

This file contains which applications need to be restarted upon installing this tweak. This is used so the tweak can be loaded automatically upon installing the tweak. In this case you can see it requires to restart com.apple.springboard, this is commonly referred to as respringing in iOS jailbreaking.

What next?

Now that I have studied a few open source tweaks similar to how I took apart this tweak. It’s time for me to jump in and try and develop my own tweak. In the next post I will be explaining the entire process of me attempting to develop a relatively simple tweak. Thanks for reading! View the next post here.

This post is licensed under CC BY 4.0 by the author.