wata / PermissionAccess

A unified API to ask for permissions on iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PermissionAccess

Version

A unified API to ask for permissions on iOS.

Supported Permissions

  • Bluetooth
  • Camera
  • Contacts
  • Events
  • LocationAlways
  • LocationWhenInUse
  • Microphone
  • Motion
  • Notifications
  • PhotoLibrary
  • Reminders
  • Siri
  • SpeechRecognition
  • MediaLibrary

Usage

PermissionAccess.request(.contacts, presentDeniedAlert: true) { (isAuthorized) in
    print(isAuthorized)
}

// Returns a authorization status of authorized / notDetermined / denied / disabled.
PermissionAccess.status(.contacts)

// Returns a Boolean value that indicates whether the permission is granted.
PermissionAccess.hasPermisson(.contacts)

locationPermissionStatusChanged notification

A notification posted when the status of the location permission have changed.

let observer = NotificationCenter.default.addObserver(forName: .locationPermissionStatusChanged, object: nil, queue: .main) { (_) in
    print(PermissionAccess.hasPermission(.locationAlways))
}

PermissionAccess.request(.locationAlways)

Custom permission group with PromiseKit

import PromiseKit

extension PermissionAccess {
    static var myMovieCameraStatus: PermissionStatus {
        let statuses = [status(.camera), status(.microphone), status(.photoLibrary)]
        if statuses.elementsEqual([.authorized, .authorized, .authorized]) {
            return .authorized
        }
        if statuses.contains(.denied) {
            return .denied
        }
        return .notDetermined
    }

    static var hasMyMovieCameraPermission: Bool {
        return myMovieCameraStatus.isAuthorized
    }

    static func requestMyMovieCamera(presentDeniedAlert: Bool = false, handler: PermissionHandler? = nil) {
        let error = NSError(domain: "", code: 0, userInfo: nil)
        firstly {
            return Promise { (seal) in
                request(.camera, presentDeniedAlert: false) { (isAuthorized) in
                    isAuthorized ? seal.fulfill(()) : seal.reject(error)
                }
            }
        }.then {
            return Promise { (seal) in
                request(.microphone, presentDeniedAlert: false) { (isAuthorized) in
                    isAuthorized ? seal.fulfill(()) : seal.reject(error)
                }
            }
        }.then {
            return Promise { (seal) in
                request(.photoLibrary, presentDeniedAlert: false) { (isAuthorized) in
                    isAuthorized ? seal.fulfill(()) : seal.reject(error)
                }
            }
        }.done {
            handler?(true)
        }.catch { (_) in
            if presentDeniedAlert {
                presentDeniedAlertController(permissionName: "", title: "My title", message: "My message", handler: handler)
            } else {
                handler?(false)
            }
        }
    }
}

About

A unified API to ask for permissions on iOS.

License:MIT License


Languages

Language:Swift 89.6%Language:Ruby 8.7%Language:Objective-C 1.7%