This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo ScreenCapture

一个库,可让你保护应用中的屏幕不被捕获或记录。

Android
iOS
Included in Expo Go
Recommended version:
~58.0.5

expo-screen-capture 允许你保护应用中的屏幕不被截屏或录制,并在应用处于前台时被通知是否有人截取了屏幕。你可能想要防止屏幕捕获的两个最常见原因是:

  • 如果屏幕显示敏感信息(密码、信用卡数据等)
  • 你正在显示你不希望被记录和共享的付费内容

这在 Android 上尤其重要,因为 android.media.projection API 允许第三方应用执行屏幕捕捉或屏幕共享(即使应用在后台)。

🌐 This is especially important on Android since the android.media.projection API allows third-party apps to perform screen capture or screen sharing (even if the app is in the background).

在 Android 上,屏幕捕获回调仅在 Android 14 及以上版本无需额外权限即可工作。在 Android 14+ 上,你无需请求或检查用于阻止屏幕捕获或使用回调的权限。

🌐 On Android, the screen capture callback works without additional permissions only for Android 14+. You don't need to request or check permissions for blocking screen capture or using the callback on Android 14+.

如果你想在 Android 13 或更低版本上使用屏幕捕获回调,你需要在 AndroidManifest.xml 文件中添加 READ_MEDIA_IMAGES 权限。你可以在应用配置中使用 android.permissions 键。更多信息请参阅 Android 权限。

🌐 If you want to use the screen capture callback on Android 13 or lower, you need to add the READ_MEDIA_IMAGES permission to your AndroidManifest.xml file. You can use the android.permissions key in your app config. See Android permissions for more information.

安装

🌐 Installation

Terminal
- npx expo install expo-screen-capture
- yarn expo install expo-screen-capture
- pnpm expo install expo-screen-capture
- bun expo install expo-screen-capture

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

用法

🌐 Usage

例子:钩子

🌐 Example: hook

Screen Capture hook
import { usePreventScreenCapture } from 'expo-screen-capture'; import { Text, View } from 'react-native'; export default function ScreenCaptureExample() { usePreventScreenCapture(); return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>As long as this component is mounted, this screen is unrecordable!</Text> </View> ); }

示例:强制阻止屏幕捕捉

🌐 Example: Blocking screen capture imperatively

Blocking screen capture
import * as ScreenCapture from 'expo-screen-capture'; import { useEffect } from 'react'; import { Button, StyleSheet, View } from 'react-native'; export default function ScreenCaptureExample() { const activate = async () => { await ScreenCapture.preventScreenCaptureAsync(); }; const deactivate = async () => { await ScreenCapture.allowScreenCaptureAsync(); }; return ( <View style={styles.container}> <Button title="激活" onPress={activate} /> <Button title="停用" onPress={deactivate} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, });

示例:屏幕捕获回调

🌐 Example: Callback for screen capture

Callback for screen capture
import * as ScreenCapture from 'expo-screen-capture'; import { useEffect } from 'react'; import { Button, StyleSheet, View } from 'react-native'; export default function useScreenCaptureCallback() { // Only use this if you add the READ_MEDIA_IMAGES permission to your AndroidManifest.xml const hasPermissions = async () => { const { status } = await ScreenCapture.requestPermissionsAsync(); return status === 'granted'; }; useEffect(() => { let subscription; const addListenerAsync = async () => { if (await hasPermissions()) { subscription = ScreenCapture.addScreenshotListener(() => { alert('Thanks for screenshotting my beautiful app 😊'); }); } else { console.error('Permissions needed to subscribe to screenshot events are missing!'); } }; addListenerAsync(); return () => { subscription?.remove(); }; }, []); }

应用接口

🌐 API

import * as ScreenCapture from 'expo-screen-capture';

No API data file found, sorry!