PermissionScope的使用方法

作者: 残无殇 | 来源:发表于2017-07-06 17:33 被阅读91次

PermissionScope是一个请求权限的封装库,可以很方便的请求系统的权限

GitHub地址:https://github.com/nickoneill/PermissionScope

基本使用方法:

/// cocoapods导入
pod "PermissionScope"

/// 添加头文件
import PermissionScope

/// 初始化权限管理对象
let PermissionScopeManager = PermissionScope.init(backgroundTapCancels: true)

/// 设置弹出提示框的底层视图控制器
PermissionScopeManager.viewControllerForAlerts = self

/// 请求通知权限
PermissionScopeManager.requestNotifications()

/// 监控通知权限,一旦用户点击允许或者拒绝,都会执行以下方法(此处应该吐槽以下,得手写block,不能自动提示)
PermissionScopeManager.onAuthChange = { (finished: Bool, results) in  
    print("权限被改变")
}

/// 获取当前的发送通知的权限状态
let status = PermissionScopeManager.statusNotifications()

/// 执行此方法,如果要获取的权限被拒绝,会弹出提示框,告诉用户权限被拒绝,点击确认按钮会打开系统的权限设置
PermissionScopeManager.onDisabledOrDenied = { result in
    print("onDisabledOrDenied:权限被拒绝")
}

以下为练习写的基本测试方法


import UIKit
import PermissionScope

class PermissionScopeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        /// 初始化权限管理对象
        PermissionScopeManager = PermissionScope.init(backgroundTapCancels: true)
        PermissionScopeManager.viewControllerForAlerts = self
        
        /// 执行此方法,如果要获取的权限被拒绝,会弹出提示框,告诉用户权限被拒绝,点击确认按钮会打开系统的权限设置
        PermissionScopeManager.onDisabledOrDenied = { result in
            print("onDisabledOrDenied:权限被拒绝")
        }
    }
    
    /// 权限管理的对象
    private var PermissionScopeManager: PermissionScope!
    
    /// 请求访问日历的权限
    @IBAction func requestEvents(_ sender: UIButton) {
        
        PermissionScopeManager.requestEvents()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印日历访问的权限
            print(self.PermissionScopeManager.statusEvents())
        }
    }
    
    /// 请求定位权限,一直使用定位
    @IBAction func requestLocationAlways(_ sender: UIButton) {
        
        PermissionScopeManager.requestLocationAlways()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印一直使用定位的权限
            print(self.PermissionScopeManager.statusLocationAlways())
        }
    }
    
    /// 请求发送通知的权限
    @IBAction func requestNotifications(_ sender: UIButton) {
        
        PermissionScopeManager.requestNotifications()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印发送通知的权限
            print(self.PermissionScopeManager.statusNotifications())
        }
    }
    
    /// 请求定位权限,临时使用
    @IBAction func requestLocationInUse(_ sender: UIButton) {
        
        PermissionScopeManager.requestLocationInUse()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印临时使用定位的权限
            print(self.PermissionScopeManager.statusLocationInUse())
        }
    }
    
    /// 请求麦克风权限
    @IBAction func requestMicrophone(_ sender: UIButton) {
        
        PermissionScopeManager.requestMicrophone()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印麦克风的权限
            print(self.PermissionScopeManager.statusMicrophone())
        }
    }
    
    /// 请求访问提醒事项的权限
    @IBAction func requestReminders(_ sender: UIButton) {
        
        PermissionScopeManager.requestReminders()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印访问提醒事项的权限
            print(self.PermissionScopeManager.statusReminders())
        }
    }
    
    /// 请求使用蓝牙的权限
    @IBAction func requestBluetooth(_ sender: UIButton) {
        
        PermissionScopeManager.requestBluetooth()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印使用蓝牙的权限
            print(self.PermissionScopeManager.statusBluetooth())
        }
    }
    
    /// 请求访问通讯录的权限
    @IBAction func requestContacts(_ sender: UIButton) {
        
        PermissionScopeManager.requestContacts()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印访问通讯录的权限
            print(self.PermissionScopeManager.statusContacts())
        }
    }
    
    /// 请求访问照片的权限
    @IBAction func requestPhotos(_ sender: UIButton) {
        
        PermissionScopeManager.requestPhotos()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印访问照片的权限
            print(self.PermissionScopeManager.statusPhotos())
        }
    }
    
    /// 请求使用相机的权限
    @IBAction func requestCamera(_ sender: UIButton) {
        
        PermissionScopeManager.requestCamera()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印使用相机的权限
            print(self.PermissionScopeManager.statusCamera())
        }
    }
    
    /// 请求访问活动与体能训练记录的权限
    @IBAction func requestMotion(_ sender: UIButton) {
        
        PermissionScopeManager.requestMotion()
        PermissionScopeManager.onAuthChange = { (finished: Bool, results) in
            /// 权限请求完成会执行此处代码
            /// 此处为打印访问活动与体能训练记录的权限
            print(self.PermissionScopeManager.statusMotion())
        }
    }
}

总结:
PermissionScope让我们请求权限更简单了。
初始化一个对象之后,要是请求权限的话,直接调用request开头的方法,如果要是获取权限的状态,直接调用status开头的方法,如果要监控权限请求的结果,可以使用

PermissionScopeManager.onAuthChange = { (finished: Bool, results) in  }

所有的请求方法执行之后,用户的操作都会回调这个方法;
如果想在用户拒绝权限后提示用户开启权限,可以使用

PermissionScopeManager.onDisabledOrDenied = { result in  }

这个方法会自动弹出一个提示框,告诉用户权限被拒绝,需要自己去设置开启;提示框有两个按钮,一个是取消,一个是确定,如果点击了确定,就会打开系统设置,进入APP的权限设置界面;

结束:
PermissionScope的基本使用并不复杂,如果有定制化需求的话,比如提示框要自定义,就需要自己去研究了,本文只是介绍简单的使用方法,另外,千万记得请求权限之前,在info.plist文件中对该权限所属的字段进行设置,否则会崩掉的哦

相关文章

网友评论

  • 布袋的世界:这个是重点,记得在// FIXME: on PermissionScope -> PermissionScope.swift
    using weak var by apiapia on 07/08/2017
    weak var weakSelf = self
    viewControllerForAlerts = weakSelf
    残无殇:@布袋的世界 要求不高的话还是可以的,毕竟,一般的APP 对于这些地方,并没有太多的要求。而且,基本上使用的并不多。还是可以满足的
    布袋的世界:@残无殇 不过,这个PermissonScope第三方,感觉好多的BUG!觉得很多没有完善!实在是心累!
    残无殇:@布袋的世界 受教了

本文标题:PermissionScope的使用方法

本文链接:https://www.haomeiwen.com/subject/nwzhhxtx.html