更新记录
1.0.3(2025-01-17) 下载此版本
完善demo
1.0.2(2025-01-16) 下载此版本
完善文档
1.0.1(2025-01-15) 下载此版本
更新文档
查看更多平台兼容性
uni-app
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 | 
|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | - | - | 9.0 | 12 | × | 
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 | 
|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | - | × | × | × | × | 
uni-app x
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 | 
|---|---|---|---|---|---|
| - | - | 9.0 | 12 | × | × | 
permission-handler
介绍
permission-handler 是一款支持iOS/Android 的权限申请插件
增强版permission-handler-plus(支持HarmonyOS Next)
使用方法
import {
    checkPermission, requestPermissions
} from '@/uni_modules/permission-handler';
requestLocation() {
    checkPermission('Location', (result) => {
        console.log(result)
        if (result == "denied") {
            requestPermissions({
                names: ['Location'],
                success: (result) => {
                    console.log(result)
                },
                fail:(error) => {
                    console.log(error)
                }
            })
        }
    })
}
参数及结果说明:
权限状态说明
PermissionStatus: string
| 状态 | iOS | Android | 
|---|---|---|
| denied | 用户拒绝访问所请求的功能,需要先请求权限。 | 用户拒绝访问所请求的功能,需要先请求权限。 | 
| granted | 用户授予了所请求功能的访问权限。 | 用户授予了所请求功能的访问权限。 | 
| restricted | 操作系统拒绝访问所请求的功能,可能是由于启用了家长控制等活动限制。 | 不适用。 | 
| limited | 用户已授权此应用程序进行有限访问(仅支持 iOS 14+)。 | 用户已授权此应用程序进行有限访问(仅支持 Android 14+)。 | 
| permanentlyDenied | 用户拒绝权限,并选择不再显示权限对话框。 用户可在设置中更改权限状态。  | 
Android 11+:用户第二次拒绝权限。 Android 11 以下:用户拒绝并选择不再显示请求。  | 
| provisional | 应用程序被临时授权发送非打扰性用户通知(仅支持 iOS 12+)。 | 不适用。 | 
权限列表说明
name: string
| 状态 | iOS | Android | 
|---|---|---|
| Calendar | 日历(事件) | 日历 | 
| Camera | 照片(相机胶卷和摄像头) | 摄像头 | 
| Contacts | 通讯录 | 通讯录 | 
| Location | CoreLocation(始终和使用时) | 精确位置和模糊位置 | 
| LocationAlways | CoreLocation - 始终 | Android 10+: 后台位置权限 Android 10以下: 精确位置和模糊位置权限  | 
| LocationWhenInUse | CoreLocation - 使用时 | 精确位置和模糊位置 | 
| MediaLibrary | 媒体库(仅支持 iOS 9.3+) | 不适用 | 
| Microphone | 麦克风 | 麦克风 | 
| Phone | 不适用 | 电话状态 | 
| Photos | 照片(iOS 14+ 提供读写访问级别) | Android 13+: photosAndroid 12以下: storage | 
| PhotosAddOnly | 照片(仅限添加,iOS 14+ 提供读写访问级别) | 不适用 | 
| Reminders | 提醒事项 | 不适用 | 
| Sensors | CoreMotion | 身体传感器 | 
| Sms | 不适用 | 发送和读取短信(仅限 Android) | 
| Speech | 语音识别(与麦克风权限不同) | 语音识别(与麦克风权限相同) | 
| Storage | 默认隐式授予(访问 Documents 或 Downloads 文件夹) | Android 13+: 替代为 photos、video 等Android 12以下: 读写外部存储权限  | 
| IgnoreBatteryOptimizations | 不适用 | 忽略电池优化(仅限 Android) | 
| Notification | 推送通知 | 推送通知 | 
| AccessMediaLocation | 不适用 | Android 10+(API 29+):访问共享媒体中的地理位置 | 
| ActivityRecognition | 不适用 | Android 10+(API 29+):活动信息 | 
| Bluetooth | Core Bluetooth 授权状态(iOS 13+) | 总是允许 | 
| ManageExternalStorage | 不适用 | Android 11+(API 30+):广泛访问分区存储中的文件 | 
| SystemAlertWindow | 不适用 | 创建覆盖所有应用的窗口(仅限 Android) | 
| RequestInstallPackages | 不适用 | 安装包的权限(Android Marshmallow+) | 
| AppTrackingTransparency | 应用跟踪状态,广告标识符 | 不适用 | 
| CriticalAlerts | 关键通知 | 不适用 | 
| AccessNotificationPolicy | 不适用 | 访问通知策略(Android Marshmallow+) | 
| BluetoothScan | 不适用 | 扫描蓝牙设备(Android 12+) | 
| BluetoothAdvertise | 不适用 | 广播蓝牙设备(Android 12+) | 
| BluetoothConnect | 不适用 | 连接蓝牙设备(Android 12+) | 
| NearbyWifiDevices | 不适用 | 通过 Wi-Fi 连接附近设备(Android 13+) | 
| Videos | 不适用 | 访问外部存储的视频文件(Android 13+) | 
| Audio | 不适用 | 访问外部存储的音频文件(Android 13+) | 
| ScheduleExactAlarm | 不适用 | 调度精确闹钟(Android 12+) | 
| SensorsAlways | 不适用 | 后台访问设备传感器(Android 13+) | 
| CalendarWriteOnly | 写入日历(与 CalendarFullAccess 相同,仅限 iOS 16 以下) | 不适用 | 
| CalendarFullAccess | 读写日历 | 日历 | 
| Assistant | SiriKit | 无操作 | 
| BackgroundRefresh | 读取后台刷新状态 | 不适用 | 
请求权限 requestPermissions
/// 检查权限 checkPermission
checkPermission('Calendar', (result)=> {
    /// result 权限状态
    console.log(result)
})
请求权限 requestPermissions
/// 请求权限
requestPermissions({
    names: ['Location', 'Calendar'],
    success: (result) => {
        /// 请求结果
        /// result: {
        ///     'Location' : 'granted',
        ///     'Calendar' : 'denied'
        /// }
        console.log(result)
    },
    fail:(error) => {
        /// 请求异常
        /// error: {
        ///     'code' : '错误码',
        ///     'msg' : '错误原因'
        /// }
        console.log(error)
    }
})
注意
- 真机调试 HbuilderX 需要配置各平台运行环境配置
 - 真机调试Android需要自定义基座,除了基座自带权限,添加新的权限后调试需要重新打包新基座,否则相当于没有配置权限,iOS不需要自定义基座。
 - android targetSDK请设置不低于28
 - 未配置权限会直接返回denied
 

                                                                    
                                                                        收藏人数:
                                    
                                                        下载插件并导入HBuilderX
                                                    
                                            下载示例项目ZIP
                                        
                                        赞赏(0)
                                    
                                            
 下载 854
                
 赞赏 8
                
            
                    下载 10685145 
                
                        赞赏 1797 
                    
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                                赞赏
                            
            
京公网安备:11010802035340号