---
title: "在 iOS SDK 中实现 Web 付费墙"
description: "设置 Web 付费墙，无需支付 App Store 费用和审核即可收款。"
---

:::important
在开始之前，请确保您已[在控制台中配置了 Web 付费墙](web-paywall)，并安装了 Adapty SDK 3.6.1 或更高版本。
:::

## 打开 Web 付费墙 \{#open-web-paywalls\}

如果您使用的是自行开发的付费墙，需要通过 SDK 方法来处理 Web 付费墙。`.openWebPaywall` 方法会：
1. 生成一个唯一 URL，让 Adapty 能够将展示给特定用户的付费墙与其跳转到的网页关联起来。
2. 跟踪用户返回应用的时机，并以短时间间隔调用 `.getProfile`，以判断用户画像的访问权限是否已更新。
这样，一旦支付成功并且访问权限完成更新，订阅几乎立即就会在应用中激活。

```swift showLineNumbers title="Swift"
do {
    try await Adapty.openWebPaywall(for: product)
} catch {
    print("Failed to open web paywall: \(error)")
}
```
:::note
`openWebPaywall` 方法有两个版本：
1. `openWebPaywall(product)` 根据付费墙生成 URL，并将产品数据附加到 URL 中。
2. `openWebPaywall(paywall)` 根据付费墙生成 URL，但不附加产品数据。当 Adapty 付费墙中的产品与 Web 付费墙中的产品不一致时，请使用此版本。
:::
## 处理错误 \{#handle-errors\}
| 错误 | 描述 | 建议操作 |
|-----------------------------------------|--------------------------------------------------------|---------------------------------------------------------------------------|
| AdaptyError.paywallWithoutPurchaseUrl | 付费墙未配置网页购买 URL | 检查付费墙是否已在 Adapty 看板中正确配置 |
| AdaptyError.productWithoutPurchaseUrl | 产品没有网页购买 URL | 在 Adapty 看板中验证产品配置 |
| AdaptyError.failedOpeningWebPaywallUrl | 无法在浏览器中打开该 URL | 检查设备设置，或提供其他购买方式 |
| AdaptyError.failedDecodingWebPaywallUrl | 无法正确编码 URL 中的参数 | 验证 URL 参数是否有效且格式正确 |
## 实现示例 \{#implementation-example\}

```swift showLineNumbers title="Swift"
class SubscriptionViewController: UIViewController {
    var paywall: AdaptyPaywall?
    
    @IBAction func purchaseButtonTapped(_ sender: UIButton) {
        guard let paywall = paywall, let product = paywall.products.first else { return }
         Task {
            await offerWebPurchase(for: product)
         }
    }
    
    func offerWebPurchase(for paywallProduct: AdaptyPaywallProduct) async {
        do {
            // Attempt to open web paywall
            try await Adapty.openWebPaywall(for: paywallProduct)
        } catch let error as AdaptyError {
            
            switch error {
            case .paywallWithoutPurchaseUrl, .productWithoutPurchaseUrl:
                showAlert(message: "Web purchase is not available for this product.")
            case .failedOpeningWebPaywallUrl:
                showAlert(message: "Could not open web browser. Please try again.")
            default:
                showAlert(message: "An error occurred: \(error.localizedDescription)")
            }
        } catch {
            showAlert(message: "An unexpected error occurred.")
        }
    }
    
    // Helper methods
    private func showAlert(message: String) { /* ... */ }
}
```

:::note
用户返回应用后，请刷新 UI 以反映用户画像的更新。`AdaptyDelegate` 将接收并处理用户画像更新事件。
:::

## 在应用内浏览器中打开网页付费墙 \{#open-web-paywalls-in-an-in-app-browser\}

:::important
从 Adapty SDK v3.15 起，支持在应用内浏览器中打开网页付费墙。
:::

默认情况下，网页付费墙会在外部浏览器中打开。

为了提供流畅的用户体验，你可以在应用内浏览器中打开网页付费墙。这样，网页购买页面会直接在你的应用内显示，用户无需切换应用即可完成交易。

要启用此功能，请将 `in` 参数设置为 `.inAppBrowser`：
```swift showLineNumbers title="Swift"
do {
    try await Adapty.openWebPaywall(for: product, in: .inAppBrowser) // default – .externalBrowser
} catch {
    print("Failed to open web paywall: \(error)")
}
```