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

Just to make the plugin work (more easily) on ios #97

Merged
merged 6 commits into from
Mar 17, 2020
Merged
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
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<YOUR_APP_BUNDLE_ID>.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.
Expand Down
51 changes: 33 additions & 18 deletions hooks/iosAddTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -151,16 +148,16 @@ 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 [{
key: '__DISPLAY_NAME__',
value: projectName
}, {
key: '__BUNDLE_IDENTIFIER__',
value: plist.CFBundleIdentifier + BUNDLE_SUFFIX
value: bundleIdentifier + BUNDLE_SUFFIX
} ,{
key: '__GROUP_IDENTIFIER__',
value: group
Expand Down Expand Up @@ -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');
// }
Expand All @@ -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));
Expand All @@ -241,15 +242,15 @@ 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.');
}

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);
Expand All @@ -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) {
Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ SOFTWARE.
</config-file>

<hook type="before_plugin_install" src="hooks/npmInstall.js" />
<hook type="before_plugin_install" src="hooks/iosCopyShareExtension.js" />
<hook type="after_plugin_add" src="hooks/iosAddTarget.js" />
<hook type="before_prepare" src="hooks/iosCopyShareExtension.js" />
<hook type="after_prepare" src="hooks/iosAddTarget.js" />
<hook type="before_plugin_uninstall" src="hooks/iosRemoveTarget.js" />

<framework src="MobileCoreServices.framework" />
Expand Down
10 changes: 10 additions & 0 deletions src/ios/ShareExtension/ShareExtension-Entitlements.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>__GROUP_IDENTIFIER__</string>
</array>
</dict>
</plist>