xcode 如何检测设备是否支持 FaceID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46402657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect whether device support FaceID or not?
提问by Aleem
Its bit early to ask but I'm planning to add feature specially for FaceID, so before that I need to validate either device support FaceID or not? Need suggestion and help. Thanks in advance.
现在问还为时过早,但我计划专门为 FaceID 添加功能,所以在此之前我需要验证任一设备是否支持 FaceID?需要建议和帮助。提前致谢。
回答by Stuart P.
I found that you have to call canEvaluatePolicy before you will properly get the biometry type. If you don't you'll always get 0 for the raw value.
我发现您必须先调用 canEvaluatePolicy 才能正确获取生物识别类型。如果不这样做,原始值将始终为 0。
So something like this in Swift 3, tested and working in Xcode 9.0 & beta 9.0.1.
所以在 Swift 3 中类似这样的东西,在 Xcode 9.0 & beta 9.0.1 中测试和工作。
class func canAuthenticateByFaceID () -> Bool {
//if iOS 11 doesn't exist then FaceID doesn't either
if #available(iOS 11.0, *) {
let context = LAContext.init()
var error: NSError?
if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
//As of 11.2 typeFaceID is now just faceID
if (context.biometryType == LABiometryType.typeFaceID) {
return true
}
}
}
return false
}
You could of course write that just to see if it's either biometric and return the type along with the bool but this should be more than enough for most to work off of.
您当然可以编写它只是为了查看它是否是生物特征并将类型与布尔值一起返回,但这对于大多数人来说应该足够了。
回答by Hai Hw
Objective-C version
Objective-C 版本
- (BOOL) isFaceIdSupported{
if (@available(iOS 11.0, *)) {
LAContext *context = [[LAContext alloc] init];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]){
return ( context.biometryType == LABiometryTypeFaceID);
}
}
return NO;
}
回答by Aleem
Thanks Ashley Mills, I created a function to detect FaceID in Device.
感谢 Ashley Mills,我创建了一个函数来检测设备中的 FaceID。
- (BOOL)canAuthenticateByFaceID {
LAContext *context = [[LAContext alloc] init];
if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
if (context.biometryType == LABiometryTypeFaceID && @available(iOS 11.0, *)) {
return YES;
} else {
return NO;
}
}
}
Hope this will help other. Happy coding!!
希望这会帮助其他人。快乐编码!!
Finally I wrote my own Library for detecting FaceID here you find
最后我写了我自己的库来检测 FaceID在这里你找到
回答by rockdaswift
Swift 4 compatible version
Swift 4 兼容版本
var isFaceIDSupported: Bool {
if #available(iOS 11.0, *) {
let localAuthenticationContext = LAContext()
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
return localAuthenticationContext.biometryType == .faceID
}
}
return false
}
回答by Mo Farhand
+(BOOL)supportFaceID
{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
// call this method only to get the biometryType and we don't care about the result!
[myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError];
NSLog(@"%@",authError.localizedDescription);
if (@available(iOS 11.0, *)) {
return myContext.biometryType == LABiometryTypeFaceID;
} else {
// Device is running on older iOS version and OFC doesn't have FaceID
return NO;
}
}