You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
8.3 KiB
8.3 KiB
JLAuthorizationManager
中文文档请参看这里
🔑 JLAuthorizationManager is a simple, lightWeight, easy-to-use, complete, thread-safety permission lib for iOS developers to use.
At present, the lib has supported Objective-C version and Swift version
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 JLAuthorizationManager can target: iOS 8.0 or later.
- Xcode 8.0 or later required.
Installation
- Via Cocoapods:
1.InstallCocoapods;
2.Runpod repo update
to make CocoaPods aware of the latest available JLAuthorizationManager versions.
3.Due to Podfile 's target, add corresponding pod commands as follows,then runpod install
.In the end, use the .xcworkspace file generated by CocoaPods to work on your project.
// pod 'JLAuthorizationManager/All' is equivatent to
pod 'JLAuthorizationManager'
Or
pod 'JLAuthorizationManager/AuthorizationManager'
Or
pod 'JLAuthorizationManager/Camera'
Or
pod 'JLAuthorizationManager/Microphone'
...
- Via Manually
1.first,clone project from https://github.com/123sunxiaolin/JLAuthorizationManager.git;
2.Then, find document pathJLAuthorizationManager/Classes
, and permission file you need.Need to pay attention to, must add Base folder into your project before use.
Tutorials
Usage of JLAuthorizationManager:
- 1.Via
+ (JLAuthorizationManager *)defaultManager;
singleton method to use. - 2.add head file
#import "JLAuthorizationManager.h"
- 3.Use uniform interface:
// Geneeral
- (void)JL_requestAuthorizationWithAuthorizationType:(JLAuthorizationType)authorizationType
authorizedHandler:(void(^)())authorizedHandler
unAuthorizedHandler:(void(^)())unAuthorizedHandler;
// Health Data
- (void)JL_requestHealthAuthorizationWithShareTypes:(NSSet*)typesToShare
readTypes:(NSSet*)typesToRead
authorizedHandler:(JLGeneralAuthorizationCompletion)authorizedHandler
unAuthorizedHandler:(JLGeneralAuthorizationCompletion)unAuthorizedHandler;
// Social Account( deprecated at 8.0 or later)
- (void)JL_requestAccountAuthorizationWithAuthorizationType:(JLAuthorizationType)authorizationType
options:(NSDictionary *)options
authorizedHandler:(JLGeneralAuthorizationCompletion)authorizedHandler
unAuthorizedHandler:(JLGeneralAuthorizationCompletion)unAuthorizedHandler
errorHandler:(void(^)(NSError *error))errorHandler;
Usage of single permission - JLxxxPermission:
-
- Every single permission class inherits from base classJLBasePermisssion, implementing a unified interface protocol JLAuthorizationProtocol:
@protocol JLAuthorizationProtocol <NSObject>
/**
return current request authorization type.
*/
@property (nonatomic, assign, readonly) JLAuthorizationType type;
/**
return current authorization status.
In most cases, suggest use 'requestAuthorizationWithCompletion:completion' method.
*/
- (JLAuthorizationStatus)authorizationStatus;
/**
Request authorization and return authorization status in main thread.
*/
- (void)requestAuthorizationWithCompletion:(JLAuthorizationCompletion)completion;
@optional
/**
Wheather add specific permission description key needed.
*/
- (BOOL)hasSpecificPermissionKeyFromInfoPlist;
@end
- Basic usage(take request photos permission as an example):
JLPhotosPermission *permission = [JLPhotosPermission instance];
NSLog(@"current authoriazation status:%@", [self authorizationWithStatus:permission.authorizationStatus]);
NSLog(@"%@ add permission plist description key", permission.hasSpecificPermissionKeyFromInfoPlist ? @"Yes" : @"NO");
[permission requestAuthorizationWithCompletion:^(BOOL granted) {
NSLog(@"%@ : %@", [self titleWithType:permission.type], granted ? @"authorized" : @"unauthorized");
}];
- Use singleton method
sharedInstance
to call, when requesting Location and Bluetooth permission. Take requesting Location permission as an excample:
JLLocationAlwaysPermission *permission = [JLLocationAlwaysPermission sharedInstance];
NSLog(@"current authoriazation status:%@", [self authorizationWithStatus:permission.authorizationStatus]);
NSLog(@"%@ add permission plist description key", permission.hasSpecificPermissionKeyFromInfoPlist ? @"Yes" : @"No");
[permission requestAuthorizationWithCompletion:^(BOOL granted) {
NSLog(@"%@ : %@", [self titleWithType:permission.type], granted ? @"authorized" : @"unauthorized");
}];
- Health Data request:
HKObjectType *type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
HKObjectType *type1 = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSSet *shareSet = [NSSet setWithObjects:type, type1, nil];
JLHealthPermission *permission = [[JLHealthPermission alloc] initWithShareType:shareSet readTypes:shareSet];
NSLog(@"current authoriazation status:%@", [self authorizationWithStatus:permission.authorizationStatus]);
NSLog(@"%@ add permission plist description key", permission.hasSpecificPermissionKeyFromInfoPlist ? @"Yes" : @"NO");
[permission requestAuthorizationWithCompletion:^(BOOL granted) {
NSLog(@"%@ : %@", [self titleWithType:permission.type], granted ? @"authorized" : @"unauthorized");
}];
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
orSiri
,please open switch onCapabilities
,then system createxx..entitlements
file automatically. - if project is not submited to App store, you can use the unified permission manager file
JLAuthorizationManager
; 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
- v2.1.0 (2019-5-21):fix API available version error.
- v2.0.2 (2019-2-07):Update podSpec file.
- v2.0.0 (2019-2-07):Divide all permissions into single permission file to avoid fail to commit AppStore and provide various ways to request.
- v1.1.0 (2019-1-17):Optimize request methods and add notification permission.
- v1.0.0 (2017-04-13):Provide simple usage of permission for developer and all request usages are in unique class-JLAuthorizationManager.
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
License
JLAuthorizationManager is under MIT license. See the LICENSE file for more info.