programing

Swift에서 고유한 장치 ID를 얻는 방법은 무엇입니까?

jooyons 2023. 4. 25. 22:17
반응형

Swift에서 고유한 장치 ID를 얻는 방법은 무엇입니까?

Swift에서 장치의 고유 ID를 얻으려면 어떻게 해야 하나요?

데이터베이스에서 사용할 ID와 소셜 앱에서 웹 서비스의 API 키로 사용할 ID가 필요합니다.이 장치의 일상적인 사용을 추적하고 쿼리를 데이터베이스로 제한하기 위한 것입니다.

다음 항목(Swift 3)을 사용할 수 있습니다.

UIDevice.current.identifierForVendor!.uuidString

이전 버전의 경우 다음을 수행합니다.

UIDevice.currentDevice().identifierForVendor

또는 문자열을 원하는 경우 다음을 수행합니다.

UIDevice.currentDevice().identifierForVendor!.UUIDString


사용자가 앱을 제거한 후에는 더 이상 장치를 고유하게 식별할 수 없습니다.설명서에는 다음과 같이 나와 있습니다.

앱(또는 동일한 공급업체의 다른 앱)이 iOS 기기에 설치되어 있는 동안 이 속성의 값은 동일하게 유지됩니다.사용자가 장치에서 해당 공급업체의 앱을 모두 삭제한 후 하나 이상의 앱을 다시 설치하면 값이 변경됩니다.


자세한 내용은 Matt Thompson의 다음 기사를 읽어보시기 바랍니다.
http://nshipster.com/uuid-udid-unique-identifier/

Swift 4.1용 업데이트. 다음을 사용해야 합니다.

UIDevice.current.identifierForVendor?.uuidString

Swift 4에서 장치 검사를 사용할 수 있습니다. Apple 설명서

func sendEphemeralToken() {
        //check if DCDevice is available (iOS 11)

        //get the **ephemeral** token
        DCDevice.current.generateToken {
        (data, error) in
        guard let data = data else {
            return
        }

        //send **ephemeral** token to server to 
        let token = data.base64EncodedString()
        //Alamofire.request("https://myServer/deviceToken" ...
    }
}

일반적인 사용법은 다음과 같습니다.

일반적으로 DeviceCheck API를 사용하여 새 사용자가 동일한 기기에서 다른 사용자 이름으로 제안을 이미 상환하지 않았는지 확인합니다.

서버 작업 필요:

WWDC 2017 — 세션 702(24:06)를 참조하십시오.

자세한 내용은 Santosh Botre 기사 - iOS 장치의 고유 식별자를 참조하십시오.

연결된 서버는 이 토큰을 Apple에서 받은 인증 키와 결합하고 결과를 사용하여 장치별 비트에 대한 액세스를 요청합니다.

Swift 3.X 최신 작업 코드의 경우 쉽게 사용할 수 있습니다.

   let deviceID = UIDevice.current.identifierForVendor!.uuidString
   print(deviceID)

UIDevice 클래스에 있는 identifierForVendor 공용 속성을 사용할 수 있습니다.

let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
        print("UUID: \(UUIDValue)")

Swift 3을 편집합니다.

UIDevice.current.identifierForVendor!.uuidString

편집을 종료합니다.

속성 계층에 대한 스크린샷입니다.

Swift 2.2입니다.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let userDefaults = NSUserDefaults.standardUserDefaults()

    if userDefaults.objectForKey("ApplicationIdentifier") == nil {
        let UUID = NSUUID().UUIDString
        userDefaults.setObject(UUID, forKey: "ApplicationIdentifier")
        userDefaults.synchronize()
    }
    return true
}

//Retrieve
print(NSUserDefaults.standardUserDefaults().valueForKey("ApplicationIdentifier")!)

swift 4,5 아래 코드를 사용하여 UUID를 얻을 수 있습니다.

print(UIDevice.current.identifierForVendor!.uuidString)

산출량

여기에 이미지 설명을 입력하십시오.

이와는 별도로 연결된 장치에서 여러 속성을 가져올 수 있습니다.

 UIDevice.current.name             // e.g. "My iPhone"
 UIDevice.current.model            // e.g. @"iPhone", @"iPod touch"
 UIDevice.current.localizedModel   // localized version of model
 UIDevice.current.systemName       // e.g. @"iOS"
 UIDevice.current.systemVersion    // e.g. @"15.5"
if (UIDevice.current.identifierForVendor?.uuidString) != nil
        {
            self.lblDeviceIdValue.text = UIDevice.current.identifierForVendor?.uuidString
        }

iOS 11 이상부터는 Apple의 api DeviceCheck를 사용할 수 있습니다.

class func uuid(completionHandler: @escaping (String) -> ()) {
    if let uuid = UIDevice.current.identifierForVendor?.uuidString {
        completionHandler(uuid)
    }
    else {
        // If the value is nil, wait and get the value again later. This happens, for example, after the device has been restarted but before the user has unlocked the device.
        // https://developer.apple.com/documentation/uikit/uidevice/1620059-identifierforvendor?language=objc
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            uuid(completionHandler: completionHandler)
        }
    }
}

와 함께 시도했습니다.

let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString

대신

let UUID = NSUUID().UUIDString

그리고 그것은 동작한다.

언급URL : https://stackoverflow.com/questions/25925481/how-to-get-a-unique-device-id-in-swift 입니다.

반응형