# iOS 与 Safari 上的 PWA

> iOS 与 iPadOS 如何把 PWA 添加到主屏幕、选择 Web 应用或书签启动方式、检测运行上下文，并处理推送与存储限制。

iOS 26 和 iPadOS 26 默认把每个添加到主屏幕的网站作为 Web 应用打开；用户可以在添加时关闭 **Open as Web App**，改为添加浏览器书签，即使该网站已配置为 Web 应用。

安装必须由用户手动完成，没有可调用的安装接口。向用户说明添加步骤，并按 WebKit 各版本提供的能力设计应用。

## 工作原理

iOS 16.4（2023-03）允许第三方浏览器把网站添加到主屏幕，并加入了对 manifest `id` 成员的支持。声明式 Web Push 在 iOS 18.4（2025-03）加入。到 iOS 26（2025-09），添加到主屏幕的网站默认"以 Web 应用打开"。

### 添加到主屏幕的手势

Apple 的 iPhone 使用手册 [Turn a website into an app in Safari on iPhone](https://support.apple.com/guide/iphone/open-as-web-app-iphea86e5236/ios) 记载了 Safari 中的路径（英文界面）：点按共享按钮，向下滚动操作列表，点按 **Add to Home Screen**，保持 **Open as Web App** 开启，再点按 **Add**。如果共享面板里没有 **Add to Home Screen**，点按列表底部的 **Edit Actions** 把它加回来。图标只出现在添加它的那台设备上。

自 iOS 与 iPadOS 16.4 起，第三方浏览器也能提供同样的操作，条件见 [WebKit 公告](https://webkit.org/blog/13878/web-push-for-web-apps-on-ios-and-ipados/)：应用持有 `com.apple.developer.web-browser` 受管理权限、共享面板的 `activityItems` 中包含一个正在显示 `http:` 或 `https:` 文档的 `WKWebView`、且设备不是共享 iPad。

### 由什么决定是 Web 应用还是书签

在 iOS 16.4 到 18 上由 manifest 决定：manifest 把 `display` 设为 `standalone` 或 `fullscreen` 的网站，无论由哪个浏览器添加，都以 Web 应用打开；没有这种 manifest（也没有旧式 web-app-capable meta 标签）的网站成为主屏幕书签，在用户的默认浏览器中打开。

iOS 26 与 iPadOS 26 把决定权交给了用户。WebKit 的 [Safari 26.0 说明](https://webkit.org/blog/17333/webkit-features-in-safari-26-0/)写明：默认情况下每个添加到主屏幕的网站都以 Web 应用打开；用户可以在添加时关闭"Open as Web App"改为书签；Safari 中"可安装性"的要求为零。manifest 仍会影响应用外观和启动方式。WebKit 说它带来的好处仍是体验的一部分，其中声明的图标会被使用；Safari 17.0 的说明把显示模式、名称、主题色和起始 URL 列为 manifest 可以定制的内容。

### 图标与身份

iOS 15.4 起会读取 manifest 图标；同时存在 `apple-touch-icon` 链接时，优先使用后者。完全没有图标时，iOS 16.4 及之后会用网站名称首字母配上取自网站的颜色生成一个字母图标。manifest 的 `id` 成员（16.4+）与用户添加时输入的名称组合，用来区分同一网站的多个安装，因此专注模式可以单独静音其中一份，不影响另一份。

### 只在已安装上下文中可用的 API

Web Push、Notifications API 与 Badging API 自 iOS 16.4 起在主屏幕 Web 应用中可用；iOS 版 Safari 标签页不支持这些 API。请求通知权限必须响应用户手势，例如点按订阅按钮；`setAppBadge()` 在权限授予前就会改变存储的计数，待用户允许通知后显示在图标上。声明式 Web Push（iOS 与 iPadOS 18.4、macOS 15.5）增加了 `window.pushManager`，无需 Service Worker 即可显示通知；原有的推送流程仍通过 Service Worker 注册对象订阅。

### 存储

除非源调用 `navigator.storage.persist()` 并且 Safari 批准，浏览器存储的数据都是尽力而为；Safari 根据用户交互历史批准或拒绝请求，不弹提示。开启跨站跟踪预防时，若某个源在最近七天的浏览器使用中没有任何点按或点击，Safari 会删除该源由脚本创建的数据，服务器设置的 Cookie 得以保留（MDN，[Storage quotas and eviction criteria](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)）。自 iOS 17 起，主屏幕 Web 应用采用浏览器应用配额，每个源约占磁盘的 60%；内嵌 `WKWebView` 的配额为 15%。

## 示例

这些示例先检测能力：回退分支可在 Safari 标签页中运行，支持分支则用于主屏幕 Web 应用。

### 优先使用声明式管理器订阅推送

先检查可用的推送类型，再选择 `PushManager`。声明式管理器位于 `window` 上，无需 Service Worker。原有管理器位于 Service Worker 注册对象上，要等 Worker 激活后才能用。

```js
subscribeButton.addEventListener('click', async () => {
  if (!('PushManager' in window) || !('Notification' in window)) {
    // iOS 上的 Safari 标签页，或任何没有推送的浏览器：提供应用内收件箱。
    showInAppInbox();
    return;
  }

  let pushManager;
  if ('pushManager' in window) {
    pushManager = window.pushManager; // 声明式 Web Push，iOS 18.4+
  } else if ('serviceWorker' in navigator) {
    await navigator.serviceWorker.register('/sw.js');
    pushManager = (await navigator.serviceWorker.ready).pushManager;
  } else {
    showInAppInbox();
    return;
  }

  if ((await Notification.requestPermission()) !== 'granted') return;

  await pushManager.subscribe({
    userVisibleOnly: true,
    applicationServerKey: VAPID_PUBLIC_KEY,
  });
});
```

与 Chrome 和 Edge 一样，WebKit 会拒绝没有 `userVisibleOnly: true` 的订阅（[MDN，`PushManager.subscribe()`](https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe)）。声明式路径还要求服务端发送声明式 JSON 载荷，见 [iOS Safari 推送](/zh/reference/notifications/ios-safari-push/)。

### 只在浏览器标签页中显示安装提示

`display-mode` 媒体特性表示当前的呈现方式，不能说明页面如何启动。只在匹配浏览器标签页模式时显示安装提示。

```js
const inBrowserTab =
  typeof window.matchMedia === 'function' &&
  window.matchMedia('(display-mode: browser)').matches;

if (inBrowserTab) {
  // iOS 上没有安装事件：自己解释"共享 > Add to Home Screen"。
  showAddToHomeScreenHint();
} else {
  // 主屏幕 Web 应用、全屏，或无法回答的引擎：不显示提示。
  hideInstallHint();
}
```

页面用 Fullscreen API 进入全屏后，也不再匹配 `browser`。这样会少显示一次提示，但能避免浮层遮住全屏视频。

### 安装接口不存在时检测运行上下文

使用标准的 `display-mode` 媒体特性判断页面处于已安装的显示环境还是浏览器标签页。在 iOS Safari 中，再以非标准的只读布尔属性 `navigator.standalone` 补充判断。Apple 文档用它检查页面是否以 standalone 模式显示，[MDN 则注明它只在 Apple 的 iOS Safari 中可用](https://developer.mozilla.org/en-US/docs/Web/API/Navigator#non-standard_properties)（[Apple 文档归档](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html)）。只有明确匹配浏览器显示模式且没有 `BeforeInstallPromptEvent` 时，才提供手动添加到主屏幕的路径。把代码保存成完整 HTML 文件即可运行，其中已包含状态文字和恢复操作。

```html
<!doctype html>
<meta name="viewport" content="width=device-width">
<p id="install-status"></p>
<button id="install-help" hidden>查看安装步骤</button>
<script>
  const status = document.querySelector('#install-status');
  const help = document.querySelector('#install-help');
  const matchesDisplayMode = (mode) =>
    typeof window.matchMedia === 'function' &&
    window.matchMedia(`(display-mode: ${mode})`).matches;
  const inBrowserTab = matchesDisplayMode('browser');
  const installed =
    navigator.standalone === true ||
    matchesDisplayMode('standalone');
  const hasInstallEvent = 'BeforeInstallPromptEvent' in window;

  if (installed) {
    status.textContent = '正在主屏幕 Web 应用中运行。';
  } else if (hasInstallEvent) {
    status.textContent = '等待浏览器发出安装事件。';
  } else if (inBrowserTab) {
    status.textContent = '打开共享菜单，再选 Add to Home Screen。';
    help.hidden = false;
  } else {
    status.textContent = '无法在此显示模式中判断安装状态。';
  }

  help.addEventListener('click', () => {
    status.textContent = '打开共享菜单，选择 Add to Home Screen。';
  });
</script>
```

`browser` 与 `standalone` 是 `display-mode` 媒体特性的取值，详见 [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/display-mode)。只有处于 `browser` 模式且安装事件接口不存在时，手动回退操作才会显示；全屏或无法识别的显示模式会继续隐藏安装控件，也不会被标记成已安装。

## 实测行为

一项 [2026-10-20 的真机测试](/zh/lab/behaviour/install-apis-on-ios/)在一台运行 iOS 18.6.2（22G100）的 iPhone 16 Pro 上检查了 Safari 标签页和两个主屏幕 Web 应用。三种上下文中的 `'BeforeInstallPromptEvent' in window` 都是 `false`，`typeof navigator.install` 都是 `"undefined"`；演示页也显示 `beforeinstallprompt` 尚未触发。测试只读取接口是否存在，没有测试安装行为，也没有覆盖第三方 iOS 浏览器、iPadOS、iOS 26 或更高版本。原始值收录在 [iOS 18 主屏幕 Web 应用数据包](/zh/research/2026-10-ios18-home-screen-web-apps/data/)中。

:::caution
Apple 的
[Turn a website into an app in Safari on iPhone](https://support.apple.com/guide/iphone/open-as-web-app-iphea86e5236/ios)
要求用户点按"共享"，再点 **Add to Home Screen**，打开 **Open as Web App**，然后点 **Add**。
WebKit 的 [Safari 26.0 功能说明](https://webkit.org/blog/17333/webkit-features-in-safari-26-0/)
指出，在 iOS 26 上添加到主屏幕的每个网站默认都作为 Web 应用打开，用户可以关闭
"Open as Web App"，改为添加浏览器书签，即使该网站已配置为 Web 应用。
:::

用户每次添加都能单独设置开关，所以两个用户添加同一个 URL，可能以不同方式打开。运行时检测 `display-mode`，仅看 manifest 无法确定结果。

## 另请参阅

- [iOS 添加到主屏幕](/zh/reference/installation/ios-add-to-home-screen/)
- [iOS Safari 推送](/zh/reference/notifications/ios-safari-push/)
- [角标](/zh/reference/installation/badging/)
- [存储持久化](/zh/reference/storage/persistence/)
- [macOS 上的 Safari PWA（Dock 应用）](/zh/reference/platforms/macos-safari/)
- [Web Push for Web Apps on iOS and iPadOS](https://webkit.org/blog/13878/web-push-for-web-apps-on-ios-and-ipados/)（webkit.org）
- [Meet Declarative Web Push](https://webkit.org/blog/16535/meet-declarative-web-push/)（webkit.org）
- [WebKit Features in Safari 26.0](https://webkit.org/blog/17333/webkit-features-in-safari-26-0/)（webkit.org）
- [Turn a website into an app in Safari on iPhone](https://support.apple.com/guide/iphone/open-as-web-app-iphea86e5236/ios)（support.apple.com）