iOS系统授权开发
iOS系统开发中,最常用的系统授权,莫过于系统通知
,用户相册
,位置服务
了,这篇文章将简单讲解这三项功能的开发,并附带我写的一个开源项目,统一管理系统授权。
注:本文和项目基于iOS 8.0
及以上系统框架,低版本框架接口略有不同。
系统通知授权
系统通知方法在UIApplication类方法中,其中使用isRegisteredForRemoteNotifications
获取本地推送授权状态。
+ (UIUserNotificationType)notificationType
{
UIApplication* application = [UIApplication sharedApplication];
return [application currentUserNotificationSettings].types;
}
这里授权状态的枚举类型有
UIUserNotificationTypeNone
无授权UIUserNotificationTypeBadge
角标UIUserNotificationTypeSound
声音UIUserNotificationTypeAlert
通知
原枚举如下
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;
授权方法
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 项目
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
字段。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。