在用Uniapp框架做APP,由於APP開發的時候,有微信登錄和QQ登錄,所以還需要開發蘋果授權登錄。
HBuilderX 自 2.4.7+ 版本開始支持 Sign in with Apple (蘋果登錄),蘋果登錄是 iOS13 新增加的功能,當你的應用使用了第三方登錄比如微信登錄,同時也需要集成蘋果登錄,否則提交審核可能會被拒絕。
官方文檔參考:
https://ask.dcloud.net.cn/article/36651
第一步:在HbuilderX中配置apple登錄
在 HBuilderX 打開需要配置 蘋果授權登錄(Sign in with Apple)的項目找到 manifest.json-App SDK配置-登錄鑒權-蘋果登錄(Sign In with Apple) 將其勾選
注意:在進行測試蘋果登錄服務的時候,需要提交雲打包或者提交自定義基座打包,而且只有添加了蘋果登錄服務,提交打包才會將蘋果授權登錄模塊打包進去,否則調不到相關的方法。
第二步:APP登錄按鈕樣式
蘋果授權登錄按鈕樣式參考官方文檔:
https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple/overview/buttons/
第三步:登錄代碼
蘋果授權登錄是ios13才有的,所以需要判斷平台以及ios系統版本
<template> <view class="content"> <!-- 蘋果登錄 --> <view class="sign-in-with-apple" v-if="system >= 13 && platform=='ios'" @click="appleLogin">sign in with apple</view> </view> </template>
script代碼:
export default { data() { return { title: 'Hello', system: '', // 系統版本 platform: '', // 平台 } }, onLoad() { // 先判斷 系統版本 uni.getSystemInfo({ success: (res) => { this.system = res.system this.platform = res.platform },fail: (err) => { },complete: () => { } }) }, methods: { // 蘋果登錄 appleLogin() { // 判斷是 iOS13版本 uni.login({ provider: 'apple', success: (loginRes) => { uni.getUserInfo({ provider: 'apple', success: (userInfoRes) => { }, fail: (err) => { } }) }, fail: (err) => { } }) } } } </script>
授權成功后的回調:
{ "errMsg": "getUserInfo:ok", "rawData": "json字符串", "userInfo": { "openId": "xxx.xxxxx.xxx", // 蘋果用戶唯一標識符,該值在同一個開發者賬號下的所有 App 下是一樣的,開發者可以用該唯一標識符與自己后台系統的賬號體系綁定起來。 "fullName": {}, // 當且僅當第一次授權才會返回 "authorizationCode": "12345678xxx", // 服務器驗證需要使用的參數 "identityToken": "header.payload.signature", // 服務器驗證需要使用的參數 "realUserStatus": 1 // 用於判斷當前登錄的蘋果賬號是否是一個真實用戶 }, "signature": "" }