更新记录
1.0.0(2026-04-21)
v1.0.0 (2025-12-18)
- ✨ 初始版本
- ✅ 支持Apple登录
- ✅ 支持获取用户信息
- ✅ 完整的错误处理
平台兼容性
uni-app(4.84)
| Vue2 | Vue2插件版本 | Vue3 | Vue3插件版本 | Chrome | Safari | app-vue | app-vue插件版本 | app-nvue | Android | iOS | iOS插件版本 | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | 1.0.0 | √ | 1.0.0 | - | - | √ | 1.0.0 | - | - | 13 | 1.0.0 | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | - | - | - | - | - | - |
Apple Login 使用说明
功能简介
mm-apple-login 是一个用于uni-app的iOS平台Apple登录插件,支持使用Apple ID进行用户认证。
系统要求
- iOS版本: iOS 13.0 及以上
- Xcode配置: 需要在Xcode中启用 "Sign in with Apple" 功能
安装
将插件放置在项目的 uni_modules 目录下即可。
API文档
appleLogin(options)
发起Apple登录请求
参数说明:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| scopes | Array<'fullName' | 'email'> | 否 | 请求的权限范围 |
| success | Function | 否 | 成功回调函数 |
| fail | Function | 否 | 失败回调函数 |
| complete | Function | 否 | 完成回调函数 |
success返回参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| user | string | 用户唯一标识符(稳定不变) |
| identityToken | string | null | 身份令牌(JWT格式,用于服务器验证) |
| authorizationCode | string | null | 授权码(一次性使用,用于服务器) |
| string | null | 用户邮箱(可能是隐私中继邮箱) | |
| fullName | string | null | 用户姓名(仅首次授权时返回) |
| realUserStatus | number | 真实用户状态 (0:unknown, 1:unsupported, 2:likelyReal) |
fail返回参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| errCode | number | 错误码 |
| errMsg | string | 错误信息 |
错误码说明:
| 错误码 | 说明 |
|---|---|
| 9020001 | 用户取消登录 |
| 9020002 | 功能不可用(设备不支持或系统版本过低) |
| 9020003 | 授权失败 |
| 9020004 | 未知错误 |
isAppleLoginAvailable()
检查当前设备是否支持Apple登录
返回值: boolean - true表示支持,false表示不支持
使用示例
基础示例
import { appleLogin, isAppleLoginAvailable } from '@/uni_modules/mm-apple-login';
// 检查是否可用
if (isAppleLoginAvailable()) {
// 发起登录
appleLogin({
scopes: ['fullName', 'email'],
success: (res) => {
console.log('Apple登录成功');
console.log('用户ID:', res.user);
console.log('Identity Token:', res.identityToken);
console.log('邮箱:', res.email);
console.log('姓名:', res.fullName);
// 将token发送到服务器进行验证
// ...
},
fail: (err) => {
console.error('Apple登录失败');
console.error('错误码:', err.errCode);
console.error('错误信息:', err.errMsg);
if (err.errCode === 9020001) {
console.log('用户取消了登录');
}
},
complete: () => {
console.log('登录流程完成');
}
});
} else {
console.log('当前设备不支持Apple登录');
}
完整示例(Vue组件)
<template>
<view class="login-page">
<button
v-if="appleLoginSupported"
@click="handleAppleLogin"
class="apple-login-btn"
>
使用Apple登录
</button>
<text v-else class="unsupported-text">
当前设备不支持Apple登录
</text>
</view>
</template>
<script setup lang="uts">
import { ref } from 'vue';
import { appleLogin, isAppleLoginAvailable } from '@/uni_modules/mm-apple-login';
const appleLoginSupported = ref(false);
// 检查是否支持Apple登录
onMounted(() => {
appleLoginSupported.value = isAppleLoginAvailable();
});
// 处理Apple登录
const handleAppleLogin = () => {
uni.showLoading({
title: '登录中...'
});
appleLogin({
scopes: ['fullName', 'email'],
success: (res) => {
console.log('登录成功', res);
// 发送到服务器验证
uni.request({
url: 'https://your-server.com/api/apple-login',
method: 'POST',
data: {
identityToken: res.identityToken,
authorizationCode: res.authorizationCode,
user: res.user
},
success: (serverRes) => {
// 服务器验证成功
uni.showToast({
title: '登录成功',
icon: 'success'
});
// 保存用户信息
// ...
}
});
},
fail: (err) => {
let message = '登录失败';
switch (err.errCode) {
case 9020001:
message = '您取消了登录';
break;
case 9020002:
message = '设备不支持Apple登录';
break;
case 9020003:
message = '授权失败,请重试';
break;
default:
message = '登录出错: ' + err.errMsg;
}
uni.showToast({
title: message,
icon: 'none'
});
},
complete: () => {
uni.hideLoading();
}
});
};
</script>
<style>
.login-page {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
}
.apple-login-btn {
background-color: #000;
color: #fff;
border-radius: 8px;
padding: 12px 24px;
}
.unsupported-text {
color: #666;
font-size: 14px;
}
</style>
服务器端验证
Apple登录成功后,建议在服务器端验证 identityToken 的有效性:
- 使用Apple的公钥验证JWT签名
- 检查token的过期时间
- 验证token的issuer是
https://appleid.apple.com - 验证audience是你的App ID
更多信息请参考:Apple官方文档
注意事项
[!IMPORTANT] Xcode配置要求:
- 在Xcode项目的
Signing & Capabilities中添加Sign in with Apple能力- 确保App ID在Apple Developer中启用了
Sign in with Apple功能- 首次授权时才会返回用户姓名和邮箱,后续登录可能返回null
[!WARNING] 隐私说明:
- 用户可以选择隐藏真实邮箱,此时会返回Apple的隐私中继邮箱
- 用户姓名仅在第一次授权时返回,请务必保存
user标识符是唯一且稳定的,建议作为用户主键使用[!TIP] 最佳实践:
- 始终在服务器端验证
identityToken- 使用
authorizationCode在服务器端获取refresh token- 首次登录时保存用户姓名和邮箱到服务器
- 定期检查用户的凭证状态
常见问题
Q: 为什么第二次登录时fullName和email是null?
A: Apple只在用户首次授权时返回这些信息。如果需要这些数据,应该在首次登录时保存到服务器。如果需要重新获取,用户需要在系统设置中撤销授权后重新登录。
Q: 如何在开发环境测试?
A: 可以使用iOS模拟器或真机测试。确保设备已登录Apple ID,并在Xcode中配置了正确的证书和Capabilities。
Q: identityToken用来做什么?
A: identityToken是JWT格式的令牌,包含用户身份信息,应该发送到服务器进行验证,确保用户身份的真实性。
更新日志
v1.0.0 (2025-12-18)
- ✨ 初始版本
- ✅ 支持Apple登录
- ✅ 支持获取用户信息
- ✅ 完整的错误处理

收藏人数:
购买源码授权版(
试用
赞赏(0)
下载 38
赞赏 0
下载 11616755
赞赏 1906
赞赏
京公网安备:11010802035340号