CLP
CLP
  • 发布:2019-05-10 12:13
  • 更新:2023-09-06 09:59
  • 阅读:16670

iOS平台:用Native.js来写 如何判断系统功能权限是否开启

分类:Native.js

更新:插件市场已经提供了封装更完善版本:https://ext.dcloud.net.cn/plugin?id=594

1、推送

判断推送权限是否开启

var UIApplication = plus.ios.import("UIApplication");  
                var app = UIApplication.sharedApplication();  
                var enabledTypes = 0;  
                if (app.currentUserNotificationSettings) {  
                    var settings = app.currentUserNotificationSettings();  
                    enabledTypes = settings.plusGetAttribute("types");  
                    console.log("enabledTypes1:" + enabledTypes);  
                    if (enabledTypes == 0) {  
                        console.log("推送权限没有开启");  
                    }else {  
                        console.log("已经开启推送功能!")  
                    }  
                    plus.ios.deleteObject(settings);  
                } else {  
                    enabledTypes = app.enabledRemoteNotificationTypes();  
                    if (enabledTypes == 0) {  
                        console.log("推送权限没有开启!");  
                    } else {  
                        console.log("已经开启推送功能!")  
                    }  
                    console.log("enabledTypes2:" + enabledTypes);  
                }  
                plus.ios.deleteObject(app);  
                                plus.ios.deleteObject(UIApplication);

2、定位

判断定位权限是否开启

                                var cllocationManger = plus.ios.import("CLLocationManager");  
                var enable = cllocationManger.locationServicesEnabled();  
                var status = cllocationManger.authorizationStatus();  
                console.log("enable:" + enable);  
                console.log("status:" + status);  
                if (enable && status != 2) {  
                    console.log("手机系统的定位已经打开");  
                } else {  
                    console.log("手机系统的定位没有打开");  
                }  
                             plus.ios.deleteObject(cllocationManger);

3、麦克风

判断麦克风权限是否开启

                                var avaudiosession = plus.ios.import("AVAudioSession");  
                var avaudio = avaudiosession.sharedInstance();  
                var permissionStatus = avaudio.recordPermission();  
                console.log("permissionStatus:" + permissionStatus);  
                if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {   
                    console.log("麦克风权限没有开启");  
                } else {  
                    console.log("麦克风权限已经开启");  
                }  
                    plus.ios.deleteObject(avaudiosession);

4、相机

判断相机权限是否开启

                               var AVCaptureDevice = plus.ios.import("AVCaptureDevice");  
                var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');  
                console.log("authStatus:" + authStatus);  
                if (authStatus == 3) {  
                    console.log("相机权限已经开启");  
                } else {  
                    console.log("相机权限没有开启");  
                }  
                    plus.ios.deleteObject(AVCaptureDevice);

5、相册

判断相册权限是否开启

                              var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");  
                var authStatus = PHPhotoLibrary.authorizationStatus();  
                console.log("authStatus:" + authStatus);  
                if (authStatus == 3) {  
                    console.log("相册权限已经开启");  
                } else  {  
                    console.log("相册权限没有开启");  
                }  
                    plus.ios.deleteObject(PHPhotoLibrary);

6、通讯录

判断通讯录权限是否开启

                              var CNContactStore = plus.ios.import("CNContactStore");  
                var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);  
                if (cnAuthStatus == 3) {  
                    console.log("通讯录权限已经开启");  
                } else  {  
                    console.log("通讯录权限没有开启");  
                }  
                    plus.ios.deleteObject(CNContactStore);

7、日历

判断日历权限是否开启

                              var EKEventStore = plus.ios.import("EKEventStore");  
                  var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);  
                if (ekAuthStatus == 3) {  
                    console.log("日历权限已经开启");  
                } else  {  
                    console.log("日历权限没有开启");  
                }  
                    plus.ios.deleteObject(EKEventStore);

8、备忘录

判断备忘录权限是否开启

                              var EKEventStore = plus.ios.import("EKEventStore");  
                  var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);  
                if (ekAuthStatus == 3) {  
                    console.log("备忘录权限已经开启");  
                } else  {  
                    console.log("备忘录权限没有开启");  
                }  
                    plus.ios.deleteObject(EKEventStore);

9、网络

判断网络是否开启

                              var cellularData = plus.ios.newObject("CTCellularData");  
                var state = cellularData.plusGetAttribute("restrictedState");  
                if (state == 2) {  
                    console.log("已经开启了互联网权限:NotRestricted");  
                } else if (state == 0) {  
                    console.log("StateUnknown");  
                } else if (state == 1) {  
                    console.log("Restricted");  
                }  
                    plus.ios.deleteObject(cellularData);

9、蓝牙

判断蓝牙是否开启

判断蓝牙权限是否开启无法用native.js来写,可以用蓝牙模块的API, getBluetoothAdapterState(获取本机蓝牙适配器状态),onBluetoothAdapterStateChange(监听蓝牙适配器状态变化事件)来判断。

提示用户开启权限

跳转到应用的权限页面,下面写了一个提示框让用户去选择

                                      plus.nativeUI.confirm("XX权限没有开启,是否去开启?", function(e) {  
                        if (e.index == 0) {  
                            var UIApplication = plus.ios.import("UIApplication");  
                            var application2 = UIApplication.sharedApplication();  
                            var NSURL2 = plus.ios.import("NSURL");                        
                            var setting2 = NSURL2.URLWithString("app-settings:");  
                            application2.openURL(setting2);  

                            plus.ios.deleteObject(setting2);  
                            plus.ios.deleteObject(NSURL2);  
                            plus.ios.deleteObject(application2);  
                        }  
                    }, {  
                        "buttons": ["Yes", "No"],  
                        "verticalAlign": "center"  
                    });

安卓权限见:https://ask.dcloud.net.cn/article/35861

10 关注 分享
yoggexu 靓崽 skysowe 7***@qq.com 271355477qq 2***@qq.com 7***@qq.com r***@qq.com 流云12138 9157654

要回复文章请先登录注册

易软

易软

回复 CLP :
型号8p ios版本11
2020-11-02 18:02
CLP

CLP (作者)

回复 易软 :
问题确定吗? iOS几? 什么机型?
2020-11-02 17:43
易软

易软

为什么 判断网络权限这个 应用的网络权限开了 也是 state = 0 把应用网络权限禁了 还是 state = 0
2020-11-02 17:40
6***@qq.com

6***@qq.com

写的很好
2020-07-21 10:54
s***@163.com

s***@163.com

回复 2***@qq.com :
大佬 你怎么写的 可以发一下示例吗
2020-07-21 10:28
CLP

CLP (作者)

回复 Eros_Vertigo :
没有
2020-07-02 18:22
Eros_Vertigo

Eros_Vertigo

回复 CLP :
ios 如何用plus申请开启相机权限
2020-07-02 17:49
老哥教教我

老哥教教我

回复 1***@qq.com :
5+吗?https://ext.dcloud.net.cn/plugin?id=594
2020-04-03 19:35
1***@qq.com

1***@qq.com

回复 老哥教教我 :
可以告知吗
2020-04-03 18:12
老哥教教我

老哥教教我

怎么打开定位服务页面???
2019-12-14 18:06