H5W3
当前位置:H5W3 > uniapp技术问答专区 > 正文

Native.js判断系统功能权限大全

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

本文地址:H5W3 » Native.js判断系统功能权限大全

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址