Coder Social home page Coder Social logo

permission's Introduction

Travis Status CocoaPods compatible Carthage compatible

Permission exposes a unified API to request permissions on iOS.

UsageExampleInstallationLicense

Usage

Permission

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")
    }
}

Supported permissions:

PermissionType.swift PermissionTypes/

PermissionAlert

PermissionAlert.swift

When you first request a permission, a system alert is presented to the user. If you request a permission that was denied/disabled, a PermissionAlert will be presented. You might want to change the default title, message, cancel and settings text:

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

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

PermissionSet

PermissionSet.swift

Use a PermissionSet to check the status of a group of Permission and to react a permission is requested.

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

A PermissionButton requests the permission when tapped and updates itself when its underlying permission status changes.

let button = PermissionButton(.Photos)

PermissionButton is a subclass of UIButton. All the getters and setters of UIButton have their equivalent in PermissionButton.

button.setTitles([
    .Authorized:    "Authorized",
    .Denied:        "Denied",
    .Disabled:      "Disabled",
    .NotDetermined: "Not determined"
])

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

Example

class PermissionsViewController: UIViewController, PermissionSetDelegate {

    let label = UILabel()

    override func viewDidLoad() {
        let contacts   = PermissionButton(.Contacts)
        let camera     = PermissionButton(.Camera)
        let microphone = PermissionButton(.Microphone)
        let photos     = PermissionButton(.Photos)

        // ...

        let permissionSet = PermissionSet(contacts, camera, microphone, photos)
        
        permissionSet.delegate = self
        
        label.text = String(permissionSet.status)
    }

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

Installation

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Permission into your Xcode project using Carthage, specify it in your Cartfile:

github "delba/Permission"

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

You can install it with the following command:

$ gem install cocoapods

To integrate Permission into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

pod 'Permission'

License

Copyright (c) 2015 Damien (http://delba.io)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

permission's People

Contributors

delba avatar

Watchers

Chew Chit Siang avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.