diff --git a/example/lib/main.dart b/example/lib/main.dart index fbbea62..3365285 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_local_authentication/flutter_local_authentication.dart'; @@ -44,6 +45,10 @@ class _HomeWidgetState extends State { cancelButtonTitle: "cancel" ); _flutterLocalAuthenticationPlugin.setLocalizationModel(localization); + + if (Platform.isIOS) { + _flutterLocalAuthenticationPlugin.setBiometricsRequired(false); + } } Future checkSupport() async { diff --git a/example/pubspec.yaml b/example/pubspec.yaml index dbfe9e7..0cd3b37 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -5,7 +5,7 @@ description: "Demonstrates how to use the flutter_local_authentication plugin." publish_to: 'none' # Remove this line if you wish to publish to pub.dev environment: - sdk: '>=3.1.3 <4.0.0' + sdk: '>=3.1.0 <4.0.0' # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions diff --git a/ios/Classes/FlutterLocalAuthenticationPlugin.swift b/ios/Classes/FlutterLocalAuthenticationPlugin.swift index 5339cd3..ad90e54 100644 --- a/ios/Classes/FlutterLocalAuthenticationPlugin.swift +++ b/ios/Classes/FlutterLocalAuthenticationPlugin.swift @@ -18,6 +18,7 @@ import LocalAuthentication public class FlutterLocalAuthenticationPlugin: NSObject, FlutterPlugin { let context = LAContext() + var authPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics; var localizationModel = LocalizationModel.default /// Registers the plugin with the Flutter engine. @@ -41,16 +42,16 @@ public class FlutterLocalAuthenticationPlugin: NSObject, FlutterPlugin { } switch method { case .canAuthenticate: - let (supports, error) = supportsLocalAuthentication(with: .deviceOwnerAuthenticationWithBiometrics) + let (supports, error) = supportsLocalAuthentication(with: authPolicy) result(supports && error == nil) case .authenticate: - authenticate { autheticated, error in + authenticate(with: authPolicy) { authenticated, error in if let error = error { let flutterError = FlutterError(code: "authentication_error", message: error.localizedDescription, details: nil) result(flutterError) return } - result(autheticated) + result(authenticated) } case .setTouchIDAuthenticationAllowableReuseDuration(let duration): setTouchIDAuthenticationAllowableReuseDuration(duration) @@ -61,6 +62,8 @@ public class FlutterLocalAuthenticationPlugin: NSObject, FlutterPlugin { if let model { localizationModel = model } + case .setBiometricsRequired(let biometricsRequired): + setBiometricsRequired(biometricsRequired) } } @@ -80,7 +83,7 @@ public class FlutterLocalAuthenticationPlugin: NSObject, FlutterPlugin { /// - Parameters: /// - policy: The authentication policy to use. /// - callback: A callback to handle the authentication result. - fileprivate func authenticate(with policy: LAPolicy = .deviceOwnerAuthenticationWithBiometrics, callback: @escaping (Bool, Error?) -> Void) { + fileprivate func authenticate(with policy: LAPolicy, callback: @escaping (Bool, Error?) -> Void) { context.evaluatePolicy(policy, localizedReason: localizationModel.reason, reply: callback) } @@ -102,4 +105,12 @@ public class FlutterLocalAuthenticationPlugin: NSObject, FlutterPlugin { fileprivate func getTouchIDAuthenticationAllowableReuseDuration() -> Double { return context.touchIDAuthenticationAllowableReuseDuration } + + fileprivate func setBiometricsRequired(_ biometricsRequired: Bool) { + if biometricsRequired { + authPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics + } else { + authPolicy = LAPolicy.deviceOwnerAuthentication + } + } } diff --git a/ios/Classes/PluginMethod.swift b/ios/Classes/PluginMethod.swift index 3c2ccd2..d44d8d1 100644 --- a/ios/Classes/PluginMethod.swift +++ b/ios/Classes/PluginMethod.swift @@ -15,6 +15,7 @@ enum PluginMethod { case setTouchIDAuthenticationAllowableReuseDuration(duration: Double) case getTouchIDAuthenticationAllowableReuseDuration case setLocalizationModel(model: LocalizationModel?) + case setBiometricsRequired(biometricsRequired: Bool) static func from(_ call: FlutterMethodCall) -> PluginMethod? { switch call.method { @@ -29,6 +30,14 @@ enum PluginMethod { case "setLocalizationModel": let model = LocalizationModel.from(call.arguments as? [String: Any]) return .setLocalizationModel(model: model) + case "setBiometricsRequired": + if + let arguments = call.arguments as? [String: Any], + let biometricsRequired : Bool = arguments["biometricsRequired"] as? Bool { + return .setBiometricsRequired(biometricsRequired: biometricsRequired) + } else { + return nil + } default: return nil } diff --git a/lib/flutter_local_authentication.dart b/lib/flutter_local_authentication.dart index efdc79d..d896f1c 100644 --- a/lib/flutter_local_authentication.dart +++ b/lib/flutter_local_authentication.dart @@ -184,4 +184,12 @@ class FlutterLocalAuthentication { await FlutterLocalAuthenticationPlatform.instance.setLocalizationModel(localizationModel.toJson()); } } + + Future setBiometricsRequired( + bool biometricsRequired) async { + if (Platform.isIOS) { + return await FlutterLocalAuthenticationPlatform.instance + .setBiometricsRequired(biometricsRequired); + } + } } diff --git a/lib/flutter_local_authentication_method_channel.dart b/lib/flutter_local_authentication_method_channel.dart index 4fd5d1b..ccb5e42 100644 --- a/lib/flutter_local_authentication_method_channel.dart +++ b/lib/flutter_local_authentication_method_channel.dart @@ -102,4 +102,10 @@ class MethodChannelFlutterLocalAuthentication Map localizationModel) async { await methodChannel.invokeMethod('setLocalizationModel', localizationModel); } + + @override + Future setBiometricsRequired( + bool biometricsRequired) async { + await methodChannel.invokeMethod('setBiometricsRequired', {'biometricsRequired': biometricsRequired}); + } } diff --git a/lib/flutter_local_authentication_platform_interface.dart b/lib/flutter_local_authentication_platform_interface.dart index 51bf3e6..31940ad 100644 --- a/lib/flutter_local_authentication_platform_interface.dart +++ b/lib/flutter_local_authentication_platform_interface.dart @@ -74,4 +74,11 @@ abstract class FlutterLocalAuthenticationPlatform extends PlatformInterface { throw UnimplementedError( 'setLocalizationModel() has not been implemented.'); } + + /// Sets whether biometrics are required (iOS only). + Future setBiometricsRequired( + bool biometricsRequired) { + throw UnimplementedError( + 'setBiometricsRequired() has not been implemented.'); + } }