@nativescript/apple-sign-in
使用 Apple 登录
npm i --save @nativescript/apple-sign-in

@nativescript/apple-sign-in

ns plugin add @nativescript/apple-sign-in

需求

访问 Apple 开发者网站 并创建一个新的应用标识符,确保已启用“使用 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>

用法

API

isSupported

由于“使用 Apple 登录”是在 iOS 13 中添加的,因此在您的应用程序中显示“使用 Apple 登录”按钮之前,请确保调用此函数。在 iOS < 13 和 Android 上,这将返回 false

import { SignIn } from "@nativescript/apple-sign-in";

const supported: boolean = SignIn.isSupported();

signInWithApple

现在您知道“使用 Apple 登录”在该设备上受支持,您可以允许用户自行登录(例如在按下漂亮的按钮之后)。

import { SignIn, User } from "@nativescript/apple-sign-in";

signIn(
{
// by default you don't get these details, but if you provide these scopes you will (and the user will get to choose which ones are allowed)
scopes: ["EMAIL", "FULLNAME"]
})
.then((result: User) => {
console.log("Signed in, user: " + result);
console.log("Signed in, familyName: " + result.fullName.familyName);
// you can remember the user to check the sign in state later (see '
getSignInWithAppleState' below)
this.user = result.user;
})
.catch(err => console.log("Error signing in: " + err));

getState

⚠️ 这似乎在模拟器上不起作用!

如果您想了解用户的当前登录状态,可以将您之前获得的 user(id) 传递。

import { SignIn } from "@nativescript/apple-sign-in";

const user: string = "the id you got back from the signIn function";

SignIn.getState(user)
.then(state => console.log("Sign in state: " + state))
.catch(err => console.log("Error getting sign in state: " + err));

许可证

Apache 许可证第 2 版