user1658674
user1658674

Reputation:

No jailbreak detection

I'm trying to make an app which only works on jailbroken iDevices. I already have jailbreak detection code:

([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]);{
    UIAlertView *cydiaisinstalled=[[UIAlertView alloc]initWithTitle:@"Cydia is installed!"
                                                            message:@"You can use Respring!"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
    [cydiaisinstalled show];
}}

But now I need to be able to detect if the device is not jailbroken.

Upvotes: 8

Views: 4535

Answers (4)

GeoSn0w
GeoSn0w

Reputation: 714

For those coming here in 2021, here's a very quick way to detect if you're jailbroken and unlike other answers, this would have the benefit on working on all the dozens of different jailbreaks that exist:

FILE * filepath = fopen("/var/mobile/test-jb", "w");
    
    if (!filepath) {
        fclose(filepath);
        fprintf(stderr,"Random processes are running sandboxed. Not jailbroken\n");
        return -2;
    }
    
    printf("Detected sandbox escape. This device is likely jailbroken.\n");
    fclose(filepath);

What this does, is to attempt to write a test file to /var/mobile. It normally works if you're jailbroken and have proper entitlements. On a non-jailbroken device, this would fail with deny() in the console.

Make sure you sign your app with these entitlements:

<key>com.apple.private.security.no-container</key>
    <true/>
<key>platform-application</key>
    <true/>

Upvotes: 0

Tomas Andrle
Tomas Andrle

Reputation: 13354

Try accessing any file outside the app's sandbox. For example:

BOOL IsDeviceJailbroken(void) {
    #if TARGET_IPHONE_SIMULATOR
    return NO;
    #else
    return [[NSFileManager defaultManager] fileExistsAtPath: @"/bin/bash"];
    #endif
}

Note that having Cydia installed and having a jailbroken device are two different things.

Upvotes: 8

user1658674
user1658674

Reputation:

OK thanks for all the answers but I found it out on my own. Here is the code:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
    //insert action if cydia is installed
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]] == NO) {
    //insert action if Cydia is not installed
}

With this code you can detect any application on your idevice as long as the app has an URL scheme you can find most of the URL scheme here: http://handleopenurl.com

PS: You have to replace the green part whith your actions:)

Upvotes: 3

pasawaya
pasawaya

Reputation: 11595

I wrote a function that detects whether the device is jailbroken for another question, but it seems relevant here:

- (BOOL) isJailbroken() {

    //If the app is running on the simulator
    #if TARGET_IPHONE_SIMULATOR
        return NO;

    //If its running on an actual device
    #else
        BOOL isJailbroken = NO;

        //This line checks for the existence of Cydia
        BOOL cydiaInstalled = [[NSFileManager defaultManager] fileExistsAtPath:@"/Applications/Cydia.app"];

        FILE *f = fopen("/bin/bash", "r");

        if (!(errno == ENOENT) || cydiaInstalled) {

            //Device is jailbroken
            isJailbroken = YES;
        }            
        fclose(f);
        return isJailbroken;
    #endif
}

This function uses two checks to see if the phone is jailbroken: it first checks if Cydia is installed. Not all jailbroken devices have Cydia installed, though most do, so I also check for the existence of bash, which also only appears on jailbroken devices. Note that this function will work in nearly all cases, but it's probably not 100%. The only people that don't have Cydia on their jailbroken iDevice are probably those that are experimenting with jailbroken devices and not using them for advantages like tweaks and themes.

Upvotes: 6

Related Questions