iOS 系统受权开发

iOS系统受权开发

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

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

截图

系统通知受权

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

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

这里受权状态的枚举类型有app

  1. UIUserNotificationTypeNone 无受权框架

  2. UIUserNotificationTypeBadge 角标工具

  3. UIUserNotificationTypeSound 声音this

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

注册本地通知也是有回调的,实现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字段。

相关文章
相关标签/搜索