@generac/react-native-local-network-permission

中文文档
1.3.0发布于 2026/06/02MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 1.4万

A React Native / Expo module to request permission to access local network on iOS

原始使用文档
文档目录7 节
  1. Getting started
  2. Swift Package Manager support
  3. iOS manual configuration
  4. Usage
  5. Jest mocking
  6. Commit messages
  7. See also

React Native Local Network Permission

A library to handle the local network permission introduced with iOS 14. It is only relevant for iOS, not for Android or web.

Getting started

Add this library to your project:

npm install @generac/react-native-local-network-permission

yarn add @generac/react-native-local-network-permission

Swift Package Manager support

CocoaPods (via the bundled .podspec) is still the primary install path and works for every React Native and Expo app today.

This module also ships SPM scaffolding for the migration described in React Native RFC 0994 (CocoaPods deprecation, December 2026):

  • spm.config.json — consumed by Expo's et prebuild pipeline (EXPO_USE_PRECOMPILED_MODULES=1) to generate XCFrameworks. Picked up automatically by Expo's autolinking; no extra setup.
  • Package.swift — a forward-compatible manifest. The library target declares its dependency on ExpoModulesCore by name; the consumer's autolinking pipeline (RN CLI's SPM autolinking once it lands, or a hand-written brownfield manifest) is responsible for providing .package(path: ".../node_modules/expo-modules-core"). As a result, this Package.swift does not resolve standalone via "Add Package Dependencies…" in Xcode today — that workflow becomes available once the React Native SPM autolinking proposal lands.

iOS manual configuration

NSLocalNetworkUsageDescription

You need to add the NSLocalNetworkUsageDescription key to your Info.plist file. This should be a description of why your app needs Local Network permissions:

<key>NSLocalNetworkUsageDescription</key>
<string>App requires local network access to do ...</string>

See also: Apple documentation

NSBonjourServices

You need to add the NSBonjourServices key to your Info.plist file. This should be an array of Bonjour services that your app uses:

<key>NSBonjourServices</key>
<array>
  <string>_bonjour._tcp</string>
  <string>_lnp._tcp.</string>
</array>

See also: Apple documentation

Expo

If you are using the Expo managed workflow, you cannot directly edit your Info.plist. Instead, you can add the following to your app.json or app.config.js:

expo: {
  ios: {
    infoPlist: {
      NSLocalNetworkUsageDescription: 'App requires local network access to do ...',
      NSBonjourServices: ['_bonjour._tcp', '_lnp._tcp.'],
    },
  },
}

Usage

This library can be used in both React Native and Expo projects. Note however:

import {
  checkLocalNetworkAccess,
  requestLocalNetworkAccess,
} from '@generac/react-native-local-network-permission';

//you need to make sure the local network permission dialog popup once
const result = await requestLocalNetworkAccess();

//then use checkLocalNetworkAccess when you need
const result = await checkLocalNetworkAccess();
//here run the code depends local network access

Jest mocking

If you need to mock this library in your Jest tests, you can use the following code in your testSetup.ts file which you need to reference in your jest.config.js under setupFilesAfterEnv:

jest.mock('@generac/react-native-local-network-permission', () => ({
    checkLocalNetworkAccess: jest.fn(),
    requestLocalNetworkAccess: jest.fn(),
  }),
);

Commit messages

This repository uses a commit message convention which is enforced by a pre-commit hook. If you execute npm run commit instead of git commit -m "some message" you will be guided through the steps to write an acceptable commit message.

It is important that you do this properly, because the semantic-release automation depends on the commit messages to be starting with the proper keyword to determine if a major, minor or patch semantic version should be assigned.

See also

版本与发布记录

@generac/react-native-local-network-permission 共发布 4 个版本,首次发布于 2023/12/19;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.0latest2026/06/025 个月前
1.2.02024/06/272 年前
1.1.02024/02/062 年前
1.0.02023/12/192 年前

关于 @generac/react-native-local-network-permission 的常见问题

@generac/react-native-local-network-permission 是什么?

A React Native / Expo module to request permission to access local network on iOS

如何安装 @generac/react-native-local-network-permission?

在项目目录运行 npm install @generac/react-native-local-network-permission,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:expo(*)、react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@generac/react-native-local-network-permission 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@generac/react-native-local-network-permission 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@generac/react-native-local-network-permission 可以商用吗?

@generac/react-native-local-network-permission 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@generac/react-native-local-network-permission 的下载量有多少?

根据 npm 官方下载统计,@generac/react-native-local-network-permission 在 2026/10/16 – 2026/11/14 这 30 天内被下载 13,552 次,日均约 452 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@generac/react-native-local-network-permission 还在维护吗?

最新版本 1.3.0 发布于 2026/06/02(5 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/12/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。