Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cordova Hook - Removes Ad Framework From Plugin.XML
// This Cordova Hook (script) removes "AdSupport.framework" and "libAdIdAccess" references
// from the cordova-plugin-google-analytics plugin, as well as removes it from
// references found in the corresponding node_module and .project.pbxproj.
// The authoritive source is found at: https://gist.github.com/SteveKennedy
module.exports = function (ctx) {
console.log("Attempting To Remove Ad (IDFA) References from project....")
var pluginName = 'cordova-plugin-google-analytics';
// Reference Dependencies
var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path');
// Define Path to Plugin.XML of cordova-plugin-google-analytics
var pluginFilePath = path.join(ctx.opts.projectRoot, 'plugins/' + pluginName + '/plugin.xml');
var nodeModulesPath = path.join(ctx.opts.projectRoot, 'node_modules/' + pluginName + '/plugin.xml');
var pbxprojFilePath = getIOSProjectPath();
removeAdReferencesFrom(pluginFilePath);
removeAdReferencesFrom(pbxprojFilePath);
removeAdReferencesFrom(nodeModulesPath);
function removeAdReferencesFrom(filePath) {
// Only Do This Only If Plugin.XML is found for this plugin.
fs.stat(filePath, function (error, stat) {
if (error) {
return;
}
// Read In That Plugin.XML File
var data = fs.readFileSync(filePath, 'utf8');
// Split the Contents of That File Into An Array Of Lines
var dataArray = data.split('\n');
// Define Substrings To Eliminate if contained within Each Line
var frameworkString = "AdSupport.framework";
var sourceFileString = "libAdIdAccess";
// Store original length of Array for comparison later
var originalDataArrayLength = dataArray.length;
// Loop Through The Lines and Remove the Line/Item if it contains The Substrings
for (var i = dataArray.length - 1; i >= 0; i--) {
if (dataArray[i].indexOf(sourceFileString) !== -1 ||
dataArray[i].indexOf(frameworkString) !== -1) {
console.log('Removing line:' + dataArray[i]);
dataArray.splice(i, 1);
}
}
// If Nothing Was Found/Removed/Spliced, No Need To Overwrite.
if (originalDataArrayLength === dataArray.length) {
console.log("No Changes to File: " + filePath);
return;
}
// Rejoin the Array Into A String
var newData = dataArray.join("\r\n");
// Overwrite the File with Modified Contents
fs.writeFileSync(filePath, newData, 'utf8');
});
}
function getIOSProjectPath() {
var common = ctx.requireCordovaModule('cordova-common');
var util = ctx.requireCordovaModule('cordova-lib/src/cordova/util');
var projectName = new common.ConfigParser(util.projectConfig(util.isCordova())).name();
var projectPath = './platforms/ios/' + projectName + '.xcodeproj/project.pbxproj';
return projectPath;
}
};
@kim3er

This comment has been minimized.

Copy link

@kim3er kim3er commented Apr 4, 2019

Firstly, such a useful script thanks for sharing!

As of the latest release, line 13 should now read:

  var fs = require('fs'),
    path = require('path');

As requireCordovaModule can no longer be used to require non Cordova modules.

@olivierschmid

This comment has been minimized.

Copy link

@olivierschmid olivierschmid commented Apr 30, 2019

Yes - with Cordova 9 line 13 and 14 must be changed as described by kim3er.
Thanks for sharing!

@fdambrosio

This comment has been minimized.

Copy link

@fdambrosio fdambrosio commented Jul 14, 2019

thank you

@amcastror

This comment has been minimized.

Copy link

@amcastror amcastror commented Apr 25, 2020

Didn't work for me, it appears that the plugin has changed. I get this output:

No Changes to File: /Users/mati/ionic/pucara-ionic-cacerones/plugins/cordova-plugin-google-analytics/plugin.xml
No Changes to File: ./platforms/ios/Pucara_Caserones.xcodeproj/project.pbxproj
No Changes to File: /Users/mati/ionic/pucara-ionic-cacerones/node_modules/cordova-plugin-google-analytics/plugin.xml

I had to add this after line 41
var googleIDFASupport = "GoogleIDFASupport";

And make sure that the the for loop at line 47 searched that keyword also.

After that, it worked again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.