├── .gitignore ├── Demo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m ├── LICENSE ├── README.md ├── ZRAuthorizationManager.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── ZRAutorizationManager ├── ZRAutorizationLocationManager.h ├── ZRAutorizationLocationManager.m ├── ZRAutorizationManager.h └── ZRAutorizationManager.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /Demo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Demo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Demo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Demo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 28 | 36 | 44 | 52 | 60 | 68 | 76 | 84 | 91 | 99 | 107 | 115 | 123 | 131 | 139 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | -------------------------------------------------------------------------------- /Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | NSCameraUsageDescription 28 | 请允许访问相机 29 | NSCalendarsUsageDescription 30 | 请允许访问日历 31 | NSContactsUsageDescription 32 | 请允许访问联系人 33 | NSLocationUsageDescription 34 | 请允许定位 35 | NSLocationWhenInUseUsageDescription 36 | 请允许定位 37 | NSMicrophoneUsageDescription 38 | 请允许访问麦克风 39 | NSPhotoLibraryUsageDescription 40 | 请允许访问相册 41 | UIRequiredDeviceCapabilities 42 | 43 | armv7 44 | 45 | UISupportedInterfaceOrientations 46 | 47 | UIInterfaceOrientationPortrait 48 | UIInterfaceOrientationLandscapeLeft 49 | UIInterfaceOrientationLandscapeRight 50 | 51 | UISupportedInterfaceOrientations~ipad 52 | 53 | UIInterfaceOrientationPortrait 54 | UIInterfaceOrientationPortraitUpsideDown 55 | UIInterfaceOrientationLandscapeLeft 56 | UIInterfaceOrientationLandscapeRight 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Demo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /Demo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "ZRAutorizationManager.h" 11 | #import "ZRAutorizationLocationManager.h" 12 | 13 | @interface ViewController () 14 | 15 | 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | 23 | [ZRAutorizationLocationManager manager].locationManager.delegate = self; 24 | } 25 | 26 | #pragma mark - 授权状态 27 | - (IBAction)photoLibraryStatusClick:(id)sender { 28 | ZRAutorizationStatus status = [ZRAutorizationManager autorizationStatusForType:ZRAutorizationTypePhotoLibrary]; 29 | [self showAlertWithTitle:@"相册" message:[self messageForStatus:status]]; 30 | } 31 | 32 | - (IBAction)cameraStatusClick:(id)sender { 33 | ZRAutorizationStatus status = [ZRAutorizationManager autorizationStatusForType:ZRAutorizationTypeCamera]; 34 | [self showAlertWithTitle:@"相机" message:[self messageForStatus:status]]; 35 | } 36 | 37 | - (IBAction)microphoneStatusClick:(id)sender { 38 | ZRAutorizationStatus status = [ZRAutorizationManager autorizationStatusForType:ZRAutorizationTypeMicrophone]; 39 | [self showAlertWithTitle:@"麦克风" message:[self messageForStatus:status]]; 40 | } 41 | 42 | - (IBAction)pushStatusClick:(id)sender { 43 | ZRAutorizationStatus status = [ZRAutorizationManager autorizationStatusForType:ZRAutorizationTypePush]; 44 | [self showAlertWithTitle:@"推送" message:[self messageForStatus:status]]; 45 | } 46 | 47 | - (IBAction)locationStatusClick:(id)sender { 48 | CLAuthorizationStatus status = [ZRAutorizationLocationManager autorizationStatus]; 49 | NSString *msg; 50 | if (status == kCLAuthorizationStatusNotDetermined) { 51 | msg = @"还未选择是否授权"; 52 | } 53 | else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) { 54 | msg = @"已拒绝授权"; 55 | } 56 | else { 57 | msg = @"已授权"; 58 | } 59 | [self showAlertWithTitle:@"定位" message:msg]; 60 | } 61 | 62 | - (IBAction)calendarStatusClick:(id)sender { 63 | ZRAutorizationStatus status = [ZRAutorizationManager autorizationStatusForType:ZRAutorizationTypeCalendar]; 64 | [self showAlertWithTitle:@"日历" message:[self messageForStatus:status]]; 65 | } 66 | 67 | - (IBAction)contactsStatusClick:(id)sender { 68 | ZRAutorizationStatus status = [ZRAutorizationManager autorizationStatusForType:ZRAutorizationTypeContacts]; 69 | [self showAlertWithTitle:@"联系人" message:[self messageForStatus:status]]; 70 | } 71 | 72 | 73 | - (NSString *)messageForStatus:(ZRAutorizationStatus)status { 74 | if (status == ZRAutorizationStatusNotDetermined) { 75 | return @"还未选择是否授权"; 76 | } 77 | else if (status == ZRAutorizationStatusDenied) { 78 | return @"已拒绝授权"; 79 | } 80 | else if (status == ZRAutorizationStatusAuthorized) { 81 | return @"已授权"; 82 | } 83 | else { 84 | return @"状态未知"; 85 | } 86 | } 87 | 88 | - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message { 89 | UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 90 | 91 | UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; 92 | 93 | UIAlertAction *settingAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 94 | 95 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil]; 96 | 97 | }]; 98 | 99 | [alert addAction:cancelAction]; 100 | [alert addAction:settingAction]; 101 | 102 | [self presentViewController:alert animated:YES completion:nil]; 103 | 104 | } 105 | 106 | 107 | 108 | #pragma mark - 获取授权 109 | 110 | - (IBAction)requestPhotoLibraryClick:(id)sender { 111 | [ZRAutorizationManager requestAuthorization:ZRAutorizationTypePhotoLibrary mainThreadGrantedBlock:^(BOOL granted) { 112 | if (!granted) { 113 | [self showAlertWithTitle:@"需要相册授权" message:@"请在设置中允许访问相册"]; 114 | } 115 | }]; 116 | } 117 | 118 | 119 | - (IBAction)requestCalendarClick:(id)sender { 120 | [ZRAutorizationManager requestAuthorization:ZRAutorizationTypeCalendar mainThreadGrantedBlock:^(BOOL granted) { 121 | if (!granted) { 122 | [self showAlertWithTitle:@"需要日历授权" message:@"请在设置中允许访问日历"]; 123 | } 124 | }]; 125 | } 126 | 127 | - (IBAction)requestContactsClick:(id)sender { 128 | [ZRAutorizationManager requestAuthorization:ZRAutorizationTypeContacts mainThreadGrantedBlock:^(BOOL granted) { 129 | if (!granted) { 130 | [self showAlertWithTitle:@"需要通讯录授权" message:@"请在设置中允许访问通讯录"]; 131 | } 132 | }]; 133 | } 134 | 135 | - (IBAction)requestLocationClick:(id)sender { 136 | [ZRAutorizationLocationManager requestAuthorizationAlwaysOrWhenInUse:NO]; 137 | } 138 | 139 | - (IBAction)requestCameraClick:(id)sender { 140 | [ZRAutorizationManager requestAuthorization:ZRAutorizationTypeCamera mainThreadGrantedBlock:^(BOOL granted) { 141 | if (!granted) { 142 | [self showAlertWithTitle:@"需要相机授权" message:@"请在设置中允许访问相机"]; 143 | } 144 | }]; 145 | } 146 | 147 | 148 | - (IBAction)requestPushClick:(id)sender { 149 | [ZRAutorizationManager requestAuthorization:ZRAutorizationTypePush mainThreadGrantedBlock:^(BOOL granted) { 150 | if (!granted) { 151 | [self showAlertWithTitle:@"需要推送授权" message:@"请在设置中允许推送"]; 152 | } 153 | }]; 154 | } 155 | 156 | - (IBAction)requestMicrophoneClick:(id)sender { 157 | [ZRAutorizationManager requestAuthorization:ZRAutorizationTypeMicrophone mainThreadGrantedBlock:^(BOOL granted) { 158 | if (!granted) { 159 | [self showAlertWithTitle:@"需要麦克风授权" message:@"请在设置中允许访问麦克风"]; 160 | } 161 | }]; 162 | } 163 | 164 | 165 | #pragma mark - CLLocationManagerDelegate 166 | - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 167 | if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) { 168 | [self showAlertWithTitle:@"需要定位授权" message:@"请在设置中允许定位"]; 169 | } 170 | } 171 | 172 | @end 173 | -------------------------------------------------------------------------------- /Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 RangerZz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZRAutorizationManager 2 | iOS常见的权限状态和请求授权 3 | ZRAutorizationManager封装了iOS开发中常用的相册,相机,麦克风推送,日历,联系人,定位等权限的状态和主动请求授权,一行代码搞定繁琐的权限状态和请求过程. 4 | 5 | ------- 6 | 7 | ### 授权状态 8 | ``` 9 | typedef NS_ENUM(NSUInteger, ZRAutorizationStatus) { 10 | ZRAutorizationStatusNotDetermined,// 用户还未选择是否允许授权 可以代表第一次询问 11 | ZRAutorizationStatusDenied,// 拒绝授权 包括 Restricted & Denied 12 | ZRAutorizationStatusAuthorized,// 已授权 13 | 14 | ZRAutorizationStatusUnknow,// 未知状态 15 | }; 16 | ``` 17 | ### 获取授权状态 18 | ``` 19 | + (ZRAutorizationStatus)autorizationStatusForType:(ZRAutorizationType)type; 20 | ``` 21 | ### 主动请求授权 22 | * 请求授权后结果的回调是系统API回调的线程,一般不是主线程 23 | 24 | ``` 25 | + (void)requestAuthorization:(ZRAutorizationType)type grantedBlock:(void (^)(BOOL granted))grantedBlock; 26 | ``` 27 | * 请求授权后结果的回调是主线程 28 | 29 | ``` 30 | + (void)requestAuthorization:(ZRAutorizationType)type mainThreadGrantedBlock:(void (^)(BOOL granted))grantedBlock; 31 | ``` 32 | 33 | #### Tips 34 | * 记得在info.plist配置对应的权限描述 35 | * 由于定位权限API的特殊性,只建议参考`ZRAutorizationLocationManager`的调用方式,不建议直接使用'ZRAutorizationLocationManager',最好在需要的地方自己实现对应代码. 36 | 37 | ------- 38 | 权限相关的详细内容请查看[我的博客](https://rangerzz.github.io/post/4/) 39 | 40 | 欢迎补充建议交流提issue 41 | -------------------------------------------------------------------------------- /ZRAuthorizationManager.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4430E1F321C7AD7200073E07 /* ZRAutorizationLocationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4430E1F221C7AD7200073E07 /* ZRAutorizationLocationManager.m */; }; 11 | 445DB36321C1F88100487D5F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 445DB36221C1F88100487D5F /* AppDelegate.m */; }; 12 | 445DB36621C1F88100487D5F /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 445DB36521C1F88100487D5F /* ViewController.m */; }; 13 | 445DB36921C1F88100487D5F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 445DB36721C1F88100487D5F /* Main.storyboard */; }; 14 | 445DB36B21C1F88200487D5F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 445DB36A21C1F88200487D5F /* Assets.xcassets */; }; 15 | 445DB36E21C1F88200487D5F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 445DB36C21C1F88200487D5F /* LaunchScreen.storyboard */; }; 16 | 445DB37121C1F88200487D5F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 445DB37021C1F88200487D5F /* main.m */; }; 17 | 445DB37921C1F90B00487D5F /* ZRAutorizationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 445DB37821C1F90B00487D5F /* ZRAutorizationManager.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 4430E1F121C7AD7200073E07 /* ZRAutorizationLocationManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ZRAutorizationLocationManager.h; sourceTree = ""; }; 22 | 4430E1F221C7AD7200073E07 /* ZRAutorizationLocationManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ZRAutorizationLocationManager.m; sourceTree = ""; }; 23 | 445DB35E21C1F88100487D5F /* ZRAuthorizationManager.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ZRAuthorizationManager.app; sourceTree = BUILT_PRODUCTS_DIR; }; 24 | 445DB36121C1F88100487D5F /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 25 | 445DB36221C1F88100487D5F /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 26 | 445DB36421C1F88100487D5F /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 27 | 445DB36521C1F88100487D5F /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 28 | 445DB36821C1F88100487D5F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29 | 445DB36A21C1F88200487D5F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 30 | 445DB36D21C1F88200487D5F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 31 | 445DB36F21C1F88200487D5F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | 445DB37021C1F88200487D5F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 33 | 445DB37721C1F90B00487D5F /* ZRAutorizationManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ZRAutorizationManager.h; sourceTree = ""; }; 34 | 445DB37821C1F90B00487D5F /* ZRAutorizationManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ZRAutorizationManager.m; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | 445DB35B21C1F88100487D5F /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | 445DB35521C1F88100487D5F = { 49 | isa = PBXGroup; 50 | children = ( 51 | 448486C921C9EC5400352491 /* ZRAutorizationManager */, 52 | 445DB36021C1F88100487D5F /* Demo */, 53 | 445DB35F21C1F88100487D5F /* Products */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | 445DB35F21C1F88100487D5F /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 445DB35E21C1F88100487D5F /* ZRAuthorizationManager.app */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | 445DB36021C1F88100487D5F /* Demo */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 445DB36121C1F88100487D5F /* AppDelegate.h */, 69 | 445DB36221C1F88100487D5F /* AppDelegate.m */, 70 | 445DB36421C1F88100487D5F /* ViewController.h */, 71 | 445DB36521C1F88100487D5F /* ViewController.m */, 72 | 445DB36721C1F88100487D5F /* Main.storyboard */, 73 | 445DB36A21C1F88200487D5F /* Assets.xcassets */, 74 | 445DB36C21C1F88200487D5F /* LaunchScreen.storyboard */, 75 | 445DB36F21C1F88200487D5F /* Info.plist */, 76 | 445DB37021C1F88200487D5F /* main.m */, 77 | ); 78 | path = Demo; 79 | sourceTree = ""; 80 | }; 81 | 448486C921C9EC5400352491 /* ZRAutorizationManager */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | 445DB37721C1F90B00487D5F /* ZRAutorizationManager.h */, 85 | 445DB37821C1F90B00487D5F /* ZRAutorizationManager.m */, 86 | 4430E1F121C7AD7200073E07 /* ZRAutorizationLocationManager.h */, 87 | 4430E1F221C7AD7200073E07 /* ZRAutorizationLocationManager.m */, 88 | ); 89 | path = ZRAutorizationManager; 90 | sourceTree = ""; 91 | }; 92 | /* End PBXGroup section */ 93 | 94 | /* Begin PBXNativeTarget section */ 95 | 445DB35D21C1F88100487D5F /* ZRAuthorizationManager */ = { 96 | isa = PBXNativeTarget; 97 | buildConfigurationList = 445DB37421C1F88200487D5F /* Build configuration list for PBXNativeTarget "ZRAuthorizationManager" */; 98 | buildPhases = ( 99 | 445DB35A21C1F88100487D5F /* Sources */, 100 | 445DB35B21C1F88100487D5F /* Frameworks */, 101 | 445DB35C21C1F88100487D5F /* Resources */, 102 | ); 103 | buildRules = ( 104 | ); 105 | dependencies = ( 106 | ); 107 | name = ZRAuthorizationManager; 108 | productName = ZRAuthorizationManager; 109 | productReference = 445DB35E21C1F88100487D5F /* ZRAuthorizationManager.app */; 110 | productType = "com.apple.product-type.application"; 111 | }; 112 | /* End PBXNativeTarget section */ 113 | 114 | /* Begin PBXProject section */ 115 | 445DB35621C1F88100487D5F /* Project object */ = { 116 | isa = PBXProject; 117 | attributes = { 118 | LastUpgradeCheck = 1010; 119 | ORGANIZATIONNAME = Ranger; 120 | TargetAttributes = { 121 | 445DB35D21C1F88100487D5F = { 122 | CreatedOnToolsVersion = 10.1; 123 | }; 124 | }; 125 | }; 126 | buildConfigurationList = 445DB35921C1F88100487D5F /* Build configuration list for PBXProject "ZRAuthorizationManager" */; 127 | compatibilityVersion = "Xcode 9.3"; 128 | developmentRegion = en; 129 | hasScannedForEncodings = 0; 130 | knownRegions = ( 131 | en, 132 | Base, 133 | ); 134 | mainGroup = 445DB35521C1F88100487D5F; 135 | productRefGroup = 445DB35F21C1F88100487D5F /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 445DB35D21C1F88100487D5F /* ZRAuthorizationManager */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXResourcesBuildPhase section */ 145 | 445DB35C21C1F88100487D5F /* Resources */ = { 146 | isa = PBXResourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 445DB36E21C1F88200487D5F /* LaunchScreen.storyboard in Resources */, 150 | 445DB36B21C1F88200487D5F /* Assets.xcassets in Resources */, 151 | 445DB36921C1F88100487D5F /* Main.storyboard in Resources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXResourcesBuildPhase section */ 156 | 157 | /* Begin PBXSourcesBuildPhase section */ 158 | 445DB35A21C1F88100487D5F /* Sources */ = { 159 | isa = PBXSourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | 445DB36621C1F88100487D5F /* ViewController.m in Sources */, 163 | 445DB37121C1F88200487D5F /* main.m in Sources */, 164 | 4430E1F321C7AD7200073E07 /* ZRAutorizationLocationManager.m in Sources */, 165 | 445DB37921C1F90B00487D5F /* ZRAutorizationManager.m in Sources */, 166 | 445DB36321C1F88100487D5F /* AppDelegate.m in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXVariantGroup section */ 173 | 445DB36721C1F88100487D5F /* Main.storyboard */ = { 174 | isa = PBXVariantGroup; 175 | children = ( 176 | 445DB36821C1F88100487D5F /* Base */, 177 | ); 178 | name = Main.storyboard; 179 | sourceTree = ""; 180 | }; 181 | 445DB36C21C1F88200487D5F /* LaunchScreen.storyboard */ = { 182 | isa = PBXVariantGroup; 183 | children = ( 184 | 445DB36D21C1F88200487D5F /* Base */, 185 | ); 186 | name = LaunchScreen.storyboard; 187 | sourceTree = ""; 188 | }; 189 | /* End PBXVariantGroup section */ 190 | 191 | /* Begin XCBuildConfiguration section */ 192 | 445DB37221C1F88200487D5F /* Debug */ = { 193 | isa = XCBuildConfiguration; 194 | buildSettings = { 195 | ALWAYS_SEARCH_USER_PATHS = NO; 196 | CLANG_ANALYZER_NONNULL = YES; 197 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 198 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 199 | CLANG_CXX_LIBRARY = "libc++"; 200 | CLANG_ENABLE_MODULES = YES; 201 | CLANG_ENABLE_OBJC_ARC = YES; 202 | CLANG_ENABLE_OBJC_WEAK = YES; 203 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 204 | CLANG_WARN_BOOL_CONVERSION = YES; 205 | CLANG_WARN_COMMA = YES; 206 | CLANG_WARN_CONSTANT_CONVERSION = YES; 207 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 208 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 209 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 210 | CLANG_WARN_EMPTY_BODY = YES; 211 | CLANG_WARN_ENUM_CONVERSION = YES; 212 | CLANG_WARN_INFINITE_RECURSION = YES; 213 | CLANG_WARN_INT_CONVERSION = YES; 214 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 215 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 216 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 217 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 218 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 219 | CLANG_WARN_STRICT_PROTOTYPES = YES; 220 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 221 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 222 | CLANG_WARN_UNREACHABLE_CODE = YES; 223 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 224 | CODE_SIGN_IDENTITY = "iPhone Developer"; 225 | COPY_PHASE_STRIP = NO; 226 | DEBUG_INFORMATION_FORMAT = dwarf; 227 | ENABLE_STRICT_OBJC_MSGSEND = YES; 228 | ENABLE_TESTABILITY = YES; 229 | GCC_C_LANGUAGE_STANDARD = gnu11; 230 | GCC_DYNAMIC_NO_PIC = NO; 231 | GCC_NO_COMMON_BLOCKS = YES; 232 | GCC_OPTIMIZATION_LEVEL = 0; 233 | GCC_PREPROCESSOR_DEFINITIONS = ( 234 | "DEBUG=1", 235 | "$(inherited)", 236 | ); 237 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 238 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 239 | GCC_WARN_UNDECLARED_SELECTOR = YES; 240 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 241 | GCC_WARN_UNUSED_FUNCTION = YES; 242 | GCC_WARN_UNUSED_VARIABLE = YES; 243 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 244 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 245 | MTL_FAST_MATH = YES; 246 | ONLY_ACTIVE_ARCH = YES; 247 | SDKROOT = iphoneos; 248 | }; 249 | name = Debug; 250 | }; 251 | 445DB37321C1F88200487D5F /* Release */ = { 252 | isa = XCBuildConfiguration; 253 | buildSettings = { 254 | ALWAYS_SEARCH_USER_PATHS = NO; 255 | CLANG_ANALYZER_NONNULL = YES; 256 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 257 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 258 | CLANG_CXX_LIBRARY = "libc++"; 259 | CLANG_ENABLE_MODULES = YES; 260 | CLANG_ENABLE_OBJC_ARC = YES; 261 | CLANG_ENABLE_OBJC_WEAK = YES; 262 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 263 | CLANG_WARN_BOOL_CONVERSION = YES; 264 | CLANG_WARN_COMMA = YES; 265 | CLANG_WARN_CONSTANT_CONVERSION = YES; 266 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 267 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 268 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 269 | CLANG_WARN_EMPTY_BODY = YES; 270 | CLANG_WARN_ENUM_CONVERSION = YES; 271 | CLANG_WARN_INFINITE_RECURSION = YES; 272 | CLANG_WARN_INT_CONVERSION = YES; 273 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 274 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 275 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 276 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 277 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 278 | CLANG_WARN_STRICT_PROTOTYPES = YES; 279 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 280 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 281 | CLANG_WARN_UNREACHABLE_CODE = YES; 282 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 283 | CODE_SIGN_IDENTITY = "iPhone Developer"; 284 | COPY_PHASE_STRIP = NO; 285 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 286 | ENABLE_NS_ASSERTIONS = NO; 287 | ENABLE_STRICT_OBJC_MSGSEND = YES; 288 | GCC_C_LANGUAGE_STANDARD = gnu11; 289 | GCC_NO_COMMON_BLOCKS = YES; 290 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 291 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 292 | GCC_WARN_UNDECLARED_SELECTOR = YES; 293 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 294 | GCC_WARN_UNUSED_FUNCTION = YES; 295 | GCC_WARN_UNUSED_VARIABLE = YES; 296 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 297 | MTL_ENABLE_DEBUG_INFO = NO; 298 | MTL_FAST_MATH = YES; 299 | SDKROOT = iphoneos; 300 | VALIDATE_PRODUCT = YES; 301 | }; 302 | name = Release; 303 | }; 304 | 445DB37521C1F88200487D5F /* Debug */ = { 305 | isa = XCBuildConfiguration; 306 | buildSettings = { 307 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 308 | CODE_SIGN_STYLE = Automatic; 309 | DEVELOPMENT_TEAM = 5CDNCS6RR8; 310 | INFOPLIST_FILE = Demo/Info.plist; 311 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 312 | LD_RUNPATH_SEARCH_PATHS = ( 313 | "$(inherited)", 314 | "@executable_path/Frameworks", 315 | ); 316 | PRODUCT_BUNDLE_IDENTIFIER = com.ranger.authorization; 317 | PRODUCT_NAME = "$(TARGET_NAME)"; 318 | TARGETED_DEVICE_FAMILY = "1,2"; 319 | }; 320 | name = Debug; 321 | }; 322 | 445DB37621C1F88200487D5F /* Release */ = { 323 | isa = XCBuildConfiguration; 324 | buildSettings = { 325 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 326 | CODE_SIGN_STYLE = Automatic; 327 | DEVELOPMENT_TEAM = 5CDNCS6RR8; 328 | INFOPLIST_FILE = Demo/Info.plist; 329 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 330 | LD_RUNPATH_SEARCH_PATHS = ( 331 | "$(inherited)", 332 | "@executable_path/Frameworks", 333 | ); 334 | PRODUCT_BUNDLE_IDENTIFIER = com.ranger.authorization; 335 | PRODUCT_NAME = "$(TARGET_NAME)"; 336 | TARGETED_DEVICE_FAMILY = "1,2"; 337 | }; 338 | name = Release; 339 | }; 340 | /* End XCBuildConfiguration section */ 341 | 342 | /* Begin XCConfigurationList section */ 343 | 445DB35921C1F88100487D5F /* Build configuration list for PBXProject "ZRAuthorizationManager" */ = { 344 | isa = XCConfigurationList; 345 | buildConfigurations = ( 346 | 445DB37221C1F88200487D5F /* Debug */, 347 | 445DB37321C1F88200487D5F /* Release */, 348 | ); 349 | defaultConfigurationIsVisible = 0; 350 | defaultConfigurationName = Release; 351 | }; 352 | 445DB37421C1F88200487D5F /* Build configuration list for PBXNativeTarget "ZRAuthorizationManager" */ = { 353 | isa = XCConfigurationList; 354 | buildConfigurations = ( 355 | 445DB37521C1F88200487D5F /* Debug */, 356 | 445DB37621C1F88200487D5F /* Release */, 357 | ); 358 | defaultConfigurationIsVisible = 0; 359 | defaultConfigurationName = Release; 360 | }; 361 | /* End XCConfigurationList section */ 362 | }; 363 | rootObject = 445DB35621C1F88100487D5F /* Project object */; 364 | } 365 | -------------------------------------------------------------------------------- /ZRAuthorizationManager.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ZRAuthorizationManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ZRAutorizationManager/ZRAutorizationLocationManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // ZRAutorizationLocationManager.h 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/17. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface ZRAutorizationLocationManager : NSObject 15 | 16 | @property (nonatomic, strong, readonly) CLLocationManager *locationManager; 17 | 18 | + (instancetype)manager; 19 | 20 | 21 | + (CLAuthorizationStatus)autorizationStatus; 22 | 23 | + (void)requestAuthorizationAlwaysOrWhenInUse:(BOOL)always; 24 | 25 | 26 | @end 27 | 28 | NS_ASSUME_NONNULL_END 29 | -------------------------------------------------------------------------------- /ZRAutorizationManager/ZRAutorizationLocationManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // ZRAutorizationLocationManager.m 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/17. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import "ZRAutorizationLocationManager.h" 10 | 11 | @interface ZRAutorizationLocationManager () 12 | 13 | @property (nonatomic, strong, readwrite) CLLocationManager *locationManager; 14 | 15 | @end 16 | 17 | @implementation ZRAutorizationLocationManager 18 | 19 | 20 | + (instancetype)manager { 21 | static dispatch_once_t onceToken; 22 | static ZRAutorizationLocationManager *manager; 23 | dispatch_once(&onceToken, ^{ 24 | manager = [ZRAutorizationLocationManager new]; 25 | }); 26 | return manager; 27 | } 28 | 29 | + (CLAuthorizationStatus)autorizationStatus { 30 | return [CLLocationManager authorizationStatus]; 31 | } 32 | 33 | 34 | + (void)requestAuthorizationAlwaysOrWhenInUse:(BOOL)always { 35 | [[ZRAutorizationLocationManager manager] _requestAuthorizationAlwaysOrWhenInUse:always]; 36 | } 37 | 38 | - (void)_requestAuthorizationAlwaysOrWhenInUse:(BOOL)always{ 39 | if (always) { 40 | [self.locationManager requestAlwaysAuthorization]; 41 | } 42 | else { 43 | [self.locationManager requestWhenInUseAuthorization]; 44 | } 45 | } 46 | 47 | 48 | - (CLLocationManager *)locationManager { 49 | if (_locationManager == nil) { 50 | _locationManager = [[CLLocationManager alloc] init]; 51 | } 52 | return _locationManager; 53 | } 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /ZRAutorizationManager/ZRAutorizationManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // ZRAutorizationManager.h 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | typedef NS_ENUM(NSUInteger, ZRAutorizationType) { 13 | ZRAutorizationTypePhotoLibrary API_AVAILABLE(ios(8.0)), 14 | ZRAutorizationTypeCamera API_AVAILABLE(ios(7.0)), 15 | ZRAutorizationTypeMicrophone API_AVAILABLE(ios(7.0)), 16 | ZRAutorizationTypePush API_AVAILABLE(ios(10.0)), 17 | ZRAutorizationTypeCalendar API_AVAILABLE(ios(6.0)), 18 | ZRAutorizationTypeContacts API_AVAILABLE(ios(9.0)), 19 | // ZRAutorizationTypeLocation API_AVAILABLE(ios(4.2)), 20 | }; 21 | 22 | typedef NS_ENUM(NSUInteger, ZRAutorizationStatus) { 23 | ZRAutorizationStatusNotDetermined,// 用户还未选择是否允许授权 可以代表第一次询问 24 | ZRAutorizationStatusDenied,// 拒绝授权 包括 Restricted & Denied 25 | ZRAutorizationStatusAuthorized,// 已授权 26 | 27 | ZRAutorizationStatusUnknow,// 未知状态 28 | }; 29 | 30 | NS_ASSUME_NONNULL_BEGIN 31 | 32 | @interface ZRAutorizationManager : NSObject 33 | 34 | + (ZRAutorizationStatus)autorizationStatusForType:(ZRAutorizationType)type; 35 | 36 | + (void)requestAuthorization:(ZRAutorizationType)type grantedBlock:(void (^)(BOOL granted))grantedBlock; 37 | 38 | + (void)requestAuthorization:(ZRAutorizationType)type mainThreadGrantedBlock:(void (^)(BOOL granted))grantedBlock; 39 | 40 | 41 | @end 42 | 43 | NS_ASSUME_NONNULL_END 44 | -------------------------------------------------------------------------------- /ZRAutorizationManager/ZRAutorizationManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // ZRAutorizationManager.m 3 | // ZRAuthorizationManager 4 | // 5 | // Created by Ranger on 2018/12/13. 6 | // Copyright © 2018 Ranger. All rights reserved. 7 | // 8 | 9 | #import "ZRAutorizationManager.h" 10 | #import 11 | #import 12 | #import 13 | #import 14 | #import 15 | 16 | #define ZRSafe_Block(_block_, ...) if (_block_) {_block_(__VA_ARGS__);} 17 | 18 | 19 | @interface ZRAutorizationManager () 20 | 21 | @end 22 | 23 | 24 | @implementation ZRAutorizationManager 25 | 26 | + (instancetype)manager { 27 | static ZRAutorizationManager *manager; 28 | static dispatch_once_t onceToken; 29 | dispatch_once(&onceToken, ^{ 30 | manager = [ZRAutorizationManager new]; 31 | }); 32 | return manager; 33 | } 34 | 35 | 36 | + (ZRAutorizationStatus)autorizationStatusForType:(ZRAutorizationType)type { 37 | return [[ZRAutorizationManager manager] _autorizationStatusForType:type]; 38 | } 39 | 40 | + (void)requestAuthorization:(ZRAutorizationType)type grantedBlock:(void (^)(BOOL))grantedBlock { 41 | [[ZRAutorizationManager manager] _requestAuthorization:type grantedBlock:grantedBlock mainThread:NO]; 42 | } 43 | 44 | + (void)requestAuthorization:(ZRAutorizationType)type mainThreadGrantedBlock:(void (^)(BOOL))grantedBlock { 45 | [[ZRAutorizationManager manager] _requestAuthorization:type grantedBlock:grantedBlock mainThread:YES]; 46 | } 47 | 48 | 49 | #pragma mark - private 50 | 51 | - (ZRAutorizationStatus)_autorizationStatusForType:(ZRAutorizationType)type { 52 | 53 | ZRAutorizationStatus status = ZRAutorizationStatusUnknow; 54 | switch (type) { 55 | case ZRAutorizationTypePhotoLibrary: { 56 | status = [self _statusWithSystemStatus:[PHPhotoLibrary authorizationStatus]]; 57 | } break; 58 | 59 | case ZRAutorizationTypeCamera: { 60 | status = [self _statusWithSystemStatus:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]]; 61 | } break; 62 | 63 | case ZRAutorizationTypeMicrophone: { 64 | status = [self _statusWithSystemStatus:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]]; 65 | 66 | } break; 67 | 68 | case ZRAutorizationTypePush: { 69 | 70 | dispatch_semaphore_t signal = dispatch_semaphore_create(0); 71 | 72 | __block ZRAutorizationStatus tempStatus = ZRAutorizationStatusUnknow; 73 | [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { 74 | 75 | UNAuthorizationStatus unStatus = settings.authorizationStatus; 76 | 77 | if (@available(iOS 12.0, *)) { 78 | if (unStatus == UNAuthorizationStatusProvisional) { 79 | tempStatus = ZRAutorizationStatusAuthorized; 80 | } 81 | } 82 | 83 | if (unStatus == UNAuthorizationStatusNotDetermined) { 84 | tempStatus = ZRAutorizationStatusNotDetermined; 85 | }else if (unStatus == UNAuthorizationStatusDenied) { 86 | tempStatus = ZRAutorizationStatusDenied; 87 | }else if (unStatus == UNAuthorizationStatusAuthorized) { 88 | tempStatus = ZRAutorizationStatusAuthorized; 89 | } 90 | dispatch_semaphore_signal(signal); 91 | }]; 92 | 93 | dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER); 94 | status = tempStatus; 95 | 96 | } break; 97 | 98 | case ZRAutorizationTypeCalendar: { 99 | status = [self _statusWithSystemStatus:[EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]]; 100 | } break; 101 | 102 | case ZRAutorizationTypeContacts: { 103 | status = [self _statusWithSystemStatus:[CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]]; 104 | } break; 105 | 106 | default: 107 | status = ZRAutorizationStatusUnknow; 108 | break; 109 | } 110 | return status; 111 | } 112 | 113 | 114 | - (ZRAutorizationStatus)_statusWithSystemStatus:(NSInteger)status { 115 | switch (status) { 116 | case 0: 117 | return ZRAutorizationStatusNotDetermined; 118 | break; 119 | 120 | case 1: 121 | case 2: 122 | return ZRAutorizationStatusDenied; 123 | break; 124 | 125 | case 3: 126 | case 4: 127 | return ZRAutorizationStatusAuthorized; 128 | break; 129 | 130 | default: 131 | return ZRAutorizationStatusUnknow; 132 | break; 133 | } 134 | } 135 | 136 | - (void)_requestAuthorization:(ZRAutorizationType)type grantedBlock:(void (^)(BOOL granted))grantedBlock mainThread:(BOOL)isMain { 137 | 138 | void(^repeatBlock)(BOOL granted) = ^(BOOL granted){ 139 | if (granted) { 140 | if (isMain) { 141 | if ([NSThread isMainThread]) { 142 | ZRSafe_Block(grantedBlock, YES); 143 | } 144 | else { 145 | dispatch_sync(dispatch_get_main_queue(), ^{ 146 | ZRSafe_Block(grantedBlock, YES); 147 | }); 148 | } 149 | } 150 | else { 151 | ZRSafe_Block(grantedBlock,YES); 152 | } 153 | } 154 | else { 155 | if (isMain) { 156 | if ([NSThread isMainThread]) { 157 | ZRSafe_Block(grantedBlock, NO); 158 | } 159 | else { 160 | dispatch_sync(dispatch_get_main_queue(), ^{ 161 | ZRSafe_Block(grantedBlock, NO); 162 | }); 163 | } 164 | } 165 | else { 166 | ZRSafe_Block(grantedBlock,NO); 167 | } 168 | } 169 | }; 170 | 171 | switch (type) { 172 | case ZRAutorizationTypePhotoLibrary: { 173 | [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 174 | repeatBlock(status == PHAuthorizationStatusAuthorized); 175 | }]; 176 | } break; 177 | 178 | case ZRAutorizationTypeCamera: { 179 | [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { 180 | repeatBlock(granted); 181 | }]; 182 | } break; 183 | 184 | case ZRAutorizationTypeMicrophone: { 185 | [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) { 186 | repeatBlock(granted); 187 | }]; 188 | } break; 189 | 190 | case ZRAutorizationTypePush: { 191 | 192 | [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound |UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) { 193 | repeatBlock(granted); 194 | }]; 195 | 196 | 197 | } break; 198 | 199 | case ZRAutorizationTypeCalendar: { 200 | [[[EKEventStore alloc] init] requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) { 201 | repeatBlock(granted); 202 | }]; 203 | } break; 204 | 205 | case ZRAutorizationTypeContacts: { 206 | [[[CNContactStore alloc] init] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 207 | repeatBlock(granted); 208 | }]; 209 | } break; 210 | 211 | default: 212 | break; 213 | } 214 | } 215 | 216 | 217 | 218 | @end 219 | --------------------------------------------------------------------------------