我在尝试时崩溃了:
static func getDeviceUUID() -> String {
guard let uuid = UIDevice.current.identifierForVendor?.uuidString else {
assertionFailure("Nil while unwrapping UIDevice.current.identifierForVendor?.uuidString")
return ""}
return uuid
}
错误说:Thread 9: Fatal error: Nil while unwrapping UIDevice.current.identifierForVendor?.uuidString
但是当我尝试在控制台中记录它时,它说:
po UIDevice.current.identifierForVendor?.uuidString
▿ Optional<String>
- some : "39DEFA50-D6A1-4788-BCCC-5E2A28A04C57"
所以,它实际上有价值。为什么会发生崩溃?
答案 0 :(得分:0)
工作正常
let frame:CGRect = CGRect(x: 0, y: 600, width: self.view.frame.width, height: self.view.frame.height/3)
self.datePicker = CustomDatePicker.instanceFromNib(frame: frame) as? CustomDatePicker
self.datePicker?.configureDatePicker()
datePicker?.customPickerDelegate = self
self.view.addSubview(self.datePicker!)
UIView.animate(withDuration: 1.5, animations: {
DILog.print(items: "Updated Y is \(self.view.frame.height - self.view.frame.height/3)")
self.datePicker.frame = CGRect(x: 0, y: 300, width: self.view.frame.width, height: self.view.frame.height/3)
}, completion: { (value) in
})
答案 1 :(得分:0)
尝试从AdSupport框架中获取IDFA时,我遇到了类似的问题:
ASIdentifierManager.shared().advertisingIdentifier.uuidString
// causes exception
在这种情况下,似乎Objective-C代码库中有一个标记错误的'nonnull'方法,由于UUID类的uuidString方法返回的是值类型(字符串),因此无法解决。在Obj-C中,它将返回引用类型(NSString): https://forums.swift.org/t/is-it-safe-to-cast-a-nonnull-objc-type-to-a-swift-optional/10658
由于这两个都使用了UUID类,所以我猜这是同样的问题。
上面的链接建议的解决方法是构建一个Obj-C包装器来访问UUID:
+ (nullable NSString*)getUUIDFromDevice {
NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
if (uuid) {
return uuid;
} else {
return nil;
}
}
不幸的是,这很难测试,因为不清楚UUID值何时确切为零。
答案 2 :(得分:0)
也许是因为你没有检查用户是否允许检查它。
guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
return nil
}
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
顺便说一句,对于 iOS14+,您应该进行不同的检查:
import AdSupport
import AppTrackingTransparency
extension ASIdentifierManager {
//NOTE: if the user has enabled Limit Ad Tracking, this IDFA will be all zeros on a physical device
static var identifierForAdvertising: String {
if #available(iOS 14, *) {
guard ATTrackingManager.trackingAuthorizationStatus == .authorized else {
return ""
}
} else {
guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
return ""
}
}
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
}