Skip to content

Commit

Permalink
Fixes async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Vasilenko committed Dec 3, 2016
1 parent cd5005e commit 436fb73
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@
040536341DF01AD400F189BA /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ class ViewController: UIViewController {
print("Something went wrong")
}

var success = false
do {
success = try self.biometricAuth.requestAuthentication(forFeature: feature, reason: "For authentication")
} catch let error as BiometricAuthError {
print(error.localizedDescription)
} catch {
print("Something went wrong")
}

if success {
print("Success biometric authentication")
}
self.biometricAuth.requestAuthentication(forFeature: feature, reason: "Reason", completion: { (result, error) in
if result {
print("Success")
}
})
}
}

64 changes: 39 additions & 25 deletions Sources/BiometricAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public enum BiometricAuthError : Error {
case domainStateChanged
}

public typealias BiometricAuthCompletion = (Bool, Error?) -> Void

@available(iOSApplicationExtension 9.0, *)
@available(OSXApplicationExtension 10.12, *)
public class BiometricAuth {
Expand Down Expand Up @@ -89,47 +91,59 @@ public class BiometricAuth {
return true
}

public func disableAuthentication(forFeature feature: String, reason: String) throws -> Bool {
public func disableAuthentication(forFeature feature: String, reason: String, completion: @escaping BiometricAuthCompletion) {

guard try self.isAvailable() else {
return false
}
self.isAvailable(withCompletion: completion)

if try self.evaluateAuthentication(withReason: reason) {
self.save(feature: feature, enable: false)
return true
} else {
return false
}
self.evaluateAuthentication(withReason: reason, completion: {(result, error) -> Void in
if let error = error {
completion(false, error)
} else {
if result {
self.save(feature: feature, enable: false)
completion(true, nil)
} else {
completion(false, nil)
}
}
})
}

public func requestAuthentication(forFeature feature: String, reason: String) throws -> Bool {
public func requestAuthentication(forFeature feature: String, reason: String, completion: BiometricAuthCompletion!) {

guard try self.isAvailable() else {
return false
}
self.isAvailable(withCompletion: completion)

if (self.isAuthenticationAvailable(forFeature: feature)) {
return try self.evaluateAuthentication(withReason: reason)
self.evaluateAuthentication(withReason: reason, completion: completion)
} else {
return false
completion(false, nil)
}
}

fileprivate func evaluateAuthentication(withReason reason: String) throws -> Bool {
fileprivate func evaluateAuthentication(withReason reason: String, completion: BiometricAuthCompletion!) {

var success : Bool = false
var evaluateError : Error?
self.authenticationContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,
localizedReason: reason,
reply: { (result, error) in
success = result
evaluateError = error })
if let evaluateError = evaluateError {
throw BiometricAuthError.evaluateAuthenticationError(evaluateError.localizedDescription)
}
if let error = error {
completion(false, BiometricAuthError.evaluateAuthenticationError(error.localizedDescription))
} else {
completion(result, nil)
}
})
}

fileprivate func isAvailable(withCompletion completion: BiometricAuthCompletion) {

return success
do {
if try !self.isAvailable() {
completion(false, nil)
}
} catch let error as BiometricAuthError {
completion(false, error)
} catch {
completion(false, BiometricAuthError.authenticationNotAvailable("Something went wrong"))
}
}

// MARK: Storage
Expand Down

0 comments on commit 436fb73

Please sign in to comment.