一个苹果iOSococ

iOS开发常用权限汇总

2019-03-21  本文已影响1人  _小沫

主要总结下以下常用权限的获取及请求授权用法等(均不考虑iOS8以下系统):
网络权限 推送权限 定位权限 通讯录权限 相机权限 相册权限

网络权限

使用系统CoreTelephony库实现网络权限的获取及监听

 func getTelephonyAuthorization() {
        // 获取网络权限状态
        let cellularData = CTCellularData.init()
        switch cellularData.restrictedState {
            case .restricted:
                print("restricted");
            case .notRestricted:
                print("notRestricted");
            case .restrictedStateUnknown:
                print("restrictedStateUnknown");
        }
    }
   Alamofire.request("https://www.baidu.com")

弹出权限提示框

hehe
  1. 封装网络框架,请求失败时,定时重新请求
  2. 提供数据空白页,空白页提供类似“重新加载”的按钮,允许及引导用户手动重新请求;
  3. 监听网络权限的变化,当监听到网络权限更改为允许后,重新请求;
 let cellularData = CTCellularData.init()
 func requestTelephonyAuthorization() {
        // 监听网络权限变化
        cellularData.cellularDataRestrictionDidUpdateNotifier = {state in
            switch state {
                case .restricted:
                    print("restricted");
                case .notRestricted:
                    print("notRestricted");
                case .restrictedStateUnknown:
                    print("restrictedStateUnknown");
            }
        }
    }

详细处理流程,可参考iOS 10 之 网络权限带来的坑

推送权限

推送相关API iOS10更新了,对应的权限API有所不同;

 func getNotificationAuthorization() {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().getNotificationSettings { (settings) in
                switch settings.authorizationStatus.rawValue {
                case 0:
                    print("not authorized")
                default:
                    print("authorized")
                }
            }
        }else {
            guard let settings = UIApplication.shared.currentUserNotificationSettings else {
                print("not settings");
                return;
            }
            switch settings.types.rawValue {
            case 0:
                print("not authorized")
            default:
                print("authorized")
            }
        }
    }
func requestNotificationAuthorization() {
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
                if granted {
                    print("authorized");
                }else {
                    print("not authorized");
                }
            }
        } else {
            let setting = UIUserNotificationSettings.init(types: [.alert,.sound,.badge], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(setting)
        }
    }

定位权限

定位权限有两种情况,一种是针对手机的(全部应用),一种是针对当前应用;两种情况都有对应接口获取权限状态;

 func getLocationAuthorization() {
        // 手机的定位权限
        if !CLLocationManager.locationServicesEnabled() {
            print("disenable")
            return;
        }
        // 应用的定位权限
        let status = CLLocationManager.authorizationStatus()
        switch status {
            case .authorizedAlways:
                print("always")
            case .authorizedWhenInUse:
                print("authorizedWhenInUse")
            case .denied:
                print("denied")
            case .notDetermined:
                print("notDetermined")
            case .restricted:
                print("restricted")
        }
    }
   let manager = CLLocationManager.init()
    func requestLocationAuthorization() {
        manager.delegate = self
        manager.requestAlwaysAuthorization()
        manager.requestWhenInUseAuthorization()
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .authorizedAlways:
            print("always")
        case .authorizedWhenInUse:
            print("authorizedWhenInUse")
        case .denied:
            print("denied")
        case .notDetermined:
            print("notDetermined")
        case .restricted:
            print("restricted")
        }
    }
key

定位权限描述的key有:
NSLocationAlwaysAndWhenInUseUsageDescription,
NSLocationWhenInUseUsageDescription

通讯录权限

通讯录相关API iOS9更新了,对应的权限API有所不同;

func getContactAuthorization() {
        if #available(iOS 9.0, *) {
            let status = CNContactStore.authorizationStatus(for: .contacts)
            // ...
        }else {
            let status = ABAddressBookGetAuthorizationStatus()
            // ...
        }
    }
    func requestContactAuthorization() {
        if #available(iOS 9.0, *) {
            let contact = CNContactStore.init()
            contact.requestAccess(for: .contacts) { (granted, error) in
                // ...
            }
        }else {
            let addressBook = ABAddressBookCreateWithOptions(nil, nil)
            ABAddressBookRequestAccessWithCompletion(addressBook as ABAddressBook) { (granted, error) in
                // ...
            }
        }
    }

相机权限

    func getVideoAuthorization() {
        let videoAuthorStatus = AVCaptureDevice.authorizationStatus(for: .video)
        switch videoAuthorStatus {
            case .authorized:
                print("authorized");
            case .denied:
                print("denied");
            case .notDetermined:
                print("notDetermined");
            case .restricted:
                print("restricted");
        }
    }
    func requestVideoAuthorization() {
        AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted) in
            if granted {
                print("authorized");
            }else {
                print("not authorized");
            }
        })
    }

相册权限

访问系统相册,保存图片至手机相册均需要该权限;

    func getPhotoAuthorization() {
        let photoAuthorStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthorStatus {
            case .authorized:
                print("authorized");
            case .denied:
                print("denied");
            case .notDetermined:
                print("notDetermined");
            case .restricted:
                print("restricted");
            }
    }
    func requestPhotoAuthorization() {
        PHPhotoLibrary.requestAuthorization { (status) in
            if status == PHAuthorizationStatus.authorized {
                print("authorized");
            }else {
                print("not authorized");
            }
        }
    }

开发中使用场景

// iOS8及以上系统
UIApplication.shared.openURL(NSURL.init(string: UIApplication.openSettingsURLString) as! URL)
  1. 开启定时器,每间隔一段时间重新获取权限状态;当权限状态为允许时,再做相关处理;
  2. 当用户切换至系统设置界面再回到当前应用时,当前应用状态肯定会经过BecomeActive这个状态,那么我们可以添加UIApplication.didBecomeActiveNotification通知监听这个状态,做相应处理;
NotificationCenter.default.addObserver(self, selector: #selector(becomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)

以上权限中,通讯录权限、相册权限、相机权限当用户切换至系统设置界面更改后,应用将被kill

上一篇下一篇

猜你喜欢

热点阅读