diff --git a/README.md b/README.md index 933c72c..8dfeee2 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,12 @@ recommended). If you want a hook to run before another one, reorder the `` * **function**: Adds `swift` support by adding universal objective c bridge to project. +##### `ios-deeplinks-entitlement.js` + +* **author**: [Nitsnets](http://www.nitsnets.com/) +* **usage**: `` +* **function**: When using `ionic-plugin-deeplinks` and iOS, adds the propper 'Associated Domains' entitlement to the XCode Project. +* **credit**: [@AlvYuste](https://github.com/AlvYuste/), [@ordas](https://github.com/ordas/), [@lpg-mac](https://gist.github.com/lpg-mac) ### Contributing diff --git a/ios_deeplinks_entitlement.js b/ios_deeplinks_entitlement.js new file mode 100644 index 0000000..8d6320c --- /dev/null +++ b/ios_deeplinks_entitlement.js @@ -0,0 +1,46 @@ +#!/usr/bin/env node + +// deeplinks plugins generates entitlements in a way that isn't compatible +// with latest cordova build system, so we do it ourselves. +// +// https://github.com/ionic-team/ionic-plugin-deeplinks/issues/97 + +const fs = require('fs');[] +const plist = require('plist'); + +const PlistKey = 'com.apple.developer.associated-domains'; + +module.exports = function (ctx) { + if (ctx.opts.platforms.indexOf('ios') < 0) { return; } + + const common = ctx.requireCordovaModule('cordova-common'); + const util = ctx.requireCordovaModule('cordova-lib/src/cordova/util'); + + const config = new common.ConfigParser(util.projectConfig(util.isCordova())); + const projectName = config.name(); + const paths = [ + `./platforms/ios/${projectName}/Entitlements-Debug.plist`, + `./platforms/ios/${projectName}/Entitlements-Release.plist` + ]; + const plugin = config.getPlugin('ionic-plugin-deeplinks'); + if (!plugin) { return; } + const domain = [`applinks:${plugin.variables.DEEPLINK_HOST}`]; + + paths.forEach(path => addEntitlementsToFile(path, domain)); +} + +function addEntitlementsToFile(path, domain) { + const origFileContent = fs.readFileSync(path, 'utf8'); + const parsedPlist = plist.parse(origFileContent); + + if (parsedPlist[PlistKey]) { + // give ourselves a chance to notice if/when the plugin gets fixed + // (though this might be too early) + console.warn('Entitlement already exists!', path, parsedPlist[PlistKey]); + } + parsedPlist[PlistKey] = domain; + + const newFileContent = plist.build(parsedPlist); + fs.writeFileSync(path, newFileContent, { encoding: 'utf8' }); +} + diff --git a/package.json b/package.json index 266b331..fabf256 100644 --- a/package.json +++ b/package.json @@ -21,4 +21,4 @@ "xcode": "^0.8.2", "fs-extra": "3.0.1" } -} +} \ No newline at end of file