赞
踩
uni-app是一个可以混编多端多平台的开发框架,不过相对于原生还是略有不便,比如对于权限的管理,不太方便去查询权限是否授权以及主动申请权限。基于这个情况,于是开发了一款 IOS和Android原生插件来满足需求。
<template> <view> <view class="title">IOS+Android统一检查权限:</view> <view class="test_flex_row_between"> <text class="btn bg_green" @click="checkPermission('WRITE')">检查读写权限</text> <text class="btn bg_green" @click="checkPermission('CAMERA')">检查摄像头权限</text> <text class="btn bg_green" @click="checkPermission('LOCATION')">检查定位权限</text> </view> <view class="title">IOS+Android统一检查权限:</view> <view class="test_flex_row_between"> <text class="btn bg_red" @click="requestPermission('WRITE')">申请读写权限</text> <text class="btn bg_red" @click="requestPermission('CAMERA')">申请摄像头权限</text> <text class="btn bg_red" @click="requestPermission('LOCATION')">申请定位权限</text> </view> <view class="title">Android检查权限:</view> <view class="test_flex_row_between"> <text class="btn bg_blue" @click="checkPermissionAndroid('android.permission.WRITE')">检查读写权限</text> <text class="btn bg_blue" @click="checkPermissionAndroid('android.permission.CAMERA')">检查摄像头权限</text> <text class="btn bg_blue" @click="checkPermissionAndroid('android.permission.ACCESS_FINE_LOCATION')">检查定位权限</text> </view> <view class="title">Android申请权限:</view> <view class="test_flex_row_between"> <text class="btn bg_red" @click="requestPermissionAndroid('android.permission.WRITE')">申请读写权限</text> <text class="btn bg_red" @click="requestPermissionAndroid('android.permission.CAMERA')">申请摄像头权限</text> <text class="btn bg_red" @click="requestPermissionAndroid('android.permission.ACCESS_FINE_LOCATION')">申请定位权限</text> </view> <view class="title">前往系统权限页面手动设置:</view> <view class="test_flex_row_between"> <text class="btn bg_zise" @click="gotoSetting">前往系统授权页面</text> </view> </view> </template> <script> const permissionObj = uni.requireNativePlugin("LuanQing-Permission"); export default { methods: { // 前往设置页面 手动配置应用权限 gotoSetting(){ permissionObj.gotoPermissionSettingPage(); }, // 统一权限检查 checkPermission(name){ let data = permissionObj.checkPermission({permission:name}); console.error("检查完毕:",data); }, // Android特有权限检查 checkPermissionAndroid(name){ let data = permissionObj.checkPermissionAndroid({permission:name}); console.error("Android端原生检查完毕:",data); }, // 统一权限申请 requestPermission(name){ console.error("统一申请权限:",name); permissionObj.requestPermission({permission:name},(res=>{ console.error("权限申请成功嘞",res); }), (res=>{ console.error("权限申请失败嘞",res); })); }, // Android特有权限申请 requestPermissionAndroid(name){ permissionObj.requestPermissionAndroid({permission:name},(res=>{ console.error("Android端原生权限申请成功嘞",res); }), (res=>{ console.error("Android端原生权限申请失败嘞",res); })); }, } } </script> <style> @import url('test.css'); </style>
为了方便android开发者的开发习惯,特别提供了基于原生android权限名称的方法requestPermissionAndroid,直接传入android端的权限名称即可,不局限于统一权限。
private JSONObject doCheck(boolean isNative,JSONObject options){ UniLogUtils.e("是否是对象:",options.toJSONString()); String permissionStr = options.getString("permission"); String permission = isNative ? permissionStr : permissionMap.get(permissionStr); if(permission == null || "".equals(permission)){ JSONObject data = new JSONObject(); data.put("code",0); data.put("message","权限检查异常:未找到该权限:"+permission); UniLogUtils.e("检查权限模块返回:"+data.toJSONString()); return data; } boolean isHas = ContextCompat.checkSelfPermission(mUniSDKInstance.getContext(), permission) == PackageManager.PERMISSION_GRANTED; JSONObject data = new JSONObject(); data.put("code", isHas ? "1":"2"); data.put("message",isHas ? "用户已授予权限!":"用户未授予权限"); UniLogUtils.e("检查权限模块返回:"+data.toJSONString()); return data; }
IOS端因为需要和android端统一,因此只能使用两端统一的权限模块名称。
// 统一检查权限 UNI_EXPORT_METHOD_SYNC(@selector(checkPermission:)) - (NSDictionary *) checkPermission:(NSDictionary *) options{ NSLog(@"进入检查权限API %@",options); NSString *permission = [options valueForKey:@"permission"]; if(permission){ Boolean isHas = false; // 相册访问权限 if([@"WRITE" isEqualToString:permission]){ isHas = [self checkPhoto]; } // 相机权限 else if([@"CAMERA" isEqualToString:permission]){ isHas = [self checkCameraAudi:1]; } // 麦克风权限 else if([@"AUDIO" isEqualToString:permission]){ isHas = [self checkCameraAudi:2]; } // 定位权限 else if([@"LOCATION" isEqualToString:permission]){ isHas = [self checkLocatio]; } // 通讯录 else if([@"READ_CONTACTS" isEqualToString:permission]){ isHas = [self checkContacts]; } // 日历 else if([@"CALENDAR" isEqualToString:permission]){ isHas = [self checkCalendarAndMemorial:EKEntityTypeEvent]; } // 备忘录 else if([@"REMINDER" isEqualToString:permission]){ isHas = [self checkCalendarAndMemorial:EKEntityTypeReminder]; } // 蓝牙 else if([@"BLUETOOTH" isEqualToString:permission]){ isHas = [self checkBluetooth]; } NSNumber* code = [NSNumber numberWithInt:(isHas ? 1 : 2)]; NSString* mes = isHas ? @"用户已授予权限":@"用户未授予权限"; NSDictionary *data = [NSDictionary dictionaryWithObjects:@[code,mes] forKeys:@[@"code",@"message"]]; NSLog(@"进入检查权限API10 %@",data); return data; }else{ NSNumber* code = [NSNumber numberWithInt: 0]; NSDictionary *data = [NSDictionary dictionaryWithObjects:@[code , @"尚未选择检查的权限"] forKeys:@[@"code",@"message"]]; NSLog(@"进入检查权限API11 %@",data); return data; } }
链接: 前往下载插件和示例代码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。