Jak uzyskać unikalny identyfikator urządzenia w Swift?
Jak mogę uzyskać unikalny identyfikator urządzenia w Swift?
Potrzebuję ID do użycia w bazie danych i jako klucz API dla mojej usługi internetowej w mojej aplikacji społecznościowej. Coś do śledzenia tego urządzenia codziennego użytku i ograniczyć jego zapytania do bazy danych.
Dzięki!
6 answers
Możesz użyć tego (Swift 3):
UIDevice.current.identifierForVendor!.uuidString
Dla starszych wersji:
UIDevice.currentDevice().identifierForVendor
Lub jeśli chcesz ciąg znaków:
UIDevice.currentDevice().identifierForVendor!.UUIDString
Nie ma już możliwości jednoznacznej identyfikacji urządzenia po odinstalowaniu aplikacji przez użytkownika. Dokumentacja mówi:
Wartość tej właściwości pozostaje taka sama, gdy aplikacja (lub inna aplikacja od tego samego dostawcy) jest zainstalowana na urządzeniu z systemem iOS. Wartość zmienia się, gdy użytkownik usunie wszystkie aplikacje tego dostawcy z urządzenia i następnie ponownie instaluje jeden lub więcej z nich.
Możesz również przeczytać ten artykuł autorstwa Mattt Thompson, aby uzyskać więcej informacji:
http://nshipster.com/uuid-udid-unique-identifier/
Aktualizacja dla Swift 4.1, musisz użyć:
UIDevice.current.identifierForVendor?.uuidString
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-17 21:34:51
Dla Swift 3.X najnowszy kod roboczy, łatwy w użyciu;
let deviceID = UIDevice.current.identifierForVendor!.uuidString
print(deviceID)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-29 16:21:46
Możesz użyć identifierForVendor public property present in UIDevice class
let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("UUID: \(UUIDValue)")
Edytuj Swift 3:
UIDevice.current.identifierForVendor!.uuidString
END EDIT
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-16 22:21:49
Możesz użyć devicecheck (W Swift 4) Dokumentacja 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" ...
}
}
Typowe użycie:
Zazwyczaj używasz interfejsów API DeviceCheck, aby upewnić się, że nowy użytkownik nie skorzystał już z oferty pod inną nazwą użytkownika na tym samym urządzeniu.
Potrzeba działania serwera:
Więcej z artykułu Santosh Botre-unikalny identyfikator dla urządzeń z systemem iOS
Twój skojarzony serwer łączy ten token z kluczem uwierzytelniania otrzymanym od Apple i wykorzystuje wynik do żądania dostępu do bitów poszczególnych urządzeń.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-03 15:12:54
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")!)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-14 14:18:04
Próbowałem z
let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString
Zamiast
let UUID = NSUUID().UUIDString
I działa.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-01 10:39:13