mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat: Permissions API
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
<Button text="vector-image" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="visibility-vs-hidden" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="fs-helper" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="permissions" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
</StackLayout>
|
||||
</ScrollView>
|
||||
</StackLayout>
|
||||
|
||||
47
apps/toolbox/src/pages/permissions.ts
Normal file
47
apps/toolbox/src/pages/permissions.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Observable, EventData, Page, Permissions, Trace } from '@nativescript/core';
|
||||
|
||||
let page: Page;
|
||||
|
||||
export function navigatingTo(args: EventData) {
|
||||
page = <Page>args.object;
|
||||
page.bindingContext = new PermissionsModel();
|
||||
Trace.enable();
|
||||
Trace.setCategories(Trace.categories.Permissions);
|
||||
}
|
||||
|
||||
export class PermissionsModel extends Observable {
|
||||
permissions = ['location', 'camera', 'microphone', 'photo', 'contacts', 'event', 'reminder', 'bluetooth', 'bluetoothScan', 'notification', 'backgroundRefresh', 'speechRecognition', 'mediaLibrary', 'motion', 'location', 'callPhone', 'readSms', 'receiveSms'].map((v) => {
|
||||
return {
|
||||
name: v,
|
||||
checkPermission: this.checkPermission.bind(this),
|
||||
requestPermission: this.requestPermission.bind(this),
|
||||
};
|
||||
});
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
async checkPermission(args) {
|
||||
const perm = args.object.bindingContext.name;
|
||||
try {
|
||||
console.log('checkPermission', perm);
|
||||
const result = await Permissions.check(perm, { type: 'none' });
|
||||
alert(JSON.stringify(result));
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
alert(err);
|
||||
}
|
||||
}
|
||||
async requestPermission(args) {
|
||||
const perm = args.object.bindingContext.name;
|
||||
try {
|
||||
console.log('requestPermission', perm);
|
||||
const result = await Permissions.request(perm, { type: 'none' });
|
||||
alert(JSON.stringify(result));
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
alert(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
apps/toolbox/src/pages/permissions.xml
Normal file
16
apps/toolbox/src/pages/permissions.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
|
||||
<Page.actionBar>
|
||||
<ActionBar title="Permissions" icon="" class="action-bar">
|
||||
</ActionBar>
|
||||
</Page.actionBar>
|
||||
|
||||
<ListView items="{{ permissions }}">
|
||||
<ListView.itemTemplate>
|
||||
<GridLayout columns="*,auto,10,auto" padding="10" class="item" orientation="horizontal">
|
||||
<Label text="{{ name }}" fontSize="17" verticalAlignment="center" />
|
||||
<Button col="1" text="check" fontSize="17" verticalAlignment="center" tap="{{ checkPermission }}" />
|
||||
<Button col="3" text="request" fontSize="17" verticalAlignment="center" tap="{{ requestPermission }}" />
|
||||
</GridLayout>
|
||||
</ListView.itemTemplate>
|
||||
</ListView>
|
||||
</Page>
|
||||
Reference in New Issue
Block a user