Skip to content

Commit

Permalink
(ios) Add isMobileDataEnabled() to core module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Alden committed Oct 4, 2022
1 parent 20acb90 commit 5a2da5f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ios/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern NSString*const AUTHORIZATION_LIMITED;
- (void) getCurrentBatteryLevel: (CDVInvokedUrlCommand*)command;
- (void) getDeviceOSVersion: (CDVInvokedUrlCommand*)command;
- (void) getBuildOSVersion: (CDVInvokedUrlCommand*)command;
- (void) isMobileDataEnabled: (CDVInvokedUrlCommand*)command;

// Utilities
+ (id) getInstance;
Expand Down
17 changes: 17 additions & 0 deletions src/ios/Diagnostic.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#import "Diagnostic.h"
#import <CoreTelephony/CTCellularData.h>

@implementation Diagnostic

Expand All @@ -29,7 +30,9 @@ @implementation Diagnostic
static NSString*const CPU_ARCH_X86 = @"X86";
static NSString*const CPU_ARCH_X86_64 = @"X86_64";

// Internal properties
static Diagnostic* diagnostic = nil;
static CTCellularData* cellularData;

/********************************/
#pragma mark - Public static functions
Expand Down Expand Up @@ -111,6 +114,7 @@ - (void)pluginInitialize {

self.debugEnabled = false;
self.osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
cellularData = [[CTCellularData alloc] init];
}

// https://stackoverflow.com/a/38441011/777265
Expand Down Expand Up @@ -212,6 +216,19 @@ - (void) getBuildOSVersion: (CDVInvokedUrlCommand*)command {
}];
}

- (void) isMobileDataEnabled: (CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
@try {
bool isEnabled = cellularData.restrictedState == kCTCellularDataNotRestricted;;
[diagnostic sendPluginResultBool:isEnabled :command];
}
@catch (NSException *exception) {
[diagnostic handlePluginException:exception :command];
}
}];
}


/********************************/
#pragma mark - Send results
Expand Down
16 changes: 16 additions & 0 deletions www/ios/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ var Diagnostic = (function(){
[]);
};

/**
* Checks if mobile data is enabled on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if mobile data is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isMobileDataEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isMobileDataEnabled',
[]);
};

/**
* Returns details of the OS of the device on which the app is currently running
*
Expand Down

0 comments on commit 5a2da5f

Please sign in to comment.