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 登录功能的库。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.2

expo-apple-authentication 为 iOS 提供苹果认证。目前尚不支持 Android 或网页。

任何包含第三方身份验证选项的应用必须提供 Apple 身份验证选项,以符合 App Store 审核指南。如需更多信息,请参阅 使用 Apple 登录 网站上的 Apple 身份验证。

🌐 Any app that includes third-party authentication options must provide Apple authentication as an option to comply with App Store Review guidelines. For more information, see Apple authentication on the Sign In with Apple website.

安装

🌐 Installation

Terminal
- npx expo install expo-apple-authentication
- yarn expo install expo-apple-authentication
- pnpm expo install expo-apple-authentication
- bun expo install expo-apple-authentication

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),你可以使用其内置的 配置插件 来配置 expo-apple-authentication。该插件允许你配置一些无法在运行时设置的属性,这些属性需要构建新的应用二进制文件才能生效。如果你的应用不使用 CNG,那么你需要手动配置该库。

🌐 You can configure expo-apple-authentication using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

设置 iOS 项目

🌐 Setup iOS project

要在你的应用中启用 使用 Apple 登录 功能,请在项目的应用配置中将 ios.usesAppleSignIn 属性设置为 true:

🌐 To enable the Sign In with Apple capability in your app, set the ios.usesAppleSignIn property to true in your project's app config:

app.json
{ "expo": { "ios": { "usesAppleSignIn": true } } }

Example app.json with config plugin

在本地运行 EAS Build 将使用 iOS 功能签名 在构建之前启用所需的功能。

🌐 Running EAS Build locally will use iOS capabilities signing to enable the required capabilities before building.

app.json
{ "expo": { "plugins": ["expo-apple-authentication"] } }
Are you using this library in an existing React Native app?

未使用 EAS Build 的应用必须为其应用标识符 手动配置 Apple 登录 功能。

🌐 Apps that don't use EAS Build must manually configure the Apple Sign In capability for their bundle identifier.

如果你通过 Apple 开发者控制台 启用了 Apple 登录 功能,请确保在你的 ios/[app]/[app].entitlements 文件中添加以下权限:

🌐 If you enable the Apple Sign In capability through the Apple Developer Console, then be sure to add the following entitlements in your ios/[app]/[app].entitlements file:

<key>com.apple.developer.applesignin</key> <array> <string>Default</string> </array>

另外,在你的 ios/[app]/Info.plist 中将 CFBundleAllowMixedLocalizations 设置为 true,以确保登录按钮使用设备的语言环境。

🌐 Also, set CFBundleAllowMixedLocalizations to true in your ios/[app]/Info.plist to ensure the sign-in button uses the device locale.

用法

🌐 Usage

Apple Authentication usage
import * as AppleAuthentication from 'expo-apple-authentication'; import { View, StyleSheet } from 'react-native'; export default function App() { return ( <View style={styles.container}> <AppleAuthentication.AppleAuthenticationButton buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN} buttonStyle={AppleAuthentication.AppleAuthenticationButtonStyle.BLACK} cornerRadius={5} style={styles.button} onPress={async () => { try { const credential = await AppleAuthentication.signInAsync({ requestedScopes: [ AppleAuthentication.AppleAuthenticationScope.FULL_NAME, AppleAuthentication.AppleAuthenticationScope.EMAIL, ], }); // signed in } catch (e) { if (e.code === 'ERR_REQUEST_CANCELED') { // handle that the user canceled the sign-in flow } else { // handle other errors } } }} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, button: { width: 200, height: 44, }, });

开发与测试

🌐 Development and testing

你可以在 iOS 上的 Expo Go 中测试这个库,而无需遵循上面的任何说明。不过,如果你使用 EAS Build,你需要添加配置插件才能使用这个库。当你登录 Expo Go 时,你收到的标识符和数值可能与在独立应用中收到的不同。

🌐 You can test this library in Expo Go on iOS without following any of the instructions above. However, you'll need to add the config plugin to use this library if you are using EAS Build. When you sign into Expo Go, the identifiers and values you receive will likely be different than what you'll receive in standalone apps.

你可以在 iOS 模拟器上对该库进行有限的测试。然而,并非所有方法的行为都与在设备上相同,因此我们强烈建议在开发时尽可能在真实设备上进行测试。

🌐 You can do limited testing of this library on the iOS Simulator. However, not all methods will behave the same as on a device, so we highly recommend testing on a real device when possible while developing.

正在核实苹果的回复

🌐 Verifying the response from Apple

苹果的响应包括一个包含用户信息的已签名 JWT。为了确保响应确实来自苹果,你可以使用苹果的公钥对签名进行加密验证,该公钥发布在 https://appleid.apple.com/auth/keys。该过程并不限于 Expo。

🌐 Apple's response includes a signed JWT with information about the user. To ensure that the response came from Apple, you can cryptographically verify the signature with Apple's public key, which is published at https://appleid.apple.com/auth/keys. This process is not specific to Expo.

应用接口

🌐 API

import * as AppleAuthentication from 'expo-apple-authentication';

No API data file found, sorry!

错误代码

🌐 Error codes

大多数错误代码与官方 Apple 授权错误 相符。

🌐 Most of the error codes match the official Apple Authorization errors.

代码描述
ERR_INVALID_OPERATION执行了无效的授权操作。
ERR_INVALID_RESPONSE授权请求收到了无效的响应。
ERR_INVALID_SCOPE传入了无效的 AppleAuthenticationScope。
ERR_REQUEST_CANCELED用户取消了授权尝试。
ERR_REQUEST_FAILED授权尝试失败。请参阅错误信息以获取更多信息。
ERR_REQUEST_NOT_HANDLED授权请求未被正确处理。
ERR_REQUEST_NOT_INTERACTIVE授权请求不是交互式的。
ERR_REQUEST_UNKNOWN授权尝试因未知原因失败。