diff --git a/README.md b/README.md index 16aaedf..a077c98 100644 --- a/README.md +++ b/README.md @@ -71,23 +71,6 @@ cordova plugin add cc.fovea.cordova.openwith \ It shouldn't be too hard. But just in case, I [posted a screencast of it](https://youtu.be/eaE4m_xO1mg). -### iOS Setup - -After having installed the plugin, with the ios platform in place, 1 operation needs to be done manually: setup the App Group on both the Cordova App and the Share Extension. - - 1. open the **xcodeproject** for your application - 1. select the root element of your **project navigator** (the left-side pane) - 1. select the **target** of your application - 1. select **capabilities** - 1. scroll down to **App Groups** - 1. make sure it's **ON** - 1. create and activate an **App Group** called: `group..shareextension` - 1. repeat the previous five steps for the **ShareExtension target**. - -You might also have to select a Team for both the App and Share Extension targets, make sure to select the same. - -Build, XCode might complain about a few things to setup that it will fix for you (creation entitlements files, etc). - ### Advanced installation options If you do not need anything fancy, you can skip this section. diff --git a/hooks/iosAddTarget.js b/hooks/iosAddTarget.js index cd8e204..191f4fc 100755 --- a/hooks/iosAddTarget.js +++ b/hooks/iosAddTarget.js @@ -34,6 +34,8 @@ const BUNDLE_SUFFIX = '.shareextension'; var fs = require('fs'); var path = require('path'); +var packageJson; +var bundleIdentifier; function redError(message) { return new Error('"' + PLUGIN_ID + '" \x1b[1m\x1b[31m' + message + '\x1b[0m'); @@ -93,24 +95,19 @@ function getPreferenceValue(configXml, name) { } function getCordovaParameter(configXml, variableName) { - var variable; - var arg = process.argv.filter(function(arg) { - return arg.indexOf(variableName + '=') == 0; - }); - if (arg.length >= 1) { - variable = arg[0].split('=')[1]; - } else { + var variable = packageJson.cordova.plugins[PLUGIN_ID][variableName]; + if (!variable) { variable = getPreferenceValue(configXml, variableName); } return variable; } // Get the bundle id from config.xml -// function getBundleId(context, configXml) { -// var elementTree = require('elementtree'); -// var etree = elementTree.parse(configXml); -// return etree.getroot().get('id'); -// } +function getBundleId(context, configXml) { + var elementTree = require('elementtree'); + var etree = elementTree.parse(configXml); + return etree.getroot().get('id'); +} function parsePbxProject(context, pbxProjectPath) { var xcode = require('xcode'); @@ -151,8 +148,8 @@ function projectPlistJson(context, projectName) { function getPreferences(context, configXml, projectName) { var plist = projectPlistJson(context, projectName); - var group = "group." + plist.CFBundleIdentifier + BUNDLE_SUFFIX; - if (getCordovaParameter(configXml, 'GROUP_IDENTIFIER') !== "") { + var group = "group." + bundleIdentifier + BUNDLE_SUFFIX; + if (getCordovaParameter(configXml, 'IOS_GROUP_IDENTIFIER')) { group = getCordovaParameter(configXml, 'IOS_GROUP_IDENTIFIER'); } return [{ @@ -160,7 +157,7 @@ function getPreferences(context, configXml, projectName) { value: projectName }, { key: '__BUNDLE_IDENTIFIER__', - value: plist.CFBundleIdentifier + BUNDLE_SUFFIX + value: bundleIdentifier + BUNDLE_SUFFIX } ,{ key: '__GROUP_IDENTIFIER__', value: group @@ -215,6 +212,8 @@ module.exports = function (context) { var Q = require('q'); var deferral = new Q.defer(); + packageJson = require(path.join(context.opts.projectRoot, 'package.json')); + // if (context.opts.cordova.platforms.indexOf('ios') < 0) { // log('You have to add the ios platform before adding this plugin!', 'error'); // } @@ -224,6 +223,8 @@ module.exports = function (context) { configXml = configXml.substring(configXml.indexOf('<')); } + bundleIdentifier = getBundleId(context, configXml); + findXCodeproject(context, function(projectFolder, projectName) { console.log(' - Folder containing your iOS project: ' + iosFolder(context)); @@ -241,7 +242,7 @@ module.exports = function (context) { }); // Find if the project already contains the target and group - var target = pbxProject.pbxTargetByName('ShareExt'); + var target = pbxProject.pbxTargetByName('ShareExt') || pbxProject.pbxTargetByName('"ShareExt"'); if (target) { console.log(' ShareExt target already exists.'); } @@ -249,7 +250,7 @@ module.exports = function (context) { if (!target) { // Add PBXNativeTarget to the project target = pbxProject.addTarget('ShareExt', 'app_extension', 'ShareExtension'); - + // Add a new PBXSourcesBuildPhase for our ShareViewController // (we can't add it to the existing one because an extension is kind of an extra app) pbxProject.addBuildPhase([], 'PBXSourcesBuildPhase', 'Sources', target.uuid); @@ -261,7 +262,7 @@ module.exports = function (context) { // Create a separate PBXGroup for the shareExtensions files, name has to be unique and path must be in quotation marks var pbxGroupKey = pbxProject.findPBXGroupKey({name: 'ShareExtension'}); - if (pbxProject) { + if (pbxGroupKey) { console.log(' ShareExtension group already exists.'); } if (!pbxGroupKey) { @@ -287,6 +288,20 @@ module.exports = function (context) { pbxProject.addResourceFile(file.name, {target: target.uuid}, pbxGroupKey); }); + var configurations = pbxProject.pbxXCBuildConfigurationSection(); + for (var key in configurations) { + if (typeof configurations[key].buildSettings !== 'undefined') { + var buildSettingsObj = configurations[key].buildSettings; + if (typeof buildSettingsObj['PRODUCT_NAME'] !== 'undefined') { + buildSettingsObj['CODE_SIGN_ENTITLEMENTS'] = '"ShareExtension/ShareExtension-Entitlements.plist"'; + var productName = buildSettingsObj['PRODUCT_NAME']; + if (productName.indexOf('ShareExt') >= 0) { + buildSettingsObj['PRODUCT_BUNDLE_IDENTIFIER'] = bundleIdentifier+BUNDLE_SUFFIX; + } + } + } + } + //Add development team and provisioning profile var PROVISIONING_PROFILE = getCordovaParameter(configXml, 'SHAREEXT_PROVISIONING_PROFILE'); var DEVELOPMENT_TEAM = getCordovaParameter(configXml, 'SHAREEXT_DEVELOPMENT_TEAM'); diff --git a/package.json b/package.json index 8d38604..1059368 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "src/android/cc/fovea/openwith/Serializer.java", "src/ios/OpenWithPlugin.m", "src/ios/ShareExtension/MainInterface.storyboard", + "src/ios/ShareExtension/ShareExtension-Entitlements.plist", "src/ios/ShareExtension/ShareExtension-Info.plist", "src/ios/ShareExtension/ShareViewController.h", "src/ios/ShareExtension/ShareViewController.m", diff --git a/plugin.xml b/plugin.xml index 6e7e617..0b538e1 100644 --- a/plugin.xml +++ b/plugin.xml @@ -75,8 +75,8 @@ SOFTWARE. - - + + diff --git a/src/ios/ShareExtension/ShareExtension-Entitlements.plist b/src/ios/ShareExtension/ShareExtension-Entitlements.plist new file mode 100644 index 0000000..93e1fc7 --- /dev/null +++ b/src/ios/ShareExtension/ShareExtension-Entitlements.plist @@ -0,0 +1,10 @@ + + + + + com.apple.security.application-groups + + __GROUP_IDENTIFIER__ + + +