Skip to content

Support optional biometrics on iOS platform. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_local_authentication/flutter_local_authentication.dart';
Expand Down Expand Up @@ -44,6 +45,10 @@ class _HomeWidgetState extends State<HomeWidget> {
cancelButtonTitle: "cancel"
);
_flutterLocalAuthenticationPlugin.setLocalizationModel(localization);

if (Platform.isIOS) {
_flutterLocalAuthenticationPlugin.setBiometricsRequired(false);
}
}

Future<void> checkSupport() async {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions ios/Classes/FlutterLocalAuthenticationPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -61,6 +62,8 @@ public class FlutterLocalAuthenticationPlugin: NSObject, FlutterPlugin {
if let model {
localizationModel = model
}
case .setBiometricsRequired(let biometricsRequired):
setBiometricsRequired(biometricsRequired)
}
}

Expand All @@ -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)
}

Expand All @@ -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
}
}
}
9 changes: 9 additions & 0 deletions ios/Classes/PluginMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions lib/flutter_local_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,12 @@ class FlutterLocalAuthentication {
await FlutterLocalAuthenticationPlatform.instance.setLocalizationModel(localizationModel.toJson());
}
}

Future<void> setBiometricsRequired(
bool biometricsRequired) async {
if (Platform.isIOS) {
return await FlutterLocalAuthenticationPlatform.instance
.setBiometricsRequired(biometricsRequired);
}
}
}
6 changes: 6 additions & 0 deletions lib/flutter_local_authentication_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ class MethodChannelFlutterLocalAuthentication
Map<String, dynamic> localizationModel) async {
await methodChannel.invokeMethod('setLocalizationModel', localizationModel);
}

@override
Future<void> setBiometricsRequired(
bool biometricsRequired) async {
await methodChannel.invokeMethod('setBiometricsRequired', {'biometricsRequired': biometricsRequired});
}
}
7 changes: 7 additions & 0 deletions lib/flutter_local_authentication_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ abstract class FlutterLocalAuthenticationPlatform extends PlatformInterface {
throw UnimplementedError(
'setLocalizationModel() has not been implemented.');
}

/// Sets whether biometrics are required (iOS only).
Future<void> setBiometricsRequired(
bool biometricsRequired) {
throw UnimplementedError(
'setBiometricsRequired() has not been implemented.');
}
}