Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mobile data permission #478

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Cordova diagnostic plugin [![Latest Stable Version](https://img.shields.io/npm/v
- [isWifiEnabled()](#iswifienabled)
- [setWifiState()](#setwifistate)
- [switchToWifiSettings()](#switchtowifisettings)
- [Mobile Data module](#mobile-data-module)
- [isMobileDataAuthorized()](#ismobiledataauthorized)
- [Camera module](#camera-module)
- [isCameraPresent()](#iscamerapresent)
- [isCameraAvailable()](#iscameraavailable)
Expand Down Expand Up @@ -2105,6 +2107,39 @@ Displays WiFi settings to allow user to enable WiFi.

cordova.plugins.diagnostic.switchToWifiSettings();

## Mobile Data module

Purpose: get state of mobile data permission

Platforms: iOS

Configuration name: `MOBILE_DATA`

### isMobileDataAuthorized()

Platforms: iOS

Checks if mobile data is authorized for this app.
On iOS this returns true if the per app Mobile Data setting is set to enabled (regardless of whether it's connected to a network).

cordova.plugins.diagnostic.isMobileDataAuthorized(successCallback, errorCallback);

#### Parameters

- {Function} successCallback - The callback which will be called when operation is successful.
This callback function is passed a single boolean parameter which is TRUE if per app Mobile Data is enabled.
- {Function} errorCallback - The callback which will be called when operation encounters an error.
The function is passed a single string parameter containing the error message.


#### Example usage

cordova.plugins.diagnostic.isMobileDataAuthorized(function(authorized){
console.log("Mobile data for this app is " + (authorized ? "authorized" : "not authorized"));
}, function(error){
console.error("The following error occurred: "+error);
});

## Camera module

Purpose: Camera functionality to capture images / record video
Expand Down
16 changes: 16 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@
<header-file src="src/ios/Diagnostic_Wifi.h" />
<source-file src="src/ios/Diagnostic_Wifi.m" />
<!--END_MODULE WIFI-->

<!--BEGIN_MODULE Mobile Data-->
<config-file target="config.xml" parent="/*">
<feature name="Diagnostic_Mobile_Data">
<param name="ios-package" value="Diagnostic_Mobile_Data" />
<param name="onload" value="true" />
</feature>
</config-file>

<js-module src="www/ios/diagnostic.mobileData.js" name="Diagnostic_Mobile_Data">
<merges target="cordova.plugins.diagnostic.mobileData" />
</js-module>

<header-file src="src/ios/Diagnostic_Mobile_Data.h" />
<source-file src="src/ios/Diagnostic_Mobile_Data.m" />
<!--END_MODULE Mobile Data-->

<!--BEGIN_MODULE CAMERA-->
<config-file target="config.xml" parent="/*">
Expand Down
3 changes: 2 additions & 1 deletion scripts/apply-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const MODULES = [
"MOTION",
"NFC",
"EXTERNAL_STORAGE",
"AIRPLANE_MODE"
"AIRPLANE_MODE",
"MOBILE_DATA"
];

const COMMENT_START = "<!--";
Expand Down
22 changes: 22 additions & 0 deletions src/ios/Diagnostic_Mobile_Data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Diagnostic_Mobile_Data.h
* Diagnostic Plugin - Mobile Data Module
*
* Copyright (c) 2018 Working Edge Ltd.
* Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
*/

#import <CoreTelephony/CTCellularData.h>

#import <Cordova/CDV.h>
#import <Cordova/CDVPlugin.h>
#import "Diagnostic.h"


@interface Diagnostic_Mobile_Data : CDVPlugin

@property (nonatomic) CTCellularData* cellularInfo;

- (void) isMobileDataAuthorized: (CDVInvokedUrlCommand*)command;

@end
53 changes: 53 additions & 0 deletions src/ios/Diagnostic_Mobile_Data.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Diagnostic_Mobile_Data.m
* Diagnostic Plugin - Mobile Data Module
*
* Copyright (c) 2018 Working Edge Ltd.
* Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
*/

#import <CoreTelephony/CTCellularData.h>

#import "Diagnostic_Mobile_Data.h"

@implementation Diagnostic_Mobile_Data

// Internal reference to Diagnostic singleton instance
static Diagnostic* diagnostic;

// Internal constants
static NSString*const LOG_TAG = @"Diagnostic_Mobile_Data[native]";

- (void)pluginInitialize {

[super pluginInitialize];

diagnostic = [Diagnostic getInstance];
_cellularInfo = [[CTCellularData alloc] init];
}

/********************************/
#pragma mark - Plugin API
/********************************/

- (void) isMobileDataAuthorized: (CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
@try {
[diagnostic sendPluginResultBool:[self isMobileDataAuthorized] :command];
}
@catch (NSException *exception) {
[diagnostic handlePluginException:exception :command];
}
}];
}

/********************************/
#pragma mark - Internals
/********************************/

- (BOOL) isMobileDataAuthorized {
return _cellularInfo.restrictedState == kCTCellularDataNotRestricted;
}

@end
21 changes: 21 additions & 0 deletions www/ios/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,27 @@ var Diagnostic = (function(){
}
};

/*****************
* Mobile Data *
****************/

/**
* Checks if mobile data is allowed for this app.
* On iOS this returns true if the per app Mobile Data setting is set to enabled (regardless of whether it's connected to a network).
*
* @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 per app 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.isMobileDataAuthorized = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.mobileData){
cordova.plugins.diagnostic.mobileData.isMobileDataAuthorized.apply(this, arguments);
}else{
throw "Diagnostic Mobile Data module is not installed";
}
};

/***************
* Bluetooth *
***************/
Expand Down
62 changes: 62 additions & 0 deletions www/ios/diagnostic.mobileData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* globals cordova, require, exports, module */

/**
* Diagnostic Mobile Data plugin for iOS
*
* Copyright (c) 2015 Working Edge Ltd.
* Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
**/
var Diagnostic_Mobile_Data = (function(){
/***********************
*
* Internal properties
*
*********************/
var Diagnostic_Mobile_Data = {};

var Diagnostic = require("cordova.plugins.diagnostic.Diagnostic");

/********************
*
* Public properties
*
********************/

/********************
*
* Internal functions
*
********************/

/*****************************
*
* Protected member functions
*
****************************/

/**********************
*
* Public API functions
*
**********************/

/**
* Checks if mobile data is allowed for this app.
* On iOS this returns true if the per app Mobile Data setting is set to enabled (regardless of whether it's connected to a network).
*
* @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 per app 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_Mobile_Data.isMobileDataAuthorized = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic_Mobile_Data',
'isMobileDataAuthorized',
[]);
};

return Diagnostic_Mobile_Data;
});
module.exports = new Diagnostic_Mobile_Data();