Add a file to XCode Project (e.g., GoogleService-Info.plist)?
Hi,
I'm wondering if there is a way to add a file to XCode project programmatically? Using copy is not sufficient, as XCode does not automatically recognize the new file in its folder. Thanks!
platforms:
ios:
targets:
App:
copy:
- src: ../firebase/GoogleService-Info.plist
dest: App/GoogleService-Info.plist
I don't know how to do it in the YAML config, but I have done this with by own code.
I have written custom bash executables to automate a lot of the build processes for our Capacitor app. The bash file executes a typescript file based on Node.js.
package.json
"bin": {
"onetouch": "bin/onetouch",
"apkmaker": "bin/apkmaker"
}
onetouch bash file
#!/usr/bin/env node
'use strict';
process.on('unhandledRejection', err => {
console.error('Unhandled Rejection', err);
throw err;
});
import { spawn } from 'child_process';
spawn('tsx', ['bin/cli/onetouch'].concat(process.argv.slice(2)), { cwd: process.cwd(), shell: true, stdio: 'inherit' });
Without sharing too much of the code, in my typescript file, I have some code that looks like this:
const { run } = await import('../commands/build');
await runInit(opts, extras);
await runPlatform(build, opts);
await runCapCommand('sync', opts);
await run(build, opts);
async function runPlatform(build: Build, opts: CommandOpts): Promise<void> {
const { run } = await import('../commands/platform');
await run(build, opts);
}
commands/platform
import { MobileProject } from '@trapezedev/project';
import { Build, CommandOpts } from '../utils/common';
export async function run(build: Build, opts: CommandOpts) {
const project = new MobileProject(process.cwd(), {
android: {
path: 'platforms/android'
},
ios: {
path: 'platforms/ios/App'
}
});
// Load the project configuration
await project.load();
// Check if the android platform has been added
if (opts.android && project.android) {
const { configure } = await import('./platforms/android');
await configure(project.android, build, opts);
}
// Check if the iOS platform has been added
if (opts.ios && project.ios) {
const { configure } = await import('./platforms/ios');
await configure(project, project.ios, build);
}
// Commit the changes
await project.commit();
}
platforms/ios
You need to add "GoogleService-Info.plist" as a Resource file so that it is copied into the packaged app. As of v7.1.3 of trapeze/project there is a bug when using addResourceFile. The xcode package dependency v3.0.1 doesn't do any null checking when looking for a folder called "Resources". You can either manually add this yourself, or my code does it for you. I automated this part so we don't have to commit the platforms folder generated by Capacitor.
The xcode package devs have already fixed the bug, but it is only available in their v3.0.2-nightly build.
this.ios refers to the ios property on the MobileProject class.
async addResourceFile(...items: string[]): Promise<void> {
const pbx = this.ios.getPbxProject();
const pbxGroup = pbx?.pbxGroupByName('Resources');
if (!(pbxGroup && pbxGroup.path)) {
pbx?.addPbxGroup([], 'Resources', 'Resources');
}
const groups = pbx?.hash.project.objects['PBXGroup'] ?? [];
const emptyGroup = Object.entries(groups).find(([key, value]: [string, any]) => {
return value.isa === 'PBXGroup' && typeof value.name === 'undefined'
});
const appTarget = this.ios.getAppTargetName();
const appGroup = Object.entries(groups).find(([key, value]: [string, any]) => {
return value.isa === 'PBXGroup' && (value.name === appTarget || value.path === appTarget);
});
let path, pathSplit;
for (path of items) {
pathSplit = path.split(sep);
if (pathSplit[0] === appTarget && appGroup) {
pbx?.addResourceFile(pathSplit.slice(1).join(sep), {}, appGroup?.[0]);
} else {
pbx?.addResourceFile(path, {}, emptyGroup?.[0]);
}
}
}
await this.addResourceFile(join('App', 'GoogleService-Info.plist'), join('App', 'sound.caf'));