uniapp 判断安卓iOS权限

uniapp 判断安卓iOS权限,第1张

let isIos = false;

// #ifdef APP-PLUS
isIos = (plus.os.name === 'iOS')
// #endif

// 判断安卓主方法
function requestAndroidPermission(permissionID, ifRequest) {
	return new Promise((resolve, reject) => {
		plus.android.requestPermissions([permissionID], onSuccess, onError)
		function onSuccess(res) {
			const grantedList = res.granted
			const deniedList = res.deniedPresent
			const deniedAlwaysList = res.deniedAlways
			if(grantedList.includes(permissionID)) {
				resolve(true)
			} else {
				resolve(false)
				ifRequest && gotoAppPermissionSetting()
			}
		}
		function onError(err) {
			reject(err)
		}
	})
}

// 分别判断Ios
function judgeIosPermissionPush(ifRequest) {
	return new Promise(resolve => {
		var UIApplication = plus.ios.import("UIApplication");
		var app = UIApplication.sharedApplication();
		var enabledTypes = 0;
		if (app.currentUserNotificationSettings) {
			var settings = app.currentUserNotificationSettings();
			enabledTypes = settings.plusGetAttribute("types");
			if (enabledTypes == 0) {
				ifRequest && gotoAppPermissionSetting()
				resolve(false)
			} else {
				resolve(true)
			}
			plus.ios.deleteObject(settings);
		} else {
			enabledTypes = app.enabledRemoteNotificationTypes();
			if (enabledTypes == 0) {
				ifRequest && gotoAppPermissionSetting()
				resolve(false)
			} else {
				resolve(true)
			}
		}
		plus.ios.deleteObject(app);
		plus.ios.deleteObject(UIApplication);
	})
}

// 判断定位权限是否开启
function judgeIosPermissionLocation(ifRequest) {
	return new Promise(resolve => {
		var cllocationManger = plus.ios.import("CLLocationManager");
		var status = cllocationManger.authorizationStatus();
		if(status == 2) {
			ifRequest && gotoAppPermissionSetting()
			resolve(false)
		} else {
			resolve(true)
		}
		plus.ios.deleteObject(cllocationManger);
	})
}

// 判断麦克风权限是否开启
function judgeIosPermissionRecord(ifRequest) {
	return new Promise(resolve => {
		var avaudiosession = plus.ios.import("AVAudioSession");
		var avaudio = avaudiosession.sharedInstance();
		var permissionStatus = avaudio.recordPermission();
		if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
			ifRequest && gotoAppPermissionSetting()
			resolve(false)
		} else {
			resolve(true)
		}
		plus.ios.deleteObject(avaudiosession);
	})
}

// 判断相机权限是否开启
function judgeIosPermissionCamera(ifRequest) {
	return new Promise(resolve => {
		var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
		var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
		if (authStatus == 3) {
			resolve(true)
		} else {
			ifRequest && gotoAppPermissionSetting()
			resolve(false)
		}
		plus.ios.deleteObject(AVCaptureDevice);
	})
}

// 判断相册权限是否开启
function judgeIosPermissionPhotoLibrary(ifRequest) {
	return new Promise(resolve => {
		var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
		var authStatus = PHPhotoLibrary.authorizationStatus();
		if (authStatus == 3) {
			resolve(true)
		} else {
			ifRequest && gotoAppPermissionSetting()
			resolve(false)
		}
		plus.ios.deleteObject(PHPhotoLibrary);
	})
}

// 判断通讯录权限是否开启
function judgeIosPermissionContact(ifRequest) {
	return new Promise(resolve => {
		var CNContactStore = plus.ios.import("CNContactStore");
		var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
		if (cnAuthStatus == 3) {
			resolve(true)
		} else {
			ifRequest && gotoAppPermissionSetting()
			resolve(false)
		}
		plus.ios.deleteObject(CNContactStore);
	})
}

// 判断所有权限
function getPermisson(permisson, ifRequest=false) {
	switch (permisson){
		case "location":
			if(isIos) {
				return judgeIosPermissionLocation(ifRequest)
			} else {
				return requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION", ifRequest)
			}
			break;
		case "camera":
			if(isIos) {
				return judgeIosPermissionCamera(ifRequest)
			} else {
				return requestAndroidPermission("android.permission.CAMERA", ifRequest)
			}
			break;
		case "photo":
			if(isIos) {
				return judgeIosPermissionPhotoLibrary(ifRequest)
			} else {
				return requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE", ifRequest)
			}
			break;
		case "record":
			if(isIos) {
				return judgeIosPermissionRecord(ifRequest)
			} else {
				return requestAndroidPermission("android.permission.RECORD_AUDIO", ifRequest)
			}
			break;
		case "contact":
			if(isIos) {
				return judgeIosPermissionContact(ifRequest)
			} else {
				return requestAndroidPermission("android.permission.READ_CONTACTS", ifRequest)
			}
			break;
		case "call":
			if(isIos) {
				return Promise.resolve(true)
			} else {
				return requestAndroidPermission("android.permission.CALL_PHONE", ifRequest)
			}
			break;
		case "push":
			if(isIos) {
				return judgeIosPermissionPush(ifRequest)
			} else {
				return Promise.resolve(true)
			}
			break;
		default:
			break;
	}
}

// 去设置
function gotoAppPermissionSetting() {
	if (isIos) {
		var UIApplication = plus.ios.import("UIApplication");
		var application2 = UIApplication.sharedApplication();
		var NSURL2 = plus.ios.import("NSURL");
		// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");		
		var setting2 = NSURL2.URLWithString("app-settings:");
		application2.openURL(setting2);

		plus.ios.deleteObject(setting2);
		plus.ios.deleteObject(NSURL2);
		plus.ios.deleteObject(application2);
	} else {
		var Intent = plus.android.importClass("android.content.Intent");
		var Settings = plus.android.importClass("android.provider.Settings");
		var Uri = plus.android.importClass("android.net.Uri");
		var mainActivity = plus.android.runtimeMainActivity();
		var intent = new Intent();
		intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
		var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
		intent.setData(uri);
		mainActivity.startActivity(intent);
	}
}

// 检查是否开启了定位
function checkSystemEnableLocation() {
	if (isIos) {
		var result = false;
		var cllocationManger = plus.ios.import("CLLocationManager");
		var result = cllocationManger.locationServicesEnabled();
		plus.ios.deleteObject(cllocationManger);
		return result;
	} else {
		var context = plus.android.importClass("android.content.Context");
		var locationManager = plus.android.importClass("android.location.LocationManager");
		var main = plus.android.runtimeMainActivity();
		var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
		var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
		return result
	}
}

export default {
	getPermisson,
	checkSystemEnableLocation
}

以上是代码封装

以下是使用方式

// 使用
import $P from '../../../utils/zpermission.js'

onShow() {
	$P.getPermisson('location').then(res => {
		console.log(res) // true or false
	})
	const res = $P.checkSystemEnableLocation()
	console.log(res)  // true or false
},

注意: 定位权限返回为真并不意味着可以定位   需要进一步验证是否开启了定位  也就是手机里面的定位开关

安卓iOS的权限判断方法都是用 Promise 封装  使用方法一致   iOS 没有Error(catch)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/990261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存