21

When compiling my Application with Xcode 9 for IOS11 I get the following warnings:

warning: 'touchIDLockout' was deprecated in iOS 11.0: use LAErrorBiometryLockout

warning: 'touchIDNotEnrolled' was deprecated in iOS 11.0: use LAErrorBiometryNotEnrolled

warning: 'touchIDNotAvailable' was deprecated in iOS 11.0: use LAErrorBiometryNotAvailable

I'm using touchID but I'm not using touchIdLockout...cste and the touchID is working correctly.

How can I remove these warnings?


Edit (not by the original author):

I tracked this down to a single cause. It's enough to reference LAError from the LocalAuthentication framework in my code to make these warnings appear.

Steps to reproduce (tried in Xcode 9.2):

  1. Create a new iOS app (Single View template). Note the iOS Deployment Target is set to iOS 11.2.
  2. Add these lines to AppDelegate.swift:

    import LocalAuthentication
    

    And a single line in appDidFinishLaunching:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let _: LAError? = nil
        return true
    }
    
  3. Build the app.

The let _: LAError? = nil line is enough to make the three warnings appear. The warnings are not associated with any particular line of code, though. They appear in the build log without any file/line reference:

<unknown>:0: warning: 'touchIDLockout' was deprecated in iOS 11.0: use LAErrorBiometryLockout
<unknown>:0: warning: 'touchIDNotEnrolled' was deprecated in iOS 11.0: use LAErrorBiometryNotEnrolled
<unknown>:0: warning: 'touchIDNotAvailable' was deprecated in iOS 11.0: use LAErrorBiometryNotAvailable

Here's a screenshot: Screenshot of the warnings in Xcode

And a sample project: Sample project for download (Xcode 9.2)

For reference, I reported this to Apple. Radar #36028653.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
sebastien
  • 2,489
  • 5
  • 26
  • 47

4 Answers4

25

Short answer: It looks like a compiler bug to me, caused by the import of a C enumeration which defines multiple constants with the same value.

Long answer: Unfortunately I do not have a solution how to avoid the deprecation warning, only a possible explanation what causes it.

The LAError codes are defined as a C enumeration in <LAError.h> in the LocalAuthentication framework. Here is an extract of that definition:

// Error codes
#define kLAErrorAuthenticationFailed                       -1
#define kLAErrorUserCancel                                 -2
// ...
#define kLAErrorTouchIDNotAvailable                        -6
#define kLAErrorTouchIDNotEnrolled                         -7
#define kLAErrorTouchIDLockout                             -8
// ...
#define kLAErrorBiometryNotAvailable                        kLAErrorTouchIDNotAvailable
#define kLAErrorBiometryNotEnrolled                         kLAErrorTouchIDNotEnrolled
#define kLAErrorBiometryLockout                             kLAErrorTouchIDLockout

typedef NS_ENUM(NSInteger, LAError)
{
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
    LAErrorUserCancel = kLAErrorUserCancel,
    // ...
    LAErrorTouchIDNotAvailable NS_ENUM_DEPRECATED(10_10, 10_13, 8_0, 11_0, "use LAErrorBiometryNotAvailable") = kLAErrorTouchIDNotAvailable,
    LAErrorTouchIDNotEnrolled NS_ENUM_DEPRECATED(10_10, 10_13, 8_0, 11_0, "use LAErrorBiometryNotEnrolled") = kLAErrorTouchIDNotEnrolled,
    LAErrorTouchIDLockout NS_ENUM_DEPRECATED(10_11, 10_13, 9_0, 11_0, "use LAErrorBiometryLockout")
    __WATCHOS_DEPRECATED(3.0, 4.0, "use LAErrorBiometryLockout") __TVOS_DEPRECATED(10.0, 11.0, "use LAErrorBiometryLockout") = kLAErrorTouchIDLockout,
    // ...
    LAErrorBiometryNotAvailable NS_ENUM_AVAILABLE(10_13, 11_0) __WATCHOS_AVAILABLE(4.0) __TVOS_AVAILABLE(11.0) = kLAErrorBiometryNotAvailable,
    LAErrorBiometryNotEnrolled NS_ENUM_AVAILABLE(10_13, 11_0) __WATCHOS_AVAILABLE(4.0) __TVOS_AVAILABLE(11.0) = kLAErrorBiometryNotEnrolled,
    LAErrorBiometryLockout NS_ENUM_AVAILABLE(10_13, 11_0) __WATCHOS_AVAILABLE(4.0) __TVOS_AVAILABLE(11.0) = kLAErrorBiometryLockout,
    // ...
} NS_ENUM_AVAILABLE(10_10, 8_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0);

One can see that "old" (deprecated) and the "new" error codes use the same values. For example, both LAErrorTouchIDNotAvailable and LAErrorBiometryNotAvailable are defined as -6.

That is perfectly valid in C, but the raw values of a Swift enum must be mutually distinct. Apparently the Swift importer solves that by mapping the new/duplicate cases to static variables.

Here is an extract of the Swift mapping:

public struct LAError {

    public init(_nsError: NSError)
    public static var _nsErrorDomain: String { get }


    public enum Code : Int {
        case authenticationFailed
        case userCancel
        // ...
        @available(iOS, introduced: 8.0, deprecated: 11.0, message: "use LAErrorBiometryNotAvailable")
        case touchIDNotAvailable
        @available(iOS, introduced: 8.0, deprecated: 11.0, message: "use LAErrorBiometryNotEnrolled")
        case touchIDNotEnrolled
        @available(iOS, introduced: 9.0, deprecated: 11.0, message: "use LAErrorBiometryLockout")
        case touchIDLockout
        // ...
        @available(iOS 11.0, *)
        public static var biometryNotAvailable: LAError.Code { get }
        @available(iOS 11.0, *)
        public static var biometryNotEnrolled: LAError.Code { get }
        @available(iOS 11.0, *)
        public static var biometryLockout: LAError.Code { get }
        // ...
    }

    // ...
}

And this seems to be the cause for the deprecation warnings, and also for the problem reported on the swift-users mailing list

that it is impossible to write an exhaustive and warning-free switch statement for LAError.


To prove my conjecture, I have reproduced the problem with a custom enumeration: Add the following definition to the bridging header file of an macOS 10.13 or iOS 11 project:

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, MyEnum)
{
    MyEnumA = 1,
    MyEnumB = 2,
    MyEnumC NS_ENUM_DEPRECATED(10_10, 10_13, 8_0, 11_0, "use MyEnumNewC") = 3,

    MyEnumNewC NS_ENUM_AVAILABLE(10_13, 11_0) = 3,
};

This is imported to Swift as

 public enum MyEnum : Int {
    case A
    case B
    @available(OSX, introduced: 10_10, deprecated: 10_13, message: "use MyEnumNewC")
    case C

    @available(OSX 10_13, *)
    public static var newC: MyEnum { get }
 }

with 3 cases for the first (distinct) enum values, and a static property for the duplicate value.

And indeed, any use of MyEnum triggers a deprecation warning:

// main.swift:
print(MyEnum.A) // Or: let _: MyEnum? = nil

// Build log:
// <unknown>:0: warning: 'C' was deprecated in OS X 10.13: use MyEnumNewC

In addition, it is not possible to use the new enum value in a switch statement:

func foo(err: MyEnum) {
    switch err {
    case .A:
        print("A")
    case .B:
        print("B")
    case .newC:
        print("C")
    }
}

// Build log:
// main.swift:12:9: error: switch must be exhaustive
// <unknown>:0: warning: 'C' was deprecated in OS X 10.13: use MyEnumNewC

even if the compiler (apparently) knows that these cases are exhaustive:

func foo(err: MyEnum) {
    switch err { // Switch must be exhaustive
    case .A:
        print("A")
    case .B:
        print("B")
    case .newC:
        print("C")
    default:
        print("default")
    }
}

// Build log:
// <unknown>:0: warning: 'C' was deprecated in OS X 10.13: use MyEnumNewC
// main.swift:19:9: warning: default will never be executed

This looks like a compiler bug to me.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    Thanks for the comprehensive answer! I believe you're right. – Ole Begemann Dec 13 '17 at 22:07
  • Thank you for this comprehensive report. Is there a clang/llvm issue somewhere that you can track? – skagedal Oct 11 '18 at 07:50
  • @skagedal: None that I know of. It has been discussed in the Swift forums (https://forums.swift.org/t/handle-deprecated-enum-cases-from-a-library/6952), but I cannot find a corresponding issue at https://bugs.swift.org, so apparently nobody reported it so far. – Martin R Oct 11 '18 at 08:08
  • @MartinR Thanks. I commented to at least bump it on the forums. – skagedal Oct 12 '18 at 12:25
  • 1
    Ah, there is a Swift bug from Ole. https://bugs.swift.org/browse/SR-6637 – skagedal Oct 12 '18 at 18:42
7

Yes, these are new warnings which are present as Apple moves to iOS 11 and FaceID. Most likely, you are checking to see if the biometric hardware is not locked out, has enrolled fingerprints, and the device has the supporting hardware.

Here an example set up:

import LocalAuthentication

...

var authContext = LAContext()
var biometricsError: NSError?
authContext?.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &biometricsError)

Up to iOS 10, one would run checks like this:

if biometricsError?.code == LAError.touchIDNotAvailable.rawValue {
  // No hardware
}

if biometricsError?.code == LAError.touchIDNotEnrolled.rawValue {
  // No fingerprints
}

if biometricsError?.code == LAError.touchIDLockout.rawValue {
  // Locked out
}

Note: iOS 11 introduced a slight variant of the above code. Instead of using LAError.touchID for each error property, they introduced LAError.biometry. Therefore you would have: biometryNotAvailable, biometryNotEnrolled, and biometryLockout.

Apple seems to prefer this approach, instead:

if biometricsError?.code == Int(kLAErrorBiometryNotAvailable) {
  // No hardware
}

if biometricsError?.code == Int(kLAErrorBiometryNotEnrolled) {
  // No fingerprints
}

if biometricsError?.code == Int(kLAErrorBiometryLockout) {
  // Locked out
}

This method gets rid of Xcode's warnings.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • 3
    Thanks for the reply but unfortunately it doesn't remove the warning because I'm not using LAError.touchIDNotAvailable, LAError.touchIDNotEnrolled or LAError.touchIDLockout. I agree these constants are obsolete in IOS11 but I'm not using them! – sebastien Dec 10 '17 at 16:57
  • It should be noted that this is not an approach that Apple seems to prefer, but a confirmed compiler bug – see the above answer, The workaround of using the kLAError constants _does_ work though. But with a caveat. I'll add my own answer. – skagedal Oct 12 '18 at 18:59
5

As has been noted, this is a bug in the compiler. The Swift team are aware and you might want to go and vote for the bug. At the same time, add a watch on it so that you can remove the following workaround when it has been fixed.

What you need to do in order to not get the warnings is: do not mention LAError. Think of LAError as Voldemort.

Instead, use Objective-C style error checking. All Error enums map to an NSError, which are built up from a domain and a code. The constants to compare these with are also exported to Swift. They can be named without warnings. So your code might look a little (hopefully, very little) like this.

let context = LAContext()
let text = "Authenticate, please!"
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: text) { (success, error) in
    if success {
        print("")
    } else {
        guard let error = error else {
            return print("Should not happen according to the docs!")
        }
        let nsError = error as NSError
        switch nsError.domain {
        case kLAErrorDomain:
            switch nsError.code {
            case Int(kLAErrorUserCancel):
                print("User cancelled.")
            case Int(kLAErrorBiometryLockout):
                print("Biometry lockout.")
            default:
                print("Unhandled error.")
            }
        default:
            print("Unhandled error domain. Probably will not happen.")
        }
    }
}
skagedal
  • 2,323
  • 23
  • 34
  • (Yes, other, existing answers showed same technique but none really brought it all together and did not mention to check for the domain so I thought I'd write this up.) – skagedal Oct 12 '18 at 19:27
0

I too struggled with this for a very long time. Oliver's answer led me to a solution that worked for me, but just in case there are others that are still having this issue - it seems that ANY reference to the old LAError values will produce the warnings listed above.

For example, this simple code produces the warnings. Remove ALL reference to the old codes and use Oliver's approach.

func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
    var message = ""

    switch errorCode {
    case LAError.authenticationFailed.rawValue:
        message = "The user failed to provide valid credentials"
    default:
        message = "Can't use any version of old LAError"
    }

    return message
}//evaluateAuthenticationPolicyMessageForLA

In fact, it can be even simpler. Try this:

func evaluateAuthenticationPolicyMessageForBiometricsError(biometricsError: Int) -> String {

    var message = "I would normally leave this blank"

    if biometricsError == kLAErrorBiometryNotAvailable {
        message = "Biometrics are not available on this device"
    }//if not avail

    if biometricsError == kLAErrorBiometryNotEnrolled {
        message = "Biometrics are not enrolled on this device"
    }//if not enrolled

    if biometricsError == kLAErrorBiometryLockout {
        message = "Biometrics are locked out on this device"
    }//if locked out

    return message

}//evaluateAuthenticationPolicyMessageForBiometricsError
JohnSF
  • 3,736
  • 3
  • 36
  • 72