提交 05f988a0 编写于 作者: DCloud-yyl's avatar DCloud-yyl

开源uni.getAppAuthorizeSetting

上级 4310d2de
{
"id": "uni-getAppAuthorizeSetting",
"displayName": "uni-getAppAuthorizeSetting",
"version": "1.0.0",
"description": "uni-getAppAuthorizeSetting",
"keywords": [
"uni-getAppAuthorizeSetting"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"uni-ext-api": {
"uni": {
"getAppAuthorizeSetting": {
"name": "getAppAuthorizeSetting",
"app": {
"js": false,
"kotlin": true,
"swift": false
}
}
}
},
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uni-getAppAuthorizeSetting
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[UTS 组件插件](https://uniapp.dcloud.net.cn/plugin/uts-component.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts)
\ No newline at end of file
import { GetAppAuthorizeSetting, GetAppAuthorizeSettingResult } from "../interface.uts";
import UTSAndroid from 'io.dcloud.uts.UTSAndroid';
import PackageManager from 'android.content.pm.PackageManager';
import Manifest from 'android.Manifest';
import NotificationManagerCompat from 'androidx.core.app.NotificationManagerCompat';
export const getAppAuthorizeSetting : GetAppAuthorizeSetting = function () : GetAppAuthorizeSettingResult {
const context = UTSAndroid.getUniActivity()!!;
const cameraPermission = [Manifest.permission.CAMERA];
const cameraGranted = UTSAndroid.checkSystemPermissionGranted(context, cameraPermission);
let cameraResult = cameraGranted ? "authorized" : "denied";
if (!cameraGranted && !hasDefinedInManifest(Manifest.permission.CAMERA)) {
cameraResult = "config error";
}
const locationPermission = [Manifest.permission.ACCESS_COARSE_LOCATION];
const coarseLocationGranted = UTSAndroid.checkSystemPermissionGranted(context, locationPermission);
let coarseLocationResult = coarseLocationGranted ? "authorized" : "denied";
if (!coarseLocationGranted && !hasDefinedInManifest(Manifest.permission.ACCESS_COARSE_LOCATION)) {
coarseLocationResult = "config error";
}
const fineLocationPermission = [Manifest.permission.ACCESS_FINE_LOCATION];
const fineLocationGranted = UTSAndroid.checkSystemPermissionGranted(context, fineLocationPermission);
let accuracy = coarseLocationGranted ? "reduced" : "unsupported";
if (coarseLocationGranted && fineLocationGranted) {
accuracy = "full";
}
const recordAudioPermission = [Manifest.permission.RECORD_AUDIO];
const recordAudioGranted = UTSAndroid.checkSystemPermissionGranted(context, recordAudioPermission);
let recordAudioResult = recordAudioGranted ? "authorized" : "denied";
if (!recordAudioGranted && !hasDefinedInManifest(Manifest.permission.RECORD_AUDIO)) {
recordAudioResult = "config error";
}
const compat = NotificationManagerCompat.from(context);
const notificationResult = compat.areNotificationsEnabled() ? "authorized": "denied"
let result : GetAppAuthorizeSettingResult = {
cameraAuthorized: cameraResult,
locationAuthorized: coarseLocationResult,
locationAccuracy: accuracy,
microphoneAuthorized: recordAudioResult,
notificationAuthorized: notificationResult,
albumAuthorized: null,
bluetoothAuthorized: null,
locationReducedAccuracy: null,
notificationAlertAuthorized: null,
notificationBadgeAuthorized: null,
notificationSoundAuthorized: null,
phoneCalendarAuthorized: null
}
return result
};
const hasDefinedInManifest = function (permission : string) : boolean {
try {
const context = UTSAndroid.getAppContext()!!;
const packageInfo = context.getPackageManager().getPackageInfo(context.getApplicationInfo().packageName, PackageManager.GET_PERMISSIONS);
if (packageInfo != null) {
for (const requestPermission in packageInfo.requestedPermissions) {
if (permission == requestPermission) {
return true;
}
}
}
} catch (e : Exception) {
return false
}
return false;
}
\ No newline at end of file
export interface Uni {
/**
* getAppAuthorizeSetting()
* @description
* 获取 APP 授权设置。
* @param {void}
* @return {GetAppAuthorizeSettingResult}
* @tutorial http://uniapp.dcloud.io/api/system/getappauthorizesetting
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "4.4",
* "uniVer": "√",
* "unixVer": "3.9+"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
* @example
```typescript
uni.getAppAuthorizeSetting()
```
*/
getAppAuthorizeSetting(): GetAppAuthorizeSettingResult;
}
export type GetAppAuthorizeSetting = () => GetAppAuthorizeSettingResult;
export type GetAppAuthorizeSettingResult = {
/**
* 允许 App 使用相册的开关(仅 iOS 支持)
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* @type 'authorized' | 'denied' | 'not determined'
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
albumAuthorized: string | null,
/**
* 允许 App 使用蓝牙的开关(仅 iOS 支持)
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: Android平台没有该值;iOS平台:表示没有在 `manifest.json -> App模块配置` 中配置 `BlueTooth(低功耗蓝牙)` 模块
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
bluetoothAuthorized: string | null,
/**
* 允许 App 使用摄像头的开关
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: Android平台:表示没有授予 `android.permission.CAMERA` 权限;iOS平台没有该值
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
*/
cameraAuthorized: string,
/**
* 允许 App 使用定位的开关
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: Android平台:表示没有授予 `android.permission.ACCESS_COARSE_LOCATION` 权限;iOS平台:表示没有在 `manifest.json -> App模块配置` 中配置 `Geolocation(定位)` 模块
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
*/
locationAuthorized: string,
/**
* 定位准确度。true 表示模糊定位,false 表示精确定位
* - reduced: 模糊定位
* - full: 精准定位
* - unsupported: 不支持(包括用户拒绝定位权限和没有在 `manifest.json -> App模块配置` 中配置 `Geolocation(定位)` 模块)
* @type 'reduced' | 'full' | 'unsupported'
*/
locationAccuracy: string | null,
/**
* 定位准确度(推荐使用 locationAccuracy 属性)。true 表示模糊定位,false 表示精确定位(仅 iOS 支持)
* @type boolean
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
locationReducedAccuracy: boolean | null,
/**
* 允许 App 使用麦克风的开关
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: Android平台:表示没有授予 `android.permission.RECORD_AUDIO` 权限;iOS平台没有该值
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
*/
microphoneAuthorized: string,
/**
* 允许 App 通知的开关
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: Android平台没有该值;iOS平台:表示没有在 `manifest.json -> App模块配置` 中配置 `Push(推送)` 模块
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
*/
notificationAuthorized: string,
/**
* 允许 App 通知带有提醒的开关(仅 iOS 10.0+ 支持)
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: 没有在 `manifest.json -> App模块配置` 中配置 `Push(推送)` 模块
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "10.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
notificationAlertAuthorized: string | null,
/**
* 允许 App 通知带有标记的开关(仅 iOS 10.0+ 支持)
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: 没有在 `manifest.json -> App模块配置` 中配置 `Push(推送)` 模块
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "10.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
notificationBadgeAuthorized: string | null,
/**
* 允许 App 通知带有声音的开关(仅 iOS 10.0+ 支持)
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* - config error: 没有在 `manifest.json -> App模块配置` 中配置 `Push(推送)` 模块
* @type 'authorized' | 'denied' | 'not determined' | 'config error'
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "10.0",
* "uniVer": "√",
* "unixVer": "x"
* }
* }
* }
*/
notificationSoundAuthorized: string | null,
/**
* 允许读写日历的开关(仅微信小程序支持)
* - authorized: 已经获得授权,无需再次请求授权
* - denied: 请求授权被拒绝,无法再次请求授权;(此情况需要引导用户打开系统设置,在设置页中打开权限)
* - not determined: 尚未请求授权,会在App下一次调用系统相应权限时请求;(仅 iOS 会出现。此种情况下引导用户打开系统设置,不展示开关)
* @type 'authorized' | 'denied' | 'not determined'
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* }
* }
* }
*/
phoneCalendarAuthorized: string | null
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册