feat: add permission check for geoLocation setting

This commit is contained in:
Sebastian Lange
2019-01-10 13:39:43 +01:00
parent cd5ac500a0
commit d5fa2fd9a5
9 changed files with 336 additions and 336 deletions

View File

@@ -75,12 +75,18 @@
<splash height="960" src="resources/ios/splash/Default@2x~iphone.png" width="640" />
<splash height="480" src="resources/ios/splash/Default~iphone.png" width="320" />
<splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732" />
<edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
<string>The app will use your location to provide features for navigation or distances information.</string>
</edit-config>
</platform>
<plugin name="cordova-plugin-whitelist" spec="1.3.3" />
<plugin name="cordova-plugin-device" spec="2.0.2" />
<plugin name="cordova-plugin-splashscreen" spec="5.0.2" />
<plugin name="cordova-plugin-ionic-webview" spec="2.0.0" />
<plugin name="cordova-plugin-ionic-keyboard" spec="2.0.5" />
<plugin name="cordova-plugin-geolocation" spec="4.0.1">
<variable name="GEOLOCATION_USAGE_DESCRIPTION" value="The app will use your location to provide features for navigation or distances information." />
</plugin>
<engine name="ios" spec="4.5.5" />
<engine name="browser" spec="5.0.4" />
<engine name="android" spec="7.1.4" />

550
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -31,6 +31,7 @@
"@angular/platform-browser-dynamic": "6.1.10",
"@angular/router": "6.1.10",
"@ionic-native/core": "5.0.0-beta.22",
"@ionic-native/geolocation": "5.0.0-beta.22",
"@ionic-native/splash-screen": "5.0.0-beta.22",
"@ionic-native/status-bar": "5.0.0-beta.22",
"@ionic/angular": "4.0.0-beta.13",
@@ -44,6 +45,7 @@
"cordova-browser": "5.0.4",
"cordova-ios": "4.5.5",
"cordova-plugin-device": "2.0.2",
"cordova-plugin-geolocation": "4.0.1",
"cordova-plugin-ionic-keyboard": "2.0.5",
"cordova-plugin-ionic-webview": "2.3.1",
"cordova-plugin-splashscreen": "5.0.2",
@@ -87,7 +89,10 @@
"cordova-plugin-device": {},
"cordova-plugin-splashscreen": {},
"cordova-plugin-ionic-webview": {},
"cordova-plugin-ionic-keyboard": {}
"cordova-plugin-ionic-keyboard": {},
"cordova-plugin-geolocation": {
"GEOLOCATION_USAGE_DESCRIPTION": "The app will use your location to provide features for navigation or distances information."
}
},
"platforms": [
"ios",

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -13,6 +13,8 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Component, Input} from '@angular/core';
import {Geolocation} from '@ionic-native/geolocation/ngx';
import {AlertController} from '@ionic/angular';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {
SCLanguageName,
@@ -32,8 +34,10 @@ export class SettingsItemComponent {
@Input() setting: SCSetting;
constructor(private translateService: TranslateService,
private settingsProvider: SettingsProvider) {
constructor(private alertCtrl: AlertController,
private translateService: TranslateService,
private settingsProvider: SettingsProvider,
private geoLocation: Geolocation) {
this.meta = SCSettingMeta;
this.language = translateService.currentLang as SCLanguageName;
@@ -47,19 +51,80 @@ export class SettingsItemComponent {
}
/**
* handles value changes of the setting
* Checks for user permission to use location,
* if no permission is granted, setting is set to false and an alert is presented to the user
*/
private async checkGeoLocationPermission() {
// request geoLocation to test the user permission
try {
// set enableHighAccuracy, otherwise android platform does not respond
const options = {
enableHighAccuracy: true,
};
await this.geoLocation.getCurrentPosition(options);
} catch (error) {
// if error code is 1 the user denied permission,
// other errors like 'timeout' or 'no location' will be ignored here
if (error.code === 1) {
// ios has special error message for disabled location services, for the setting we ignore it
if (error.message.toLowerCase() !== 'location services are disabled.') {
// revert setting value
this.setting.input.value = false;
await this.presentGeoLocationAlert();
}
}
}
}
/**
* Shows alert with error message on denied user permission or disabled location services
*/
private async presentGeoLocationAlert() {
const title = this.translateService.instant('settings.geoLocation.permission_denied_title');
const message = this.translateService.instant('settings.geoLocation.permission_denied_message');
await this.presentAlert(title, message);
}
/**
* Shows alert with given title and message and a 'ok' button
*
* @param title title of the alert
* @param message message of the alert
*/
async presentAlert(title: string, message: string) {
const alert = await this.alertCtrl.create({
buttons: ['OK'],
header: title,
message: message,
});
await alert.present();
}
/**
* Handles value changes of the setting
*/
async settingChanged(): Promise<void> {
switch (this.setting.name) {
case 'language':
if (this.setting.input.value !== undefined) {
if (typeof this.setting.input.value !== 'undefined'
&& SettingsProvider.validateValue(this.setting, this.setting.input.value)) {
// handle general settings, with special actions
switch (this.setting.name) {
case 'language':
this.translateService.use(this.setting.input.value.toString());
}
break;
default:
break;
case 'geoLocation':
if (this.setting.input.value) {
await this.checkGeoLocationPermission();
}
break;
default:
}
await this.settingsProvider
.setSettingValue(this.setting.categories[0], this.setting.name, this.setting.input.value);
} else {
// reset setting
this.setting.input.value =
await this.settingsProvider.getSettingValue(this.setting.categories[0], this.setting.name);
}
await this.settingsProvider
.setSettingValue(this.setting.categories[0], this.setting.name, this.setting.input.value);
}
typeOf(val: any) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -16,6 +16,7 @@ import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {RouterModule, Routes} from '@angular/router';
import {Geolocation} from '@ionic-native/geolocation/ngx';
import {IonicModule} from '@ionic/angular';
import {TranslateModule} from '@ngx-translate/core';
@@ -39,6 +40,7 @@ const settingsRoutes: Routes = [
RouterModule.forChild(settingsRoutes),
],
providers: [
Geolocation,
],
})
export class SettingsModule {}

View File

@@ -293,7 +293,7 @@ describe('SettingsProvider', () => {
inputType: 'singleChoice',
values: [true, false],
},
name: 'locationService',
name: 'geoLocation',
order: 0,
origin: {
indexed: '2018-09-11T12:30:00Z',

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.

View File

@@ -9,6 +9,10 @@
}
},
"settings": {
"title": "Einstellungen"
"title": "Einstellungen",
"geoLocation": {
"permission_denied_title": "Erlaubnis für Ortungsdienst nicht gegeben",
"permission_denied_message": "Erlaube der App die Nutzung des Ortungsdienstes, um diese Funktion zu aktivieren."
}
}
}

View File

@@ -9,6 +9,10 @@
}
},
"settings": {
"title": "Settings"
"title": "Settings",
"geoLocation": {
"permission_denied_title": "Location permission not granted",
"permission_denied_message": "Allow this app to use location services to activate this feature."
}
}
}