Latest 2.1.0
Homepage https://github.com/123sunxiaolin/JLAuthorizationManager
License MIT
Platforms ios 8.0, requires ARC
Authors

JLAuthorizationManager

Build Status Pod Version Pod Platform System Version Pod License

中文文档请参看这里

Swift-Version

🔑 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

  • [x] Full coverage, currently supports access to Photos, Camera, CellularNetwork, Microphone, Calendar, Reminder, Notification, Location, AppleMusic, SpeechRecognizer, Siri, Bluetooth etc.
  • [x] Easy-to-use, unified interface and separated singe permission.Avoid submitting App Store rejected issues.
  • [x] Async fetch authorized permission and callback always in main thread.
  • [x] 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.Run pod 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 run pod 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'
...

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:

    1. 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 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 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.

Latest podspec

{
    "name": "JLAuthorizationManager",
    "version": "2.1.0",
    "summary": "A Project can provide uniform method for system authorization accesses.",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/123sunxiaolin/JLAuthorizationManager",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Jacklin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/123sunxiaolin/JLAuthorizationManager.git",
        "tag": "2.1.0"
    },
    "social_media_url": "https://123sunxiaolin.github.io",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "JLAuthorizationManager/Classes/**/*",
    "default_subspecs": "All",
    "subspecs": [
        {
            "name": "All",
            "source_files": [
                "JLAuthorizationManager/Classes/Base/",
                "JLAuthorizationManager/Classes/Permissions/"
            ]
        },
        {
            "name": "Base",
            "source_files": "JLAuthorizationManager/Classes/Base/*"
        },
        {
            "name": "AuthorizationManager",
            "source_files": "JLAuthorizationManager/Classes/AuthorizationManager/*",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Camera",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLCameraPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Microphone",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLMicrophonePermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Notification",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLNotificationPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Photos",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLPhotosPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "CellularNetwork",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLCellularNetWorkPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Contact",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLContactPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Calendar",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLCalendarPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Reminder",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLReminderPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Location",
            "source_files": [
                "JLAuthorizationManager/Classes/Permissions/JLLocationAlwaysPermission.{h,m}",
                "JLAuthorizationManager/Classes/Permissions/JLLocationInUsePermission.{h,m}"
            ],
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "AppleMusic",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLAppleMusicPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "SpeechRecognizer",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLSpeechRecognizerPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Siri",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLSiriPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Motion",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLMotionPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Bluetooth",
            "source_files": [
                "JLAuthorizationManager/Classes/Permissions/JLBluetoothPermission.{h,m}",
                "JLAuthorizationManager/Classes/Permissions/JLBluetoothPeripheralPermission.{h,m}"
            ],
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        },
        {
            "name": "Health",
            "source_files": "JLAuthorizationManager/Classes/Permissions/JLHealthPermission.{h,m}",
            "dependencies": {
                "JLAuthorizationManager/Base": []
            }
        }
    ]
}

Pin It on Pinterest

Share This