请使用以下代码适用于 Swift 4 及以上版本:

将以下代码添加到 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

}

}

在Appdelegate方法中,编写以下代码:

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()

}

}

}

}