Reclaim InApp SDK

此 SDK 允许您将 Reclaim 的应用内验证流程集成到您的 SwiftUI 应用程序中。

前提条件

示例

安装

1. 将 Reclaim InApp SDK 添加到您的 Xcode 项目

方法 1:当使用 Xcode 项目时(推荐)

复制以下 URL 并使用它将 Swift Package Manager (SPM) 依赖项添加到 Xcode 项目中

https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git
如何将 Swift Package Manager (SPM) 依赖项添加到您的 Xcode 项目
  1. 在 Xcode 编辑器的工具栏中,点击 File > Add Package Dependency...

Add a dependency to your Xcode project

2. 在“Add Package Dependency”对话框中,将 URL `https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git` 粘贴到对话框右上角的“Search or Enter package URL”字段中。

Add a dependency to your Xcode project

3. 点击“Add Package”。4. 在另一个窗口中,在“Add to Targets”部分选择您的**应用目标**,然后点击“Add package”。

Add a dependency to your Xcode project

这样就完成了 Reclaim InApp SDK 的安装。现在按照以下步骤将必要的框架链接到您的 Xcode 项目

注意

方法 2:当使用带有 CocoaPods 的 Xcode Workspace 项目时

  1. 确保在您的 Podfile 中为您的项目定义一个全局平台,版本为 13.0 或更高版本。
platform :ios, '13.0'
  1. 将以下内容添加到您的 Podfile 以覆盖 ReclaimInAppSdk 的默认版本(可选)
pod 'ReclaimInAppSdk', :git => 'https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git', :tag => '0.1.4'
pod 'ReclaimInAppSdk', :git => 'https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git'
pod 'ReclaimInAppSdk', :git => 'https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git', :commit => '184d41628026768feb703dc7bb9a3d913c6b271e'
pod 'ReclaimInAppSdk', :git => 'https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git', :branch => 'main'
  1. 添加依赖项后,您的 podfile 可能如下所示
platform :ios, '13.0'

target 'GitPodSwiftUIExample' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for GitPodSwiftUIExample
  pod 'ReclaimInAppSdk', :git => 'https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git', :commit => '184d41628026768feb703dc7bb9a3d913c6b271e'

  target 'GitPodSwiftUIExampleTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'GitPodSwiftUIExampleUITests' do
    # Pods for testing
  end

end
  1. 运行 pod install
  2. 打开 *.xcworkspace 文件以处理项目。并构建项目。
  3. 您的构建可能会因如下错误而失败

Xcode build error due to CocoaPods

  1. 要解决此问题,请在 Xcode 中打开 *.xcworkspace 文件。在您目标的“Build Settings”下的“Build Options”部分,将“User Script Sandboxing”设置为“No”,然后再次构建项目。

Xcode build error due to CocoaPods

  1. 您的项目应该成功构建。

有关更多信息,请查看Reclaim Example - SwiftUI with CocoaPods,了解如何在结合 CocoaPods 的 SwiftUI 应用程序中使用 SDK 的完整示例。

方法 3:当使用 Swift Package Manager 清单时

选择一个包版本

.package(url: "https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git", from: "0.1.2")

或选择主分支

.package(url: "https://github.com/reclaimprotocol/reclaim-inapp-ios-sdk.git", branch: "main")
选择一个产品
.product(name: "ReclaimInAppSdk", package: "ReclaimInAppSdk")

构建项目。

  1. 如果您有 *.xcworkspace 文件,请在 Xcode 中打开 YourApp.xcworkspace。验证您打开的是 MyApp.xcworkspace 而不是 MyApp.xcodeproj。.xcworkspace 文件包含 CocoaPod 依赖项,而 .xcodeproj 文件不包含。如果您没有 *.xcworkspace 文件,请在 Xcode 中打开 YourApp.xcodeproj
  2. 选择 Product > Build 或按 Cmd + B

2. 修复性能问题

当您在物理设备上运行调试可执行文件时,您的应用性能将受到严重影响。修复此问题需要在您的 Xcode 项目 xcscheme 中进行简单的更改。

方法 1:更新 XCScheme 的环境变量(推荐)

  1. 在 Xcode 中打开您的项目。
  2. 点击项目目标。
  3. 点击 Scheme 下拉菜单。

Edit current xcscheme in Xcode

  1. 点击 Edit Scheme 按钮。
  2. 点击 Run 选项卡。
  3. 点击 Arguments 选项卡,并选中 Environment Variables 部分。

Enable Debug executable in Xcode

  1. 添加以下环境变量
    • 键:GODEBUG
    • 值:asyncpreemptoff=1
  2. 点击对话框中的 Close 按钮并构建项目。
  3. 在物理设备上运行应用。

方法 2:禁用“Debug executable”

此方法不推荐,但如果您不想将环境变量添加到 xcscheme,则可能很有用。

  1. 在 Xcode 中打开您的项目。
  2. 点击项目目标。
  3. 点击 Scheme 下拉菜单。

Edit current xcscheme in Xcode

  1. 点击 Edit Scheme 按钮。
  2. 点击 Run 选项卡。
  3. 取消选中 Debug executable 复选框。

Enable Debug executable in Xcode

4. 在您的项目中使用 ReclaimInAppSdk

要在您的项目中使用 ReclaimInAppSdk,请按照以下步骤操作

  1. 将 ReclaimInAppSdk 模块导入到您的 Swift 文件中。
import ReclaimInAppSdk
  1. 创建一个请求对象。
let request = ReclaimVerification.Request.params(
    try .init(
        /// You can use the appId and secret from Reclaim Devtools to create a request.
        /// Providing appId and secret here in this initializer is optional. 
        /// If you don't provide it, the SDK will use the appId and secret from the Info.plist file.
        // appId: "YOUR_APP_ID_FROM_RECLAIM_DEVTOOLS",
        // secret: "YOUR_APP_SECRET_FROM_RECLAIM_DEVTOOLS",
        /// This is the provider id that you've added to your app in Reclaim Devtools.
        /// The verification flow will use the provider information fetch by this provider id.
        providerId: providerId
    )
)

ReclaimVerification.Request.* API 中提供了更多创建请求对象的方法。

  1. 启动验证流程。
// This is the function that starts the verification flow.
// This may fail if device screen is getting shared.
let result = try await ReclaimVerification.startVerification(request)

返回的结果是一个 ReclaimVerification.Response 对象。如果验证成功,此对象包含一个带有 proofs、exception 和 sessionId 的响应。

如果验证被取消或失败,结果将包含一个 exception。

您可以使用 ReclaimVerification.Response 对象来获取 proof、exception 和 sessionId。

let proof = result.response.proofs
let exception = result.response.exception
let sessionId = result.response.sessionId

如果验证成功,proof 将包含您需要存储在数据库中的数据。

如果验证被取消或失败,exception 将包含错误详细信息。

在上述 API 中,ReclaimVerification.Request.*ReclaimVerification.startVerificationReclaimVerification.startVerificationFromUrl 也可能抛出错误。错误可能是以下之一

您可以使用 do-catch 块来处理错误。例如

do {
    let result = try await ReclaimVerification.startVerification(request)
} catch ReclaimVerificationError.cancelled {
    print("Verification cancelled")
} catch ReclaimVerificationError.dismissed {
    print("Verification dismissed")
}

有关完整示例,请参阅Reclaim Example - SwiftUI

覆盖 | 高级用法

您可以使用 ReclaimOverrides 通过 ReclaimVerification.setOverrides 自定义验证流程。

func setOverrides() {
    Task { @MainActor in
        do {
            try await ReclaimVerification.setOverrides(
                appInfo: ReclaimOverrides.ReclaimAppInfo(
                    appName: "Overriden Example",
                    appImageUrl: "https://placehold.co/400x400/png"
                )
                // Add other overrides here
            )
        } catch {
            print("unexpected failure error details: \(error)")
            showAlert(message: "Could not set overrides")
        }
    }
}

升级

要升级到新版本的 Reclaim InApp SDK,请按照以下步骤操作

  1. 确保您拥有最新版本的 Reclaim InApp SDK。当前最新版本为 0.1.4
  2. 如果遇到任何问题:删除包,清除构建,重启 Xcode,然后重新添加包。

当发布具有 API 更改的新版本时,将提供迁移指南。