插件
Apple 登录
@nativescript/apple-sign-in
一个允许您使用 Apple 登录 认证用户的插件。
安装
npm install @nativescript/apple-sign-in
要求
访问 Apple 开发者网站 并创建一个新的应用标识符,并启用
Apple 登录
功能。确保您使用该应用标识符签署您的应用。将 Apple 登录权利文件 添加到
App_Resources/iOS/app.entitlements
。
然后将 Apple 登录权利文件 添加到 App_Resources/iOS/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.applesignin</key>
<array>
<string>Default</string>
</array>
</dict>
</plist>
使用 @nativescript/apple-sign-in
检查是否支持 Apple 登录
Apple 登录 添加在 iOS 13
中。因此,要检查设备是否支持它,请在 SignIn
上调用静态 isSupported()
方法。
在 iOS < 13
和 Android 上,isSupported()
返回 false
。
import { SignIn } from '@nativescript/apple-sign-in'
const supported: boolean = SignIn.isSupported()
Apple 登录
如果设备支持 Apple 登录,您可以向用户展示登录选项。
SignIn.signIn({
scopes: ['EMAIL', 'FULLNAME'],
})
.then((result: User) => {
console.log('Signed in, user: ' + result)
console.log('Signed in, familyName: ' + result.fullName.familyName)
this.user = result.user
})
.catch((err) => console.log('Error signing in: ' + err))
获取用户的登录状态
要获取用户的当前登录状态,请调用 getState(),将从 signIn() 方法获得的用户 ID(User.user
) 传递给它。
import { SignIn } from '@nativescript/apple-sign-in'
const user: string = User.user
SignIn.getState(user)
.then((state) => console.log('Sign in state: ' + state))
.catch((err) => console.log('Error getting sign in state: ' + err))
API
isSupported
isSupported: boolean = SignIn.isSupported()
检查设备是否支持 Apple 登录。对于 iOS 13+ 返回 true
,对于 iOS < 13
和 Android 返回 false
。
signIn()
SignIn.signIn(
options: SignInOptions
)
.then((result: User) => {
// handle the signed-in user data
})
.catch(err =>{
// handle error
});
使用指定的 SignInOptions 对象登录用户。
getState()
SignIn.getState(userID:string)
.then(state =>{
// do something with user status
})
.catch(err =>{
// handle error
});
获取用户的当前登录状态。
SignInOptions
名称 | 类型 | 描述 |
---|---|---|
user | string | 有关描述,请访问 user。 |
scopes | SignInScopes[] | 可选:默认情况下,signIn() 不会返回任何用户的范围。要返回感兴趣的范围,请将它们列在 scopes 数组中。 |
useOnce | boolean | |
nonce | string | 可选:有关详细信息,请参阅 nonce。 |
SignInScopes
type SignInScopes = 'EMAIL' | 'FULL_NAME'
有关详细信息,请访问 ASAuthorizationScope。
用户对象
以下是 signIn() 方法返回的用户对象的属性。
名称 | 类型 | 描述 |
---|---|---|
nonce | string | 可选:有关详细信息,请参阅 nonce。 |
user | string | 始终提供 |
fullName | UserFullName | 可选 |
realUserStatus | UserDetectionStatus | 可选 |
authorizedScopes | SignInScopes | 可选 |
identityToken | string | 可选 |
email | string | 可选 |
identityToken | string | 始终提供 |
state | string | 始终提供 |
authorizationCode | string | 可选 |
有关详细信息,请访问 ASAuthorizationAppleIDCredential。
UserFullName 接口
名称 | 类型 | 描述 |
---|---|---|
namePrefix | string | 可选 |
givenName | string | 可选 |
middleName | string | 可选 |
familyName | string | 可选 |
nameSuffix | string | 可选 |
nickname | string | 可选 |
有关详细信息,请访问 NSPersonNameComponents。
CredentialState 枚举
enum CredentialState {
Revoked = 'Revoked',
Authorized = 'Authorized',
NotFound = 'NotFound',
Transferred = 'Transferred',
}
有关详细信息,请访问 ASAuthorizationAppleIDProviderCredentialState。
UserDetectionStatus 枚举
enum UserDetectionStatus {
Unsupported = 'Unsupported',
Unknown = 'Unknown',
LikelyReal = 'LikelyReal',
}
有关详细信息,请访问 ASUserDetectionStatus。
许可证
Apache 许可证 2.0 版