Skip to content

Commit

Permalink
test: Add biometric prompt to permissions (#3624)
Browse files Browse the repository at this point in the history
Add a button to trigger biometric prompt for the iOS-Swift test app.
  • Loading branch information
philipphofmann authored Feb 9, 2024
1 parent 0559a8f commit ac8fd53
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

<key>NSFaceIDUsageDescription</key>
<string>$(PRODUCT_NAME) Authentication with TouchId or FaceID for testing purposes of the Sentry Cocoa SDK.</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CoreLocation
import LocalAuthentication
import UIKit

class PermissionsViewController: UIViewController {
Expand All @@ -18,9 +19,16 @@ class PermissionsViewController: UIViewController {
button.addTarget(self, action: #selector(requestLocationPermission), for: .touchUpInside)
return button
}()

private lazy var biometricButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Request Biometric Permission", for: .normal)
button.addTarget(self, action: #selector(requestBiometricPermission), for: .touchUpInside)
return button
}()

private lazy var stackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [pushPermissionButton, locationPermissionButton])
let stackView = UIStackView(arrangedSubviews: [pushPermissionButton, locationPermissionButton, biometricButton])
stackView.axis = .vertical
stackView.spacing = 10
stackView.alignment = .center
Expand Down Expand Up @@ -71,6 +79,34 @@ class PermissionsViewController: UIViewController {
print(granted)
}
}

@objc func requestBiometricPermission() {
let context = LAContext()
var error: NSError?

if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "Identify yourself!"

context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, _ in

DispatchQueue.main.async {
if success {
let crumb = Breadcrumb()
crumb.message = "Biometry success"
SentrySDK.addBreadcrumb(crumb)
} else {
let crumb = Breadcrumb()
crumb.message = "Biometry failure"
SentrySDK.addBreadcrumb(crumb)
}
}
}
} else {
let ac = UIAlertController(title: "No biometry", message: "Couldn't access biometry.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
}
}
}

extension PermissionsViewController: CLLocationManagerDelegate {
Expand Down

0 comments on commit ac8fd53

Please sign in to comment.