当前位置: 首页 > news >正文

iOS 系统授权开发

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字段。

相关文章:

  • Kubernetes首爆严重安全漏洞,请升级你的Kubernetes
  • oracle asm amdu和dd使用
  • shell脚本编程之“最简单的死循环”【转】
  • 用户,组和权限零碎知识
  • Java 与 PHP 的MD5加密方法
  • 关闭webstorm(2017.3.5)的分号检测
  • vue 之 .sync 修饰符
  • 75、STP环路防护UDLD、Loop Guard简介
  • TensorFlow提示AVX2...
  • 【转】Android自动化测试(UiAutomator)简要介绍
  • 进程 守护
  • sharepoint2013用场管理员进行文档库的爬网提示没有权限,拒绝的解决方法
  • Android中callback(接口回调)机制
  • 模式探索仍有心结,呷哺呷哺应该投入智能科技的怀抱?
  • HTTP请求中POST与GET的差别
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • HTTP 简介
  • nodejs:开发并发布一个nodejs包
  • Redis中的lru算法实现
  • Redux 中间件分析
  • SQLServer插入数据
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 精彩代码 vue.js
  • 使用API自动生成工具优化前端工作流
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • Semaphore
  • 容器镜像
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • ​马来语翻译中文去哪比较好?
  • #NOIP 2014# day.1 T2 联合权值
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (11)MATLAB PCA+SVM 人脸识别
  • (2)nginx 安装、启停
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (十六)Flask之蓝图
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • .gitignore文件设置了忽略但不生效
  • .Net 4.0并行库实用性演练
  • .NET CF命令行调试器MDbg入门(一)
  • .NET Core跨平台微服务学习资源
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .net下的富文本编辑器FCKeditor的配置方法
  • .NET中使用Protobuffer 实现序列化和反序列化
  • @javax.ws.rs Webservice注解
  • @vue/cli脚手架
  • [ 网络基础篇 ] MAP 迈普交换机常用命令详解