Spezi Access Guard

Build and Test codecov DOI

允许开发者轻松地使用访问码或生物识别来保护 SwiftUI 视图。

Screenshot showing access guarded to a SwiftUI view by an access code. Screenshot showing access guarded to a SwiftUI view by Face ID with an access code fallback.
4 位数字访问码 带访问码回退的 Face ID

概述

Access Guard 模块允许开发者使用访问码或生物识别来保护 SwiftUI 视图,并允许用户设置或重置其访问码。

更多信息,请参考 API 文档

设置

1. 将 Spezi Access Guard 添加为依赖项

首先,您需要在 Xcode 中的应用Swift 包 中添加 SpeziAccessGuard Swift 包。

2. 注册 Access Guard 模块

重要提示

如果您的应用程序尚未配置为使用 Spezi,请按照 Spezi 设置文章 来设置核心 Spezi 基础架构。

您可以在 SpeziAppDelegate 中配置 AccessGuardModule,如下所示。

访问码

在下面的示例中,我们配置了 AccessGuardModule,其中包含一个使用访问码并由 ExampleIdentifier 标识的访问保护。codeOptions 属性定义了使用的代码类型,在本例中为 4 位数字代码。timeout 属性定义了视图应在场景未处于前台时锁定的时间(以秒为单位)。

import Spezi
import SpeziAccessGuard


class ExampleDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            AccessGuardModule(
                [
                    .code(identifier: "ExampleIdentifier", codeOptions: .fourDigitNumeric, timeout: 15 * 60)
                ]
            )
        }
    }
}

带访问码回退的生物识别

如果用户在其设备上启用了 Face ID 或 Touch ID(有关更多信息,请参阅 Face IDTouch ID),则还可以使用使用 Face ID 或 Touch ID 的访问保护来配置 AccessGuardModule。 如下面的示例所示。如果生物识别不可用或生物识别认证失败,系统将提示用户输入其访问码。

import Spezi
import SpeziAccessGuard


class ExampleDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            AccessGuardModule(
                [
                    .biometric(identifier: "ExampleIdentifier", codeOptions: .fourDigitNumeric, timeout: 15 * 60)
                ]
            )
        }
    }
}

固定代码

也可以使用作为字符串传递的固定代码来配置 AccessGuardModule。 如下面的示例所示。

import Spezi
import SpeziAccessGuard


class ExampleDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            AccessGuardModule(
                [
                    .fixed(identifier: "ExampleIdentifier", code: "1234", codeOptions: .fourDigitNumeric, timeout: 15 * 60)
                ]
            )
        }
    }
}

多个保护

也可以使用不同的机制配置 AccessGuardModule,如下所示。 在此示例中,我们创建了基于生物识别的访问保护和带有固定代码的访问保护,这些访问保护可用于应用程序中的不同视图。 每个访问保护必须具有唯一的标识符。

import Spezi
import SpeziAccessGuard


class ExampleDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            AccessGuardModule(
                [
                    .biometric(identifier: "ExampleIdentifier"),
                    .fixed(identifier: "ExampleFixedIdentifier", code: "1234")
                ]
            )
        }
    }
}

注意

您可以在 Spezi 文档中了解有关 Module 的更多信息

3. 配置目标属性

为了确保您的应用程序具有生物识别所需的权限,请按照以下步骤在 Xcode 项目中配置目标属性

对于使用生物识别的应用程序,此条目是强制性的。 未能提供此条目将导致您的应用程序无法访问这些功能。

示例

设置访问码

使用 SetAccessGuard,我们可以创建一个视图,允许用户设置其访问码。 在可以使用访问保护来保护 SwiftUI 视图之前,必须完成此步骤,但使用固定代码的访问保护除外。(请注意,在设置密码后,访问保护将自动解锁,直到它被锁定或超时。)

import SpeziAccessGuard

struct SetAccessCode: View {
    var body: some View {
        SetAccessGuard(identifier: "ExampleIdentifier")
    }
}

保护对 SwiftUI 视图的访问

现在,我们可以使用 AccessGuarded 视图来使用访问码保护对 SwiftUI 视图的访问。

import SpeziAccessGuard

struct ProtectedContent: View {    
    var body: some View {
        AccessGuarded("ExampleIdentifier") {
            Text("Secured content...")
        }
    }
}

锁定 Access Guard

访问保护将在超时时自动锁定。 但是,我们也可以使用 lock(identifier:) 方法直接锁定访问保护。 在这里,我们添加一个带有按钮的工具栏项目,该按钮将锁定访问保护。

struct ProtectedContent: View {
    @Environment(AccessGuard.self) private var accessGuard
    
    var body: some View {
        AccessGuarded("ExampleIdentifier") {
            Text("Secured content...")
        }
        .toolbar {
            ToolbarItem {
                Button("Lock Access Guard") {
                    try? accessGuard.lock(identifier: "ExampleIdentifier")
                }
            }
        }
    }
}

重置 Access Guard

要从访问保护中删除访问码和所有信息,我们可以使用 resetAccessCode(for:) 方法。 在这里,我们添加一个带有按钮的工具栏项目,该按钮将重置访问保护。

struct ProtectedContent: View {
    @Environment(AccessGuard.self) private var accessGuard
    
    var body: some View {
        AccessGuarded("ExampleIdentifier") {
            Text("Secured content...")
        }
        .toolbar {
            ToolbarItem {
                Button("Reset Access Guard") {
                    try? accessGuard.resetAccessCode(for: "ExampleIdentifier")
                }
            }
        }
    }
}

更多信息,请参考 API 文档

贡献

欢迎对此项目做出贡献。 请务必首先阅读 贡献指南贡献者公约行为准则

许可证

本项目根据 MIT 许可证获得许可。 有关更多信息,请参阅 许可证

Spezi Footer Spezi Footer