iOS系统开发中,最经常使用的系统受权,莫过于系统通知
,用户相册
,位置服务
了,这篇文章将简单讲解这三项功能的开发,并附带我写的一个开源项目,统一管理系统受权。git
注:本文和项目基于iOS 8.0
及以上系统框架,低版本框架接口略有不一样。github
系统通知方法在UIApplication类方法中,其中使用isRegisteredForRemoteNotifications
获取本地推送受权状态。swift
+ (UIUserNotificationType)notificationType { UIApplication* application = [UIApplication sharedApplication]; return [application currentUserNotificationSettings].types; }
这里受权状态的枚举类型有app
UIUserNotificationTypeNone
无受权框架
UIUserNotificationTypeBadge
角标工具
UIUserNotificationTypeSound
声音this
UIUserNotificationTypeAlert
通知spa
原枚举以下rest
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) { UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received } NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
受权方法code
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound; UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
注意,每一项受权,一旦用户拒绝,必须前往设置
的相关APP页面开启。APP内跳设置
的方法是
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
注册本地通知也是有回调的,实现UIApplicationDelegate
的didRegisterUserNotificationSettings
方法。
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { }
相应的也有失败的回调。
8.0系统版本之后,框架中加入了Photos.framework
框架,固然是用UIImagePickerController
一样会提醒用户受权使用相册或相机,这里介绍一下Photos
框架的受权。
相册权限状态
+ (PHAuthorizationStatus)photoAccesStatus { return [PHPhotoLibrary authorizationStatus]; }
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) { PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application PHAuthorizationStatusRestricted, // This application is not authorized to access photo data. // The user cannot change this application’s status, possibly due to active restrictions // such as parental controls being in place. PHAuthorizationStatusDenied, // User has explicitly denied this application access to photos data. PHAuthorizationStatusAuthorized // User has authorized this application to access photos data. } NS_AVAILABLE_IOS(8_0);
这里受权状态有四个状态
PHAuthorizationStatusNotDetermined
未受权
PHAuthorizationStatusRestricted
受权中
PHAuthorizationStatusDenied
拒绝
PHAuthorizationStatusAuthorized
已受权
受权Block方法
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { }];
位置服务受权稍微复杂一点点,8.0之后,进行位置服务受权要注意一点是,须要在工程的Info.plist
文件中加入NSLocationAlwaysUsageDescription
字段。字段中是开发者展现给用户的位置服务的使用场景介绍,或者是请求受权的描述。若是不添加这个字段,受权接口无任何反应。
状态接口
+ (CLAuthorizationStatus)positionAuthorizationStatus { return [CLLocationManager authorizationStatus]; }
受权方法
+ (void)authorizedPosition:(CLLocationManager *)manager { [manager requestAlwaysAuthorization]; }
注意这里传入的manager必定要是个property
,若是是一个局部变量,大括号结束,释放掉了,受权就会消失,就会出现受权框一闪而过的现象。
开源项目 DeviceAccessViewController
PermissionScope
(Github)是一个超级屌,而且好用的开源控件,用来向用户申请系统受权。若是你有使用cocospod
管理工具,这样加入use_frameworks!
,由于PermissionScope
是swift
写的,须要编译成Framework才能够给ObjC用。
use_frameworks! pod 'PermissionScope'
具体用法
PermissionScope* scope = [[PermissionScope alloc] initWithBackgroundTapCancels:YES]; //[scope addPermission:[[CameraPermission alloc] init] message:@"请求使用您的相机"]; //[scope addPermission:[[BluetoothPermission alloc] init] message:@"请求使用您的蓝牙"]; //[scope addPermission:[[ContactsPermission alloc] init] message:@"请求使用您的通信录"]; //[scope addPermission:[[EventsPermission alloc] init] message:@"请求使用您的日历"]; [scope addPermission:[[LocationAlwaysPermission alloc] init] message:@"请求使用您的位置"]; [scope addPermission:[[MicrophonePermission alloc] init] message:@"请求使用您的麦克风"]; [scope addPermission:[[NotificationsPermission alloc] initWithNotificationCategories:nil] message:@"请求使用通知服务"]; [scope show:^(BOOL s, NSArray<PermissionResult *> * _Nonnull results) { } cancelled:^(NSArray<PermissionResult *> * _Nonnull canceled) { }];
这个例子很明了吧,但要注意几点
最少要添加一个Permission
最多添加3个Permission
8.0之后,进行位置服务受权,须要在工程的Info.plist
文件中加入NSLocationAlwaysUsageDescription
字段。