1

iOS系统授权开发

iOS系统开发中,最常用的系统授权,莫过于系统通知,用户相册,位置服务了,这篇文章将简单讲解这三项功能的开发,并附带我写的一个开源项目,统一管理系统授权。

注:本文和项目基于iOS 8.0及以上系统框架,低版本框架接口略有不同。

截图

系统通知授权

系统通知方法在UIApplication类方法中,其中使用isRegisteredForRemoteNotifications获取本地推送授权状态。

+ (UIUserNotificationType)notificationType
{
    UIApplication* application = [UIApplication sharedApplication];
    return  [application currentUserNotificationSettings].types;
}

这里授权状态的枚举类型有

  1. UIUserNotificationTypeNone 无授权

  2. UIUserNotificationTypeBadge 角标

  3. UIUserNotificationTypeSound 声音

  4. 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]];

注册本地通知也是有回调的,实现UIApplicationDelegatedidRegisterUserNotificationSettings方法。

- (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);

这里授权状态有四个状态

  1. PHAuthorizationStatusNotDetermined 未授权

  2. PHAuthorizationStatusRestricted 授权中

  3. PHAuthorizationStatusDenied 拒绝

  4. 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!,因为PermissionScopeswift写的,需要编译成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) {
                
            }];

这个例子很明了吧,但要注意几点

  1. 最少要添加一个Permission

  2. 最多添加3个Permission

  3. 8.0以后,进行位置服务授权,需要在工程的Info.plist文件中加入NSLocationAlwaysUsageDescription字段。


秋刀生鱼片
2.1k 声望82 粉丝

独立游戏开发者