From 74e1d97939a3f7d3d14424761100d4506b55afa6 Mon Sep 17 00:00:00 2001 From: Alexandr Dascal Date: Thu, 27 Jul 2017 18:25:08 +0300 Subject: [PATCH] add switchToLocationSettings to iOS --- README.md | 6 +++--- src/ios/Diagnostic.h | 1 + src/ios/Diagnostic.m | 28 ++++++++++++++++++++++++++++ www/ios/diagnostic.js | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 360ca1e..3719d1b 100755 --- a/README.md +++ b/README.md @@ -23,8 +23,8 @@ Cordova diagnostic plugin [![Latest Stable Version](https://img.shields.io/npm/v - [isWifiEnabled()](#iswifienabled) - [isCameraAvailable()](#iscameraavailable) - [isBluetoothAvailable()](#isbluetoothavailable) - - [Android and Windows 10 Mobile only](#android-and-windows-10-mobile-only) - [switchToLocationSettings()](#switchtolocationsettings) + - [Android and Windows 10 Mobile only](#android-and-windows-10-mobile-only) - [switchToMobileDataSettings()](#switchtomobiledatasettings) - [switchToBluetoothSettings()](#switchtobluetoothsettings) - [switchToWifiSettings()](#switchtowifisettings) @@ -407,8 +407,6 @@ This callback function is passed a single string parameter containing the error console.error("The following error occurred: "+error); }); -## Android and Windows 10 Mobile only - ### switchToLocationSettings() Displays the device location settings to allow user to enable location services/change location mode. @@ -417,6 +415,8 @@ Displays the device location settings to allow user to enable location services/ Note: On Android, you may want to consider using the [Request Location Accuracy Plugin for Android](https://github.com/dpa99c/cordova-plugin-request-location-accuracy) to request the desired location accuracy without needing the user to manually do this on the Location Settings page. +## Android and Windows 10 Mobile only + ### switchToMobileDataSettings() Displays mobile settings to allow user to enable mobile data. diff --git a/src/ios/Diagnostic.h b/src/ios/Diagnostic.h index 3bc4804..ed49386 100644 --- a/src/ios/Diagnostic.h +++ b/src/ios/Diagnostic.h @@ -63,6 +63,7 @@ - (void) isRegisteredForRemoteNotifications: (CDVInvokedUrlCommand*)command; - (void) switchToSettings: (CDVInvokedUrlCommand*)command; +- (void) switchToLocationSettings: (CDVInvokedUrlCommand*)command; - (void) isMicrophoneAuthorized: (CDVInvokedUrlCommand*)command; - (void) getMicrophoneAuthorizationStatus: (CDVInvokedUrlCommand*)command; diff --git a/src/ios/Diagnostic.m b/src/ios/Diagnostic.m index 41f1423..1206e73 100644 --- a/src/ios/Diagnostic.m +++ b/src/ios/Diagnostic.m @@ -334,6 +334,34 @@ - (void) switchToSettings: (CDVInvokedUrlCommand*)command } } +- (void) switchToLocationSettings: (CDVInvokedUrlCommand*)command +{ + CDVPluginResult* pluginResult; + @try { + if (UIApplicationOpenSettingsURLString != nil){ + NSURL *url; + +#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_8_0 + url = [NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]; +#else + url = [NSURL URLWithString: @"App-Prefs:root=Privacy&path=LOCATION"]; +#endif + + if (![[UIApplication sharedApplication] canOpenURL:url]) { + NSLog(@"cannot open phone settings, check the URL schema for iOS11"); + } + [[UIApplication sharedApplication] openURL:url]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + }else{ + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported below iOS 8"]; + } + } + @catch (NSException *exception) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:exception.reason]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + #pragma mark - Audio - (void) isMicrophoneAuthorized: (CDVInvokedUrlCommand*)command { diff --git a/www/ios/diagnostic.js b/www/ios/diagnostic.js index 36bb2df..b83e40e 100644 --- a/www/ios/diagnostic.js +++ b/www/ios/diagnostic.js @@ -92,6 +92,22 @@ var Diagnostic = (function(){ []); }; + /** + * Switch to location settings + * + * @param {Function} successCallback - The callback which will be called when switch to settings is successful. + * @param {Function} errorCallback - The callback which will be called when switch to settings encounters an error. + * This callback function is passed a single string parameter containing the error message. + * This works only on iOS 8+. iOS 7 and below will invoke the errorCallback. + */ + Diagnostic.switchToLocationSettings = function(successCallback, errorCallback) { + return cordova.exec(successCallback, + errorCallback, + 'Diagnostic', + 'switchToLocationSettings', + []); + }; + /************ * Location * ************/