Skip to content

Commit

Permalink
add switchToLocationSettings to iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
adascal committed Jul 27, 2017
1 parent 3bf1084 commit 74e1d97
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/ios/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
- (void) isRegisteredForRemoteNotifications: (CDVInvokedUrlCommand*)command;

- (void) switchToSettings: (CDVInvokedUrlCommand*)command;
- (void) switchToLocationSettings: (CDVInvokedUrlCommand*)command;

- (void) isMicrophoneAuthorized: (CDVInvokedUrlCommand*)command;
- (void) getMicrophoneAuthorizationStatus: (CDVInvokedUrlCommand*)command;
Expand Down
28 changes: 28 additions & 0 deletions src/ios/Diagnostic.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
16 changes: 16 additions & 0 deletions www/ios/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
************/
Expand Down

0 comments on commit 74e1d97

Please sign in to comment.