MyCujoo-Permission 3.0.3

MyCujoo-Permission 3.0.3

Atimca 维护。



  • Damien

CocoaPods compatible Carthage compatible

Permission 提供了一个统一直接到 iOS 权限请求的 API。

使用示例安装许可

使用

权限

Permission.swift PermissionStatus.swift

let permission: Permission = .contacts

print(permission.status) // PermissionStatus.NotDetermined

permission.request { status in
    switch status {
    case .authorized:    print("authorized")
    case .denied:        print("denied")
    case .disabled:      print("disabled")
    case .notDetermined: print("not determined")
    }
}
支持的权限

PermissionType.swift PermissionTypes/

权限警报

PermissionAlert.swift

被拒绝和禁用警报

当您首次请求权限时,系统会向用户展示一个警报。如果您请求了一个被拒绝/禁用的权限,则会展示一个 PermissionAlert。您可能需要更改默认的 titlecancelsettings 文本。

let alert = permission.deniedAlert // or permission.disabledAlert

alert.title    = "Please allow access to your contacts"
alert.message  = nil
alert.cancel   = "Cancel"
alert.settings = "Settings"

如果您不想展示这些警报,请将 permission.presentDeniedAlert 设置为 falsepermission.presentDisabledAlert 设置为 false

请求前的提醒

为了避免浪费展示系统警报的唯一机会,您可以展示一个 请求前的提醒。更多信息请参阅这篇文章:http://techcrunch.com/2014/04/04/the-right-way-to-ask-users-for-ios-permissions/

permission.presentPrePermissionAlert = true

let alert = permission.prePermissionAlert

alert.title   = "Let Foo Access Photos?"
alert.message = "This lets you choose which photos you want to add to your Foo profile"
alert.cancel  = "Not now"
alert.confirm = "Give Access"

只有在用户点击“授权访问”时,才会展示系统警报。

PermissionSet

PermissionSet.swift

使用 PermissionSet 来检查一组 Permission 的状态,并在请求权限时做出响应。

let permissionSet = PermissionSet(.contacts, .camera, .microphone, .photos)
permissionSet.delegate = self

print(permissionSet.status) // PermissionStatus.NotDetermined

// ...

func permissionSet(permissionSet: PermissionSet, willRequestPermission permission: Permission) {
    print("Will request \(permission)")
}

func permissionSet(permissionSet: PermissionSet, didRequestPermission permission: Permission) {
    switch permissionSet.status {
    case .authorized:    print("all the permissions are granted")
    case .denied:        print("at least one permission is denied")
    case .disabled:      print("at least one permission is disabled")
    case .notDetermined: print("at least one permission is not determined")
    }
}

PermissionButton

PermissionButton

当点击时会请求权限,并且在底层权限状态变化时更新自己的 PermissionButton

let button = PermissionButton(.photos)

PermissionButtonUIButton 的子类。所有 UIButton 的获取器和设置器在 PermissionButton 中都有对应。

button.setTitles([
    .authorized:    "Authorized",
    .denied:        "Denied",
    .disabled:      "Disabled",
    .notDetermined: "Not determined"
])

// button.setAttributedTitles
// button.setTitleColors
// button.setTitleShadowColors
// button.setImages
// button.setBackgroundImages
// etc.

第三方库

示例

class PermissionsViewController: UIViewController, PermissionSetDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let label = UILabel()

        let contacts   = PermissionButton(.contacts)
        let camera     = PermissionButton(.camera)
        let microphone = PermissionButton(.microphone)
        let photos     = PermissionButton(.photos)

        contacts.setTitles([
            .notDetermined: "Contacts - NotDetermined"
            .authorized:    "Contacts - Authorized",
            .denied:        "Contacts - Denied"
        ])

        contacts.setTitleColors([
            .notDetermined: .black,
            .authorized:    .green,
            .denied:        .red
        ])

        // ...

        let permissionSet = PermissionSet(contacts, camera, microphone, photos)

        permissionSet.delegate = self

        label.text = String(permissionSet.status)

        for subview in [label, contacts, camera, microphone, photos] {
            view.addSubview(subview)
        }
    }

    func permissionSet(permissionSet: PermissionSet, didRequestPermission permission: Permission) {
        label.text = String(permissionSet.status)
    }
}

安装

Carthage

Carthage 是一个去中心化的依赖关系管理工具,它可以自动将框架添加到您的 Cocoa 应用程序中。

您可以使用以下命令使用 Homebrew 安装 Carthage:

$ brew update
$ brew install carthage

要使用 Carthage 将 Permission 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定:

github "MyCujoo/Permission"
配置

由于苹果公司关于权限访问的新政策,二进制文件可能会因为被认为在没有使用密钥的情况下尝试访问隐私敏感数据而被拒绝,并且如果没有实际请求权限,还会进一步被拒绝。

作为解决方案,您可以在构建动态框架之前提供自定义构建标志,以便仅编译您请求的权限。这通过在项目的根目录中添加一个名为 PermissionConfiguration.xcconfig 的配置文件来完成。为了方便,您可以在 Permission/ 仓库目录中使用 PermissionConfiguration.xcconfig。只需注释掉您希望使用的权限,然后编译框架即可。

要仅编译带有通知和照片权限的框架

PERMISSION_ADDRESS_BOOK      = // PERMISSION_ADDRESS_BOOK
PERMISSION_BLUETOOTH         = // PERMISSION_BLUETOOTH
PERMISSION_CAMERA            = PERMISSION_CAMERA
PERMISSION_CONTACTS          = // PERMISSION_CONTACTS
PERMISSION_EVENTS            = // PERMISSION_EVENTS
PERMISSION_LOCATION          = // PERMISSION_LOCATION
PERMISSION_MICROPHONE        = // PERMISSION_MICROPHONE
PERMISSION_MOTION            = // PERMISSION_MOTION
PERMISSION_NOTIFICATIONS     = PERMISSION_NOTIFICATIONS
PERMISSION_PHOTOS            = // PERMISSION_PHOTOS
PERMISSION_REMINDERS         = // PERMISSION_REMINDERS
PERMISSION_SPEECH_RECOGNIZER = // PERMISSION_SPEECH_RECOGNIZER
PERMISSION_MEDIA_LIBRARY     = // PERMISSION_MEDIA_LIBRARY

// Do not modify this line. Instead, remove comments above as needed to enable the categories your app uses.
PERMISSION_FLAGS= $(PERMISSION_ADDRESS_BOOK) $(PERMISSION_BLUETOOTH) $(PERMISSION_CAMERA) $(PERMISSION_CONTACTS) $(PERMISSION_EVENTS) $(PERMISSION_LOCATION) $(PERMISSION_MICROPHONE) $(PERMISSION_MOTION) $(PERMISSION_NOTIFICATIONS) $(PERMISSION_PHOTOS) $(PERMISSION_REMINDERS) $(PERMISSION_SPEECH_RECOGNIZER) $(PERMISSION_MEDIA_LIBRARY)

SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) $(PERMISSION_FLAGS)

CocoaPods

CocoaPods 是用于 Cocoa 项目的依赖关系管理工具。

您可以使用以下命令安装它:

$ gem install cocoapods

要使用 CocoaPods 将权限集成到您的 Xcode 项目中,请在 Podfile 中指定它。由于苹果关于权限访问的新政策,您需要特别定义要使用子规范访问哪种类型的权限。例如,如果您想访问相机和通知,请定义以下内容

use_frameworks!

pod 'MyCujoo-Permission/Camera'
pod 'MyCujoo-Permission/Notifications'

请参阅 MyCujoo-Permission.podspec 了解哪些子规范可用。

此仓库是原始项目 https://github.com/delba/Permission 的分支。

许可

Permission 遵循 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。