npm i --save nativescript-apple-sign-in
- 版本:2.0.0
- GitHub: https://github.com/EddyVerbruggen/nativescript-apple-sign-in
- NPM: https://npmjs.net.cn/package/nativescript-apple-sign-in
- 下载量
- 昨日:12
- 上周:98
- 上个月:288
使用Apple ID登录,适用于NativeScript
插件版本2+与NativeScript 7+兼容。如果您使用的是较旧的NativeScript版本,请使用插件版本1.1.0。
要求
- 访问苹果开发者网站,并创建一个新的应用程序标识符,并启用“使用Apple ID登录”功能。请确保使用该应用程序标识符为您的应用程序签名,并使用配置文件。
- 打开应用程序的
App_Resources/iOS
文件夹,并将以下内容添加到名为app.entitlements
的文件中。
安装
tns plugin add nativescript-apple-sign-in
示例应用程序
如果您遇到问题或想快速了解其工作原理,请查看示例应用程序
git clone https://github.com/EddyVerbruggen/nativescript-apple-sign-in
cd nativescript-apple-sign-in/src
npm run demo.ios
API
isSignInWithAppleSupported
使用Apple ID登录自iOS 13开始提供,请确保在应用程序中显示“使用Apple ID登录”按钮之前调用此函数。在iOS < 13和Android上,此函数将返回false
。
import { isSignInWithAppleSupported } from "nativescript-apple-sign-in";
const supported: boolean = isSignInWithAppleSupported();
signInWithApple
既然您知道该设备支持“使用Apple ID登录”,您可以让用户自行登录(例如,在点击了按钮后)。
import { signInWithApple, SignInWithAppleAuthorization } from "nativescript-apple-sign-in";
signInWithApple(
{
// 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: SignInWithAppleAuthorization) => {
console.log("Signed in, credential: " + result.credential);
console.log("Signed in, familyName: " + result.credential.fullName.familyName);
// you can remember the user to check the sign in state later (see 'getSignInWithAppleState' below)
this.user = result.credential.user;
})
.catch(err => console.log("Error signing in: " + err));
getSignInWithAppleState
⚠️ 此功能似乎在模拟器上不起作用!
如果您想了解用户的当前登录状态,可以将之前获得的user
(id)传递。
import { getSignInWithAppleState } from "nativescript-apple-sign-in";
const user: string = "the id you got back from the signInWithApple function";
getSignInWithAppleState(user)
.then(state => console.log("Sign in state: " + state))
.catch(err => console.log("Error getting sign in state: " + err));