PermissionWizard 1.4.0

PermissionWizard 1.4.0

Sergey Moskvin 维护。



  • Sergei Moskvin

🔮PermissionWizard

CocoaPods Carthage

用俄语阅读

它是系统权限管理的终极工具。您再也不必理解每种新权限类型的系统API或在其上搜索Stack Overflow了。😄

优势

📱支持的最新功能
🖥
配合使用效果极佳

支持所有现有的权限类型
🛡通过验证您的plist密钥提供无崩溃体验
📬即使默认系统API没有提供,您也可以使用async/awaitcompletion blocks
🛣通过指定要调用完成块的期望调度队列,您可以忘记thread management(可选)

🚀完全用编写
🍭统一您的代码,不论您处理的是哪种权限类型
🖼包括用于您的UI的native iconslocalized strings(可选)
🍕模块化,仅添加您需要的部分到项目中
🪁不包含任何冗余内容

支持类型

要求

  • iOS 12 / macOS 10.15 Catalina
  • Xcode 14
  • Swift 5.5

安装

CocoaPods

要将 PermissionWizard 集成到您的 Xcode 项目中,将其添加到您的 Podfile

pod 'PermissionWizard'

默认情况下,库将完全安装。

由于苹果关于系统权限的政策,您的应用程序可能因提及未实际使用的 API 而被拒绝。建议仅安装您需要的组件。在这种情况下 您将不会遇到任何问题⚠️

pod 'PermissionWizard/Assets' # Icons and localized strings
pod 'PermissionWizard/Bluetooth'
pod 'PermissionWizard/Calendars'
pod 'PermissionWizard/Camera'
pod 'PermissionWizard/Contacts'
pod 'PermissionWizard/FaceID'
pod 'PermissionWizard/Health'
pod 'PermissionWizard/Home'
pod 'PermissionWizard/LocalNetwork'
pod 'PermissionWizard/Location'
pod 'PermissionWizard/Microphone'
pod 'PermissionWizard/Motion'
pod 'PermissionWizard/Music'
pod 'PermissionWizard/Notifications'
pod 'PermissionWizard/Photos'
pod 'PermissionWizard/Reminders'
pod 'PermissionWizard/Siri'
pod 'PermissionWizard/SpeechRecognition'
pod 'PermissionWizard/Tracking'

如果您安装的是单独的组件,则不要指定 pod 'PermissionWizard'

Carthage

要将 PermissionWizard 集成到您的 Xcode 项目中,将其添加到您的 Cartfile

github "debug45/PermissionWizard"

默认情况下,当您构建项目时,库将完全编译。

由于苹果关于系统权限的政策,您的应用程序可能因提及未实际使用的 API 而被拒绝。建议仅启用您需要的组件。在这种情况下 您将不会遇到任何问题⚠️

要仅启用您需要的组件,在项目的根目录中创建 PermissionWizard.xcconfig 文件。根据以下模板将适当的设置放入文件中

ENABLED_FEATURES = ASSETS BLUETOOTH CALENDARS CAMERA CONTACTS FACE_ID HEALTH HOME LOCAL_NETWORK LOCATION MICROPHONE MOTION MUSIC NOTIFICATIONS PHOTOS REMINDERS SIRI SPEECH_RECOGNITION TRACKING
SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) $(ENABLED_FEATURES) CUSTOM_SETTINGS

根据以下模板,删除不需要组件名称的模板的第一行。

如何使用

使用 PermissionWizard 非常简单!

import PermissionWizard

if useSwiftConcurrency {
    await Permission.contacts.checkStatus() // .notDetermined
} else {
    Permission.contacts.checkStatus { status in
        status // .notDetermined
    }
}

do {
    if useSwiftConcurrency {
        try await Permission.location.requestAccess(whenInUseOnly: true) // (value: .whenInUseOnly, isAccuracyReducing: false)
    } else {
        try Permission.location.requestAccess(whenInUseOnly: true) { status in
            status.value // .whenInUseOnly
            status.isAccuracyReducing // false
        }
    }
    
    if useSwiftConcurrency {
        await Permission.camera.checkStatus(withMicrophone: true) // (camera: .granted, microphone: .denied)
    } else {
        Permission.camera.checkStatus(withMicrophone: true) { status in
            status.camera // .granted
            status.microphone // .denied
        }
    }
} catch let error {
    error.userInfo["message"] // You must add a row with the “NSLocationWhenInUseUsageDescription” key to your app‘s plist file and specify the reason why you are requesting access to location. This information will be displayed to a user.
    
    guard let error = error as? Permission.Error else {
        return
    }
    
    error.type // .missingPlistKey
}

一些权限类型支持额外的功能。例如,如果一个 iOS 14 用户仅允许以低精度访问其位置,您可以请求临时访问完整精度。

if useSwiftConcurrency {
    try? await Permission.location.requestTemporaryPreciseAccess(purposePlistKey: "Default") // true
} else {
    try? Permission.location.requestTemporaryPreciseAccess(purposePlistKey: "Default") { result in
        result // true
    }
}

遗憾的是,默认系统 API 对处理某些权限类型的支持有限。例如,您只能通过请求来检查家庭权限的当前状态。

INFO.PList

对于您正在使用的每种权限类型,Apple 要求您将对应的字符串添加到您的 INFO.PList 中,以便描述您的访问请求目的。 权限向导可以帮助您找到必要的PList键的名称

Permission.faceID.usageDescriptionPlistKey // NSFaceIDUsageDescription

Permission.health.readingUsageDescriptionPlistKey // NSHealthUpdateUsageDescription
Permission.health.writingUsageDescriptionPlistKey // NSHealthShareUsageDescription

如果您使用默认系统API请求某些权限但忘记编辑您的INFO.PList,则应用会崩溃。然而,使用 权限向导就不会出现崩溃——只是使用了try

线程管理

在某些情况下,默认系统API可能会在不同的串行队列中返回结果。为了避免崩溃,并且不使用DispatchQueue.main.async,您可以请求权限向导在首选队列中调用完成块

Permission.tracking.checkStatus { _ in
    // Default queue
}

Permission.tracking.checkStatus(completion: { _ in
    // DispatchQueue.main
}, forcedInvokationQueue: .main)

UI 资产

如果您的UI需要权限类型图标或本地化名称,您可以使用权限向导轻松获取它

let permission = Permission.speechRecognition.self

imageView.image = permission.getIcon(squircle: true)
label.text = permission.getLocalizedName() // Speech Recognition

请注意,只有当Assets组件安装(CocoaPods)或启用(Carthage)时,图标和本地化字符串才可用。权限向导支持所有系统语言

已知问题

  • 无法使用async/await请求数据本地网络和位置权限
  • 蓝牙权限在模拟器上总是返回.granted
  • 本地网络权限在模拟器上不适用
  • 音乐权限在iOS 12的模拟器上不适用

路线图

  • 扩展对macOS的支持(特定权限类型,原生图标)
  • 使库兼容Swift Package Manager
  • 支持在SwiftUI代码中更方便的使用

结论

您可以在TelegramLinkedIn联系我。如果您发现任何问题,请告知

库遵循MIT许可协议。权限类型的图标和本地化字符串属于Apple,其使用受公司规则约束。

如果您觉得PermissionWizard对您有帮助,请星标此仓库。感谢!👍