密码保存
安全地将凭据保存到系统钥匙串
Capacitor 自动填充保存密码插件为 Capacitor 应用程序提供密码保存和自动填充功能。该插件与系统级凭据管理集成,通过安全的密码存储和检索提供无缝的身份验证体验。
密码保存
安全地将凭据保存到系统钥匙串
自动填充集成
系统级密码自动填充支持
跨平台
支持 iOS,Android 开发中
域名关联
关联域名实现无缝身份验证
npm install @capgo/capacitor-autofill-save-passwordnpx cap sync在 Apple Developer 账户中配置关联域名,并将其添加到 App.entitlements 文件中:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>com.apple.developer.associated-domains</key> <array> <string>webcredentials:yourdomain.com</string> <string>webcredentials:www.yourdomain.com</string> </array></dict></plist>在网站上添加 .well-known/apple-app-site-association 文件:
{ "webcredentials": { "apps": [ "TEAMID.com.yourcompany.yourapp" ] }}promptDialog(options) - 将密码保存到系统钥匙串readPassword() - 从钥匙串读取保存的密码import { SavePassword } from '@capgo/capacitor-autofill-save-password';
// 成功登录后保存密码async function login(username: string, password: string) { try { // 在此处执行登录逻辑 const loginSuccess = await performLogin(username, password);
if (loginSuccess) { // 提示用户保存密码 await SavePassword.promptDialog({ username: username, password: password, url: 'https://yourdomain.com' // 仅限 iOS });
console.log('Password saved successfully'); } } catch (error) { console.error('Failed to save password:', error); }}
// 读取保存的密码以进行自动填充async function loadSavedCredentials() { try { const credentials = await SavePassword.readPassword();
if (credentials.username && credentials.password) { console.log('Found saved credentials'); // 预填充登录表单 return { username: credentials.username, password: credentials.password }; } } catch (error) { console.error('Failed to read saved password:', error); }
return null;}对于未来的 Android 支持,将需要以下配置:
添加到 android/app/build.gradle:
apply plugin: 'com.google.gms.google-services'添加到 android/app/src/main/res/values/strings.xml:
<resources> <string name="asset_statements"> [{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "web", "site": "https://yourdomain.com" } }] </string></resources>将 google-services.json 添加到 Android 项目中。
将用户凭据保存到系统钥匙串。
interface SavePasswordOptions { username: string; password: string; url?: string; // 仅限 iOS - 关联域名 URL}从系统钥匙串检索保存的凭据。
interface SavedCredentials { username: string; password: string;}import { SavePassword } from '@capgo/capacitor-autofill-save-password';
class AuthService { async login(username: string, password: string) { try { // 使用后端进行身份验证 const response = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) });
if (response.ok) { // 提供保存凭据的选项 await this.offerToSavePassword(username, password); return true; } } catch (error) { console.error('Login failed:', error); } return false; }
private async offerToSavePassword(username: string, password: string) { try { await SavePassword.promptDialog({ username, password, url: 'https://yourdomain.com' }); } catch (error) { // 用户拒绝保存或发生错误 console.log('Password not saved:', error); } }
async loadSavedCredentials() { try { return await SavePassword.readPassword(); } catch (error) { console.log('No saved credentials found'); return null; } }}查看完整文档,了解详细的实施指南和高级集成模式。