본문 바로가기

개발/Swift

iOS Swift4 탈옥 여부 체크하기

반응형

iOS Swift4 탈옥 여부 체크하기

 

Swift 카테고리의 게시글은 처음쓴다

 

안드로이드의 루트 권한을 체크하는 포스팅을 아래와 같이 하고

내친김에 iOS를 개발할때 Swift4에서 사용하고있는 방식을 포스팅 하려한다 

 

Android 루팅 여부, 루트 권한 체크하기

Android 루팅 여부 체크하기 안드로이드 개발을 하다가 루팅된 기기에서는 앱이 동작하지 않도록 해야하는 요구사항이 생겼다 1. Command를 실행할 수 있는 Runtime.getRuntime().exec( command ); 을 이용하여 루..

6developer.com

 

1. 탈옥 시 사용하는 앱이 설치되어있는지 확인하고 접근 불가한 경로를 열어보는 시도로 탈옥 여부 즉 루트 권한이 있는지 체크한다

func hasJailbreak() -> Bool {
        
        guard let cydiaUrlScheme = NSURL(string: "cydia://package/com.example.package") else { return false }
        if UIApplication.shared.canOpenURL(cydiaUrlScheme as URL) {
            return true
        }
        #if arch(i386) || arch(x86_64)
        return false
        #endif
        
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: "/Applications/Cydia.app") ||
            fileManager.fileExists(atPath: "/Library/MobileSubstrate/MobileSubstrate.dylib") ||
            fileManager.fileExists(atPath: "/bin/bash") ||
            fileManager.fileExists(atPath: "/usr/sbin/sshd") ||
            fileManager.fileExists(atPath: "/etc/apt") ||
            fileManager.fileExists(atPath: "/usr/bin/ssh") ||
            fileManager.fileExists(atPath: "/private/var/lib/apt") {
            return true
        }
        if canOpen(path: "/Applications/Cydia.app") ||
            canOpen(path: "/Library/MobileSubstrate/MobileSubstrate.dylib") ||
            canOpen(path: "/bin/bash") ||
            canOpen(path: "/usr/sbin/sshd") ||
            canOpen(path: "/etc/apt") ||
            canOpen(path: "/usr/bin/ssh") {
            return true
        }
        let path = "/private/" + NSUUID().uuidString
        do {
            try "anyString".write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
            try fileManager.removeItem(atPath: path)
            return true
        } catch {
            return false
        }
    }
    func canOpen(path: String) -> Bool {
        let file = fopen(path, "r")
        guard file != nil else { return false }
        fclose(file)
        return true
    }

 - hasJailbreak() 를 사용하여 루트 권한 여부 탈옥 여부를 판별

 

 

2. 탈옥 여부를 체크하고 루트권한이 있는 기기라면 다이얼로그를 띄우고 확인을 누르면 앱을 종료하는 예제

	if(hasJailbreak()){

            let dialog = UIAlertController(title: nil, message: "루트권한을 가진 디바이스에서는 실행할 수 없습니다.", preferredStyle: .alert)
            let action = UIAlertAction(title: "확인", style: UIAlertActionStyle.default){
                (action:UIAlertAction!) in
                    exit(0)
            }
            dialog.addAction(action)
            self.present(dialog, animated: true, completion: nil)


        }

 - 적당한 위치에 해당 소스를 넣어 테스트해보고 입맛에 맞게 커스텀하면 되겠다

 

 

반응형

'개발 > Swift' 카테고리의 다른 글

[Swift/iOS] Key, Value 형태로 값 저장 (UserDefaults)  (0) 2021.11.02