This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo AppleAuthentication
一个为 iOS 提供“使用 Apple 登录”功能的库。
expo-apple-authentication 为 iOS 提供 Apple 身份验证。它目前尚不支持 Android 或 web。
任何包含第三方身份验证选项的应用必须提供 Apple 身份验证作为选项,以符合 App Store 审核指南。有关更多信息,请参阅 Sign In with Apple 网站上的 Apple 身份验证。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
app 配置中的配置
如果你在项目中使用配置插件(Continuous Native Generation (CNG)),可以使用 expo-apple-authentication 内置的 配置插件 来进行配置。该插件允许你配置各种无法在运行时设置、且需要重新构建新的应用二进制文件才会生效的属性。如果你的应用不使用 CNG,那么你需要手动配置该库。
设置 iOS 项目
要在你的应用中启用 使用 Apple 登录 功能,请在项目的 app 配置中将 ios.usesAppleSignIn 属性设置为 true:
Example app.json with config plugin
在本地运行 EAS Build 时,将使用 iOS 能力签名 在构建前启用所需的能力。
Are you using this library in an existing React Native app?
不使用 EAS Build 的应用必须为其 bundle 标识符手动配置 Apple 登录能力。
如果你通过 Apple Developer Console 启用了 Apple 登录能力,那么请务必在你的 ios/[app]/[app].entitlements 文件中添加以下 entitlement:
<key>com.apple.developer.applesignin</key> <array> <string>Default</string> </array>
另外,请在你的 ios/[app]/Info.plist 中将 CFBundleAllowMixedLocalizations 设置为 true,以确保登录按钮使用设备语言区域。
用法
开发和测试
你可以在 iOS 上的 Expo Go 中测试这个库,而无需遵循上面的任何说明。 不过,如果你正在使用 EAS Build,则需要添加配置插件才能使用这个库。 当你登录 Expo Go 时,你接收到的标识符和值很可能与在独立应用中接收到的不同。
你可以在 iOS 模拟器上对这个库进行有限测试。不过,并非所有方法的表现都会与设备上一致, 因此我们强烈建议在开发过程中尽可能在真实设备上进行测试。
验证来自 Apple 的响应
Apple 的响应包含一个带有用户信息的签名 JWT。为了确保该响应来自 Apple,你可以使用 Apple 的公钥对签名进行加密验证,该公钥发布在 https://appleid.apple.com/auth/keys。此过程并不特定于 Expo。
API
import * as AppleAuthentication from 'expo-apple-authentication';
Component
Type: React.Element<AppleAuthenticationButtonProps>
This component displays Apple's proprietary "Sign In with Apple" / "Continue with Apple" button
using the native ASAuthorizationAppleIDButton.
Apple's Human Interface Guidelines
allow custom buttons as long as they follow Apple's branding rules (approved title, official
logo asset, approved colors, proportions, and minimum size). Using this component is the easiest
way to meet those requirements, since the system button is automatically Apple-approved,
localized, and accessible.
You should only attempt to render this if AppleAuthentication.isAvailableAsync()
resolves to true. This component will render nothing if it is not available, and you will get
a warning in development mode (__DEV__ === true).
The properties of this component extend from View; however, you should not attempt to set
backgroundColor or borderRadius with the style property. This will not work and is against
the App Store Guidelines. Instead, you should use the buttonStyle property to choose one of the
predefined color styles and the cornerRadius property to change the border radius of the
button.
Make sure to attach height and width via the style props, since without them the button will not appear on the screen.
See: Apple Documentation for more details.
The Apple-defined color scheme to use to display the button.
The type of button text to display ("Sign In with Apple" vs. "Continue with Apple").
numberThe border radius to use when rendering the button. This works similarly to
style.borderRadius in other Views.
() => voidThe method to call when the user presses the button. You should call AppleAuthentication.signInAsync
in here.
The custom style to apply to the button. Should not include backgroundColor or borderRadius
properties.
Methods
Creates a locale-aware string representation of a person's name from an object representing the tokenized portions of a user's full name
stringA locale-aware string representation of a person's name
Queries the current state of a user credential, to determine if it is still valid or if it has been revoked.
Note: This method must be tested on a real device. On the iOS simulator it always throws an error.
A promise that fulfills with an AppleAuthenticationCredentialState
value depending on the state of the credential.
Determine if the current device's operating system supports Apple authentication.
Promise<boolean>A promise that fulfills with true if the system supports Apple authentication, and false otherwise.
An operation that refreshes the logged-in user’s credentials. Calling this method will show the sign in modal before actually refreshing the user credentials.
Promise<AppleAuthenticationCredential>A promise that fulfills with an AppleAuthenticationCredential
object after a successful authentication, and rejects with ERR_REQUEST_CANCELED if the user cancels the
refresh operation.
Sends a request to the operating system to initiate the Apple authentication flow, which will present a modal to the user over your app and allow them to sign in.
You can request access to the user's full name and email address in this method, which allows you to personalize your UI for signed in users. However, users can deny access to either or both of these options at runtime.
Additionally, you will only receive Apple Authentication Credentials the first time users sign
into your app, so you must store it for later use. It's best to store this information either
server-side, or using expo-secure-store, so that the data persists across app installs.
You can use AppleAuthenticationCredential.user to identify
the user, since this remains the same for apps released by the same developer.
Promise<AppleAuthenticationCredential>A promise that fulfills with an AppleAuthenticationCredential
object after a successful authentication, and rejects with ERR_REQUEST_CANCELED if the user cancels the
sign-in operation.
An operation that ends the authenticated session. Calling this method will show the sign in modal before actually signing the user out.
It is not recommended to use this method to sign out the user as it works counterintuitively.
Instead of using this method it is recommended to simply clear all the user's data collected
from using signInAsync or refreshAsync methods.
Promise<AppleAuthenticationCredential>A promise that fulfills with an AppleAuthenticationCredential
object after a successful authentication, and rejects with ERR_REQUEST_CANCELED if the user cancels the
sign-out operation.
Event subscriptions
Interfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
The object type returned from a successful call to AppleAuthentication.signInAsync(),
AppleAuthentication.refreshAsync(), or AppleAuthentication.signOutAsync()
which contains all of the pertinent user and credential information.
See: Apple Documentation for more details.
An object representing the tokenized portions of the user's full name. Any of all of the fields
may be null. Only applicable fields that the user has allowed your app to access will be nonnull.
Literal type: string
A value to specify the style for formatting a name.
See: Apple Documentation for more details.
Acceptable values are: 'default' | 'short' | 'medium' | 'long' | 'abbreviated'
The options you can supply when making a call to AppleAuthentication.refreshAsync().
You must include the ID string of the user whose credentials you'd like to refresh.
See: Apple Documentation for more details.
The options you can supply when making a call to AppleAuthentication.signInAsync().
None of these options are required.
See: Apple Documentation for more details.
The options you can supply when making a call to AppleAuthentication.signOutAsync().
You must include the ID string of the user to sign out.
See: Apple Documentation for more details.
Enums
An enum whose values control which pre-defined color scheme to use when rendering an AppleAuthenticationButton.
AppleAuthenticationButtonStyle.WHITE_OUTLINE = 1White button with a black outline and black text.
An enum whose values control which pre-defined text to use when rendering an AppleAuthenticationButton.
An enum whose values specify state of the credential when checked with AppleAuthentication.getCredentialStateAsync().
See: Apple Documentation for more details.
AppleAuthenticationOperation.IMPLICIT = 0An operation that depends on the particular kind of credential provider.
An enum whose values specify scopes you can request when calling AppleAuthentication.signInAsync().
Note that it is possible that you will not be granted all of the scopes which you request. You will still need to handle null values for any fields you request.
See: Apple Documentation for more details.
An enum whose values specify the system's best guess for how likely the current user is a real person.
See: Apple Documentation for more details.
AppleAuthenticationUserDetectionStatus.UNSUPPORTED = 0The system does not support this determination and there is no data.
AppleAuthenticationUserDetectionStatus.UNKNOWN = 1The system has not determined whether the user might be a real person.
错误代码
大多数错误代码与官方的 Apple 授权错误 相匹配。