How do I detect that an iOS app is running on a jailbroken phone?

后端 未结 17 1308
情书的邮戳
情书的邮戳 2020-11-22 10:48

If I want my app to behave differently on a jailbroken iPhone, how would I go about determining this?

17条回答
  •  囚心锁ツ
    2020-11-22 11:41

    Please use following code for Swift 4 and above: Add the following code in the appdelegate:

    private func getJailbrokenStatus() -> Bool {
        if TARGET_IPHONE_SIMULATOR != 1 {
            // Check 1 : existence of files that are common for jailbroken devices
            if FileManager.default.fileExists(atPath: "/Applications/Cydia.app")
                || FileManager.default.fileExists(atPath: "/Library/MobileSubstrate/MobileSubstrate.dylib")
                || FileManager.default.fileExists(atPath: "/bin/bash")
                || FileManager.default.fileExists(atPath: "/usr/sbin/sshd")
                || FileManager.default.fileExists(atPath: "/etc/apt")
                || FileManager.default.fileExists(atPath: "/private/var/lib/apt/")
                || UIApplication.shared.canOpenURL(URL(string:"cydia://package/com.example.package")!) {
                return true
            }
            // Check 2 : Reading and writing in system directories (sandbox violation)
            let stringToWrite = "Jailbreak Test"
            do {
                try stringToWrite.write(toFile:"/private/JailbreakTest.txt", atomically:true, encoding:String.Encoding.utf8)
                //Device is jailbroken
                return true
            } catch {
                return false
            }
        }
        else {
            return false
        }
    }
    

    func applicationDidBecomeActive (_ application: UIApplication) {

        if getJailbrokenStatus() {
            let alert = UIAlertController(title: LocalizedKeys.Errors.jailbreakError, message: LocalizedKeys.Errors.jailbreakErrorMessage, preferredStyle: UIAlertController.Style.alert)
            let jailBrokenView = UIViewController()
    
            jailBrokenView.view.frame = UIScreen.main.bounds
            jailBrokenView.view.backgroundColor = .white
            self.window?.rootViewController = jailBrokenView
            jailBrokenView.present(alert, animated: true, completion: nil)
        }
    
        if #available(iOS 11.0, *) {
            if !UIScreen.main.isCaptured {
                DispatchQueue.main.async {
                    self.blockImageView.removeFromSuperview()
                }
            }
        }
    }
    

提交回复
热议问题