为什么模拟器中的 UUID 为 nil?
Why is UUID nil in simulator?
我正在完全使用 Swift.
开发一个 tvOS 应用程序
在AppDelegate.swift
中,我尝试在willFinishLaunchingWithOptions
下打印UUID
。
但是,错误消息显示 fatal error: unexpectedly found nil while unwrapping an Optional value
。
该应用程序实际上可以正常运行并且之前运行良好。这个错误发生在我重置模拟器之后。
这是我如何得到 UUID
。
let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
print ("This device uuid is " + newuuid!) // error message happens here
我相信每个设备都应该包含一个 UUID,但这是怎么发生的?
UUID 仅为真实设备生成,而不是为 iOS 模拟器生成,因为硬件设备需要在 Apple 网络中注册才能访问设备令牌。
UDID and Device Token for Push are simulated that's we can send push
to simulator. they are only for development perspective. UDID keeps
changing for simulator. Same for messages, app links, mails as their
template can see on simulator bur couldn't test it.UDID will change each time that the application is re-installed
您正在尝试从 UserDefaults
获取 newuuid
:
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
但是您在重置模拟器后 UserDefaults
中的条目为零,并且还没有为键“uuid”设置值。
这没有多大意义,但解决了问题:
let uuidValue = UIDevice.current.identifierForVendor!.uuidString
UserDefaults.standard.setValue(uuidValue, forKey: "uuid") // add entry in UserDefaults
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
print ("This device uuid is " + newuuid!)
我正在完全使用 Swift.
开发一个 tvOS 应用程序在AppDelegate.swift
中,我尝试在willFinishLaunchingWithOptions
下打印UUID
。
但是,错误消息显示 fatal error: unexpectedly found nil while unwrapping an Optional value
。
该应用程序实际上可以正常运行并且之前运行良好。这个错误发生在我重置模拟器之后。
这是我如何得到 UUID
。
let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
print ("This device uuid is " + newuuid!) // error message happens here
我相信每个设备都应该包含一个 UUID,但这是怎么发生的?
UUID 仅为真实设备生成,而不是为 iOS 模拟器生成,因为硬件设备需要在 Apple 网络中注册才能访问设备令牌。
UDID and Device Token for Push are simulated that's we can send push to simulator. they are only for development perspective. UDID keeps changing for simulator. Same for messages, app links, mails as their template can see on simulator bur couldn't test it.UDID will change each time that the application is re-installed
您正在尝试从 UserDefaults
获取 newuuid
:
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
但是您在重置模拟器后 UserDefaults
中的条目为零,并且还没有为键“uuid”设置值。
这没有多大意义,但解决了问题:
let uuidValue = UIDevice.current.identifierForVendor!.uuidString
UserDefaults.standard.setValue(uuidValue, forKey: "uuid") // add entry in UserDefaults
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
print ("This device uuid is " + newuuid!)