ID Verification SDK for Flutter allows you to integrate electronic Know Your Customer (eKYC) capabilities into your Flutter application. This topic describes how to integrate ID Verification SDK for Flutter with an application and provides an example.
Usage notes
Simulators are not supported for ID Verification SDK for Flutter. Use a physical device for development and debugging.
iOS 9.0 and later, or Android 5.0 and later are supported.
Configure dependencies
Download ID Verification SDK for Flutter and decompress the package.
Copy the directory that stores the decompressed package to your project.
Modify the pubspec.yaml file of the project and add the aliyun_face_plugin dependency to the dev_dependencies field of the file.
aliyun_face_plugin: path: aliyun_face_plugin
Configure the environment for Android operating systems
Add dependencies
In your project, choose android > build.gradle. In the build.gradle file, add the flatDir configuration to the allprojects field.
flatDir {
dirs project(':aliyun_face_plugin').file('libs')
}
Configure obfuscation rules for ProGuard
If the ProGuard obfuscator is configured in release scenarios, choose android > app and add the following code to the proguard-rules.pro file.
-verbose
-keep class com.aliyun.identity.platform.api.** {*;}
-keep class com.aliyun.identity.IdentityUtils {*;}
-keep class com.aliyun.identity.ocr.IdentityOcrApi {*;}
-keep class com.aliyun.identity.platform.model.** {*;}
-keep class com.aliyun.identity.platform.config.** {*;}
-keep class com.aliyun.identity.face.IdentityFaceApi {*;}
-keep class com.face.verify.intl.** {*;}
-keep class com.alibaba.fastjson.** {*;}
-keep class com.aliyun.face.aliyun_face_plugin.** {*;}
-dontwarn okio.**
-keepclassmembers,allowobfuscation class * {
@com.alibaba.fastjson.annotation.JSONField <fields>;
}
Configure the environment for iOS operating systems
Add camera permissions
Open Xcode. Find your project and choose ios > Runner.xcworkspace in the left-side navigation pane. Open the Runner project.
Add camera permissions to the Info.plist file of the Runner project. The following figure provides an example. You can configure the Value parameter based on your business requirements.
Add bundles
In the left-side navigation pane of Xcode, click Runner. On the runner page, click the Build Phases tab. On the Build Phases tab, find the Copy Bundle Resources section and add the following bundles.
The bundle files are stored in the Frameworks directory of ID Verification SDK for Flutter. The path to the bundle files is aliyun_face_plugin/ios/Products
. Each bundle corresponds to a .framework file.
ToygerService.bundle: corresponds to ToygerService.framework.
AliyunIdentityPlatform.bundle: corresponds to AliyunIdentityPlatform.framework.
AliyunIdentityOCR.bundle: corresponds to AliyunIdentityOCR.framework.
AliyunIdentityFace.bundle: corresponds to AliyunIdentityFace.framework.
Add the -ObjC linker flag
In the left-side navigation pane of Xcode, click Pods. On the Pods page, click the Build Settings tab. On the Build Settings tab, find Other Linker Flags in the Linking section and add the -ObjC linker flag.
Operation description
ID Verification SDK for Flutter provides the following operations: init, getMetaInfos, and verify.
class AliyunFacePlugin {
// The init operation.
Future<void> init() {
return AliyunFacePluginPlatform.instance.init();
}
// The getMetaInfos operation. ID Verification obtains metadata from the client to obtain the value of transcationId from the server.
Future<String?> getMetaInfos() {
return AliyunFacePluginPlatform.instance.getMetaInfos();
}
// The verify operation.
Future<String?> verify(String key, String value) {
return AliyunFacePluginPlatform.instance.verify(key, value);
}
}
Initialize the SDK
Call the init
operation to initialize ID Verification SDK for Flutter at your earliest opportunity after you cold start the application and confirm the privacy policy.
Obtain metadata
Call the getMetaInfos
operation to obtain the information about the environment of the client. The client sends a request to the business server based on the information that you obtain. Then, the business server sends a request for initialization to Alibaba Cloud to obtain the value of transactionId. This value is used for verification in the following steps.
Start verification
Call the verify
operation to initiate an eKYC request.
The key parameter is fixed to transactionId, which is a string.
The value parameter is fixed to the value of transactionId that you obtain by calling the init operation.
The response is in the
code,reason
format. code indicates a status code, and reason indicates the description of the status code. The values of code and reason are separated by commas (,).The meanings of the values of code and reason vary between the iOS and Android operating systems. For more information, see the related documentation.
The value of transactionId can be used only once. Otherwise, an error occurs, and the following error information is returned:
iOS: 2002,ZIMNetworkfail
Android: 1001,NET_RESPONSE_INVALID
Sample code
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:aliyun_face_plugin/aliyun_face_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _infos = 'Unknown';
final _aliyunFacePlugin = AliyunFacePlugin();
@override
void initState() {
super.initState();
// Call the init operation immediately after you start the application.
_aliyunFacePlugin.init();
}
Future<void> getMetaInfos() async {
String metainfos;
try {
// Obtain the metadata of the client, send the metadata to ID Verification, and then call the related operation of ID Verification to obtain the value of transactionId.
metainfos = await _aliyunFacePlugin.getMetaInfos() ?? 'Unknown metainfos';
} on PlatformException {
metainfos = 'Failed to get metainfos.';
}
setState(() {
_infos = "metainfos: " + metainfos;
});
}
Future<void> startVerify() async {
String verifyResult;
try {
// Call the verify operation. You must first call the related operation of ID Verification to obtain the value of transcationId.
// The value of transcationId can be used only once. Otherwise, an error occurs. An error code of 2002 is returned for iOS, and an error code of 1001 is returned for Android.
verifyResult = await _aliyunFacePlugin.verify(
"transactionId", "hksdecd1823a84321d7360dc5119****") ?? '-1,error';
} on PlatformException {
verifyResult = '-2,exception';
}
setState(() {
_infos = "verifyResult: " + verifyResult;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Aliyun face plugin demo')),
body: Center(
child: Column(children: <Widget>[
Text('$_infos\n'),
ElevatedButton(
onPressed: () async {
getMetaInfos();
},
child: Text("getMetaInfos")),
ElevatedButton(
onPressed: () async {
startVerify();
},
child: Text("startVerify")),
])),
),
);
}
}