Ba-Permissions 是一款权限申请插件,支持权限使用说明弹窗,满足市场审核需求。支持自定义权限申请,也支持快速申请定位、相机、媒体、文件、悬浮窗等常见权限。
在 script
中引入组件
const permission = uni.requireNativePlugin('Ba-Permissions')
在 script
中调用(示例参考,可根据自己业务和调用方法自行修改)
const permission = uni.requireNativePlugin('Ba-Permissions')
export default {
data() {
return {
msgList: []
}
},
methods: {
request() {//自定义申请权限
let that = this;
permission.request({
permissions: ['android.permission.CAMERA']
},
(res) => {
that.showResult(res)
});
},
isGranted() { //判断一个或多个权限是否全部授予了
let that = this;
permission.isGranted({
permissions: ['android.permission.CAMERA']
},
(res) => {
that.showResult(res)
});
},
getDenied() { //获取没有授予的权限
let that = this;
permission.getDenied({
permissions: ['android.permission.CAMERA', 'android.permission.ACCESS_COARSE_LOCATION',
'android.permission.ACCESS_FINE_LOCATION',
'android.permission.ACCESS_BACKGROUND_LOCATION'
]
},
(res) => {
that.showResult(res)
});
},
goPermissionPage() { //跳转到应用权限设置页
let that = this;
permission.goPermissionPage({
permissions: ['android.permission.CAMERA']
},
(res) => {
that.showResult(res)
});
},
goAppDetails() {//跳转应用详情
let that = this;
permission.goAppDetails(
(res) => {
that.showResult(res)
});
},
reqLocation() {//申请位置权限
let that = this;
permission.reqLocation(
(res) => {
that.showResult(res)
});
},
reqAlertWindow() { //申请悬浮窗权限
let that = this;
permission.reqAlertWindow(
(res) => {
that.showResult(res)
});
},
showResult(res) {
console.log(res);
//this.msgList.unshift(JSON.stringify(res))
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
}
}
}
方法名 | 说明 |
---|---|
request | 自定义权限申请 |
isGranted | 判断一个或多个权限是否全部授予了 |
permission | 获取没有授予的权限 |
getDenied | 跳转到悬浮窗权限页面 |
goPermissionPage | 跳转到应用权限设置页 |
goAppDetails | 跳转应用详情 |
reqLocation | 定位权限 |
reqLocation2 | 定位权限(需要后台) |
reqAlertWindow | 悬浮窗权限 |
reqManageStorage | 申请所有文件读取权限 |
reqReadMediaLocation | 申请读取图片地理信息权限 |
reqBluetooth | 申请蓝牙权限 |
reqNotification | 申请通知权限 |
reqNotifyPolicy | 申请勿扰权限 |
reqNotifyListener | 申请通知栏监听权限 |
reqIgnoreBattery | 申请忽略电池优化权限 |
reqSensors | 传感器 |
reqRecognition | 申请身体活动权限 |
reqInstallPackages | 申请安装包权限 |
reqUsageStats | 申请使用统计权限 |
reqExactAlarm | 申请闹钟提醒权限 |
<!--相机-->
<uses-permission android:name="android.permission.CAMERA" />
<!--悬浮窗-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!--定位权限-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!--传感器-->
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND" />
<!--身体活动-->
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<!--蓝牙权限-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!--图片地理信息-->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--媒体文件读取权限(不适配分区存储)-->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<!--媒体文件读取权限(适配分区存储)-->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
<!--安装包-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!--通知权限-->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.NOTIFICATION_SERVICE" />
<!--勿扰权限-->
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<!--使用统计权限-->
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
<!--闹钟提醒权限-->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<!--忽略电池优化权限-->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />