跳转到内容

入门指南

Terminal window
npm install @capgo/capacitor-autofill-save-password
npx cap sync
  • iOS:完全支持(iOS 18.3 及更早版本)
  • Android:开发中

在 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"
]
}
}

添加到 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>
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;
}
promptDialog(options: SavePasswordOptions) => Promise<void>

将用户凭据保存到系统钥匙串。

参数类型
optionsSavePasswordOptions
readPassword() => Promise<SavedCredentials>

从系统钥匙串中检索保存的凭据。

返回值: Promise<SavedCredentials>

属性类型描述
usernamestring要保存的用户名
passwordstring要保存的密码
urlstring仅限 iOS - 关联域名 URL(可选)
属性类型描述
usernamestring保存的用户名
passwordstring保存的密码
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;
}
}
}
  • 仅在身份验证成功后提示保存密码
  • 妥善处理用户拒绝保存密码的情况
  • 对钥匙串访问失败实施适当的错误处理
  • 使用关联域名实现无缝的 Web 应用凭据共享
  • 在不同 iOS 版本中测试自动填充功能
  • 凭据使用适当的安全标志存储在系统钥匙串中
  • 关联域名确保凭据仅可被授权应用访问
  • 遵循平台安全指南进行凭据管理
  • 对于敏感应用,考虑实施额外的安全措施