-
[iOS] iPhone(아이폰) 기기 식별하기 (모델, 모델명 구하기)/iOS 📱 2023. 8. 13. 23:29반응형
안녕하세요 아렉스입니다 ! :D
오늘은 아이폰 기기를 식별하는 방법을 정리해보겠습니다
iOS Hardware Identification, iOS Hardware type 등 검색해보면 결과들이 많이 나옵니다.
https://www.theiphonewiki.com/wiki/Models
Models - The iPhone Wiki
iOS runs on various different models of devices. This page is used to give an overview of the different model numbers (or "M" numbers) used by devices. The model number of your device is located in the Settings app on the "General -> About" screen under
www.theiphonewiki.com
사이트를 접속해보면 아이폰 세대 별로 identifier를 확인해볼 수 있습니다.
오늘은 이 identifier를 구하는 방법을 소개해드릴게요 !
extension UIDevice { static var deviceModel: String { #if targetEnvironment(simulator) let DEVICE_IS_SIMULATOR = true #else let DEVICE_IS_SIMULATOR = false #endif var machineString : String = "" if DEVICE_IS_SIMULATOR == true { if let dir = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { machineString = dir } } else { var systemInfo = utsname() uname(&systemInfo) let machineMirror = Mirror(reflecting: systemInfo.machine) machineString = machineMirror.children.reduce("") { identifier, element in guard let value = element.value as? Int8, value != 0 else { return identifier } return identifier + String(UnicodeScalar(UInt8(value))) } } return machineString }
답을 먼저 공개 했구 과정에 대해서도 서술해볼까합니다 !
[macOS] uname 명령어 사용법
uname 명령어는 시스템 정보를 출력해줍니다. 주로 시스템과 리눅스 커널에 관련된 정보를 확인할 수 있습니다. uname은 리눅스뿐만 아니라 맥OS 등에서도 사용할 수 있습니다. 현재 시스템의 시스
plcprogrammer-dy.tistory.com
[macOS] Darwin은 무엇일까 ?
"Darwin"은 macOS, iOS, watchOS, 그리고 tvOS의 기초를 이루는 오픈 소스 운영 체제 Core입니다. 이것은 Apple에 의해 개발되었고(NeXT를 1997년에 인수) BSD Unix 운영 체제를 기반으로 합니다. [macOS] macOS의 뿌리
plcprogrammer-dy.tistory.com
이전 포스팅 두개를 참고하면 uname을 통해 우리는 프로세스 정보 등 많은 정보를 조회해볼 수 있었습니다.
그리고 uname 리눅스를 명령어를 사용하기 위해 Darwin이 존재하였고
Darwin 위에 코코아 프레임워크가 올라간다고 포스팅했었습니다.
이런 빌드업을 왜 했을까요 ..! ㅎㅎ
UIKit 프레임워크 내부에는 Foundation 프레임워크가 import 되어있는 것 다들 아시죠 ??
Foundation 프레임워크 내부에는 Darwin이 import 되어있습니다 !
그렇기에 우린 iOS 개발에서도 uname을 사용할 수 있습니다.
uname 함수의 정의는 이렇습니다.
UnsafeMutablePointer, 포인터는 C언어에서 나오는 개념이라 들어보신 분들도, 들어본 적이 없으신 분도 계실 것 같습니다.
정말 정말 간단하게 축약해보자면 변수의 메모리에 접근할 수 있습니다.
inout 방식으로 &(앰퍼선드)를 통해서 간단하게 넘길 수 있습니다.
var systemInfo = utsname() // & 통해 넘기기 위해서는 var로 선언해야함. uname(&systemInfo)
좀 길긴.. 하지만 ! struct utsname 의 initialization 입니다 !
sysname, nodename, release, version, machine을 인자로 받습니다.
제가 한번 세보았는데 256개 즉, 0~255 까지 !! 유추했지만 엮을만한게 생각이 안나네요 .. !
공부가 보강되면 채워넣겠습니다 !
CChar 타입은 이렇습니다 C언어의 char 타입을 나타내고있습니다.
오늘 테스트에 쓰일 제 아이폰은 iPhone 14 Pro 입니다 !
로직도 알았겠다 ! Playground에서 한번 코드를 작성해볼까요 ?
let machineNumbers: [Int] = [105, 80, 104, 111, 110, 101, 49, 53, 44, 50] var machineString: String = "" // UInt8 - An 8-bit unsigned integer value // typealias UnicodeScalar = Unicode.Scalar machineNumbers.forEach { let string = String(UnicodeScalar(UInt8($0))) machineString += string print("machineString: \(machineString)") } print("Result") print("machineString: \(machineString)")
결과를 볼까요 ?
iPhone15,2 -> iPhone 14 Pro 가 나왔네요 !
' > iOS 📱' 카테고리의 다른 글
[iOS] class가 NSObject를 상속받아야할 때 (NSObjectProtocol) (1) 2023.11.04 [iOS] 하이브리드 앱 개발시 Bridge 구현 (Javascript와 통신하기 - WKScriptMessageHandler) (0) 2023.11.03 [iOS] 라이트모드/다크모드 설정하기 (Appearance) (0) 2023.08.07 [iOS] Lottie 애니메이션 사용하기 (0) 2023.08.07 [iOS] 스토리보드 없이 개발하기 (0) 2023.08.07