Skip to content

123sunxiaolin/JLAuthorizationManager-Swift

Repository files navigation

JLAuthorizationManagerSwift

Build Status Version License Platform

中文文档

Objective-C 版本 请戳这

🔑 JLAuthorizationManagerSwift is a simple, lightWeight, easy-to-use, complete, thread-safety permission lib for iOS developers to use.

Features

  • Full coverage, currently supports access to Photos, Camera, CellularNetwork, Microphone, Calendar, Reminder, Notification, Location, AppleMusic, SpeechRecognizer, Siri, Bluetooth etc.
  • Easy-to-use, unified interface and separated singe permission.Avoid submitting App Store rejected issues.
  • Async fetch authorized permission and callback always in main thread.
  • multiple ways to use and flexible use.

Getting Started

Prerequisites

  • Apps using JLAuthorizationManagerSwift can target: iOS 8.0 or later.
  • Xcode 8.0 or later required.

Installation

  • Via Cocoapods
    1.InstallCocoapods;
    2.Run pod repo update to make CocoaPods aware of the latest available JLAuthorizationManagerSwift versions.
    3.Due to Podfile 's target, add corresponding pod commands as follows,then run pod install.In the end, use the .xcworkspace file generated by CocoaPods to work on your project.
// pod 'JLAuthorizationManagerSwift/All' is equivatent to
pod 'JLAuthorizationManagerSwift' 
Or
pod 'JLAuthorizationManagerSwift/AuthorizationManager'
Or
pod 'JLAuthorizationManagerSwift/Camera'
Or
pod 'JLAuthorizationManagerSwift/Microphone'
...

Tutorials

Usage of JLAuthorizationManager

  • 1.Via shared singleton method to use.
  • 2.Use uniform interface:
// Geneeral
public func requestPermission(_ type: PermissionType,
                                  completion: @escaping PermissionCompletion)
                                 
 // Health Data
  public func requestHealthPermission(_ typesToShare: Set<HKSampleType>,
                                        typesToRead: Set<HKObjectType>,
                                        completion: @escaping PermissionCompletion)
                                 

Usage of single permission - xxxPermission:

    1. Every single permission class inherits from base classBasePermisssion, implementing a unified interface protocol Permission:
public protocol Permission where Self: BasePermission {
    
    /// Permission type
    var type: PermissionType { get }
    
    /// Current authorized status
    func authorizedStatus() -> AuthorizedStatus
    
    /// Request current permission
    func requestPermission(_ completion: @escaping AuthorizedCompletion)
    
}

  • Basic usage(take request photos permission as an example):
let permission = PhotosPermission()
print("\(type.title) -> status:\(permission.authorizedStatus())")
permission.requestPermission { granted in
	print(granted ? "Authorized -> \(type.title)" : "unAuthorized -> \(type.title)")
}
  • Use singleton method shared to call, when requesting Location and Bluetooth permission. Take requesting Location permission as an excample:
let permission = LocationAlwaysPermission.shared
print("\(type.title) -> status:\(permission.authorizedStatus())")
permission.requestPermission { granted in
	print(granted ? "Authorized -> \(type.title)" : "unAuthorized -> \(type.title)")
}
  • Health Data request:
let permission = HealthPermission(typesToShare, typesToRead: typesToRead)
print("\(type.title) -> status:\(permission.authorizedStatus())")
permission.requestPermission { granted in
	print(granted ? "Authorized -> \(type.title)" : "unAuthorized -> \(type.title)")
}

For more infomation about useags, please refer to DEMO.

Tips and Tricks

  • Don't forget add authorization Description in info.plist.
  • if you want to use HealthKit or Siri,please open switch on Capabilities,then system create xx..entitlements file automatically.
  • if project is not submited to App store, you can use the unified permission manager file JLAuthorizationManagerSwift; if you need to submit project to the App Store, you need to add the corresponding permission request file according to the business needs, otherwise, the App Store will be rejected due to the addition of useless permissions.

Update Note

  • v1.0.4 (2019-2-12):Fix bugs.
  • v1.0.3 (2019-1-18):Update project structure.
  • v1.0.0 (2019-1-17):Divide all permissions into single permission file to avoid fail to commit AppStore and provide various ways to request..

Issues and Improvements

  • If you find some bugs or things to improve when use the lib, you can open a issue directly. If you have a better implementation, welcome to Pull Request.

Discussion and Learning

  • iOS QQ group:709148214
  • Official Account(Wechat):猿视角(iOSDevSkills)
  • Wechat:401788217
  • Jianshu

Author

Jacklin, 401788217@qq.com

License

JLAuthorizationManagerSwift is available under the MIT license. See the LICENSE file for more info.

About

Swift version for authorization manager

Resources

License

Stars

Watchers

Forks

Packages

No packages published