Create IPA from a Cordova project with xcodebuild command - ios

I'm trying to build a system which can compile a project given the source code (the Cordova SDK app project) and the optional provision settings, similiar to PhoneGap's Build. Behind the scenes I try to use the xcodebuild command to create the final IPA and return it to the user. All the projects developed using Cordova and prepared to be compiled with XCode with the following commands:
cordova platform add ios
cordova prepare ios
The problem is that the xcodebuild requires the project's schemes, which according to my search on the subject are generated only when you open the project with the XCode GUI.
Is there any way to generate the schemes for the project using command line tools only? Are the schemes are the same for every project so I can copy a static one to each project I want to compile?
I'm not really a XCode guy or Mac guy for that matter, so I'd be happy for a clear explanation for how and why the solution works, if there is one...
Edit:
Just to clarify my final intestions:
When I open the project using the XCode GUI (double click the .xcodeproj) it generates the schemes and then I can use the xcodebuild command successfully without any problem. But I need this system to be completely automated, so that the user can upload he's project built with the Cordova framework and have this system generate the IPA for him if he choose so. (He can also choose other platforms which are supported by the Cordova framework). Much like PhoneGap's build eventually.

You can generate the schemes programmatically using a build hook script that the Cordova CLI will run before building for iOS. I wrote a blog post on this here but here's a summary:
I chose to use the xcodeproj Ruby gem, you can get this with:
sudo gem install xcodeproj
Then create a hook script "fix_xcode_schemes.rb" in the hooks folder of your Cordova project, set it to 755 file permissions so that it is executable and put this in the script:
#!/usr/bin/env ruby
require 'xcodeproj'
xcproj = Xcodeproj::Project.open("platforms/ios/schemedemo.xcodeproj")
xcproj.recreate_user_schemes
xcproj.save
Adjust the platforms/ios/schemedemo.xcodeproj to match your project name.
Then to run the script edit your project's config.xml and add:
<platform name="ios">
<hook type="after_platform_add" src="hooks/fix_xcode_schemes.rb" />
...
</platform>
Full code and Github on the blog post I linked to. Here I use after_platform_add so the Cordova CLI will add the schemes after the iOS platform is added. For an existing project you may want to swap this for before_prepare or before_build to add the schemes if you don't want to remove and re-add the ios platform to use my original example. Cordova Hook documentation is here.

Related

Can't build Cordova iOS project using CLI but can build/run the Xcode project

Absolutely new to Cordova. These are the steps I've done:
Checked out an existing project
run cordova build ios
The above generates this build error:
(node:10242) UnhandledPromiseRejectionWarning: Error [ERR_UNHANDLED_ERROR]: Unhandled error. (The platform "ios" does not appear to be a valid cordova platform. It is missing API.js. ios not supported.)
I've Googled for a solution to the above and the general consensus seems to be to remove and then add the iOS platform.
Now there's a problem with that. Looking through the commit history, I can see that there have been code changes done to files within the platforms directory so I can't just remove it, or...
Question 1)
Can I?
Question 2)
Is it right to make code changes within the platforms directory? Because it seems like those are generated directories.
Question 3)
Why is it that when I open the Xcode project under platforms/ios, I am able to build/run it fine despite the Cordova CLI error above?
it's not right to manually edit the platform files. And some manual changes might make the project not build, so might be related to the manual changes somebody did. So if you don't want to risk to remove the platform, continue building from Xcode
from root www to platforms/ios is transferred by cordova commands such us cordova prepare ios or cordova build ios or cordova run ios.
So if you can't do a cordova prepare ios because of that error you can try a few things.
Copy your files from www to platforms/ios without replacing the whole folder, just the duplicate files. cordova prepare ios is not just a copy, it's also does some modifications/aditions, but if you don't remove existing files it might work.
Create a new project and copy the platforms/ios/cordova to your own project and see if that fixes the mentioned problem, as that's where the Api.js file is.
Downgrade the Cordova CLI, try to latest 7.x.x and if it doesn't work, try with latest 6.x.x, one of those won't require the Api.js as it relatively new.

Cordova project can't remove plugin after update my project

I am trying to upgrade my Cordova Ionic project ios platform from 3.0.1 to the newest 4.5.2 using xcode 9. I remove the ios platform and re-add it again under CLI 7.1.0. Then I restore all the .xcodeproject settings by copy the old .xcodeproject into the new ios platform and reset all the path necessary in the workspace. Luckily my project build successfully and runs fine in the debug mode.
However when I am trying to upgrade some out of date plugins in the new project, I found that I can only "add" plugin, which everything just goes fine. But when I am trying to remove any plugin (whether they have been added in the new workspace or not), the CLI always show red font message: Error: Could not find *-Info.plist file, or config.xml file.
This message will also show up if I tried to run ionic prepare ios command. Interestingly enough, if I use the .xcodeproject file cordova originally generated, then all the things will just work fine.
Any idea on what specific settings is wrong so that the CLI could only recognize the new .xcodeproject file but not the old .xcodeproject file?
To answer "why don't want to use the new .xcodeproject automatically generated". My project is pretty huge which contains 4 different targets and each of them have different project settings. First of all, CLI don't know that and will only generate one target and I have to duplicate it another 3 times. Secondly, it will be such a pain to restore each target settings one by one. And thirdly the new project doesn't contain any changes in the iOS native code I made such like in appDelegate class. Therefore I just want to find a trick way to make it through.
Cordova creates the files automatically, I believe copying a file generated from an old version and pasting a new one will break your project.
What happens if you remove the platform, add again but without reusing the .xcodeproject? The normal thing is that you got to work normally.
Another option would be to upgrade Cordova or a downgrade, using nvm you can have several versions of Nodejs and Cordova.
You didn't mentioned in your question how you are removing the ios platform , try to do them as follows:
cordova platform remove ios
cordova platform list here you can verify that ios platform deleted
cordova platform add ios --save
I don't know what version you are using but cordova 6.5.0 should solve the ios-build issues. you can install using like below:
sudo npm i -g cordova#6.5.0

How to add plugins for ios in phonegap?

I am new to phonegap.
Using phonegap app I have created new project.
Now I like to use this project in xcode (7.2.1) is it possible?
What is the best way to develop app in phonegap for various plugin ,I was googled and heard about coredova but can't understand exact needed .
For IOS Try this to add any new Plugin.
Go to your Created Project Directory in Mac CLI.
Then do this.
If you haven't added IOS Platform then run below command.
cordova platform add ios
Then build your whole project with below command.
cordova prepare ios
Then to add plugin run below command.
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
Here take whatever plugin you want to install.
Make sure you connected with internet while installing plugin.
Now I like to use this project in xCode (7.2.1) is it possible?*
Yes it is possible, Read this article
What is the best way to develop app in phonegap for various plugin
I am assuming you are asking how to add different plugins?
Create your APP with the PhoneGap Interface (You already did)
Go to the www folder where you stored the PG build files and look for your config.xml, then you will see a section where a few plugins already exists. You can add additional ones by just simply copying and pasting the below XML syntax (Battery Status Plugin), example:
<plugin name="cordova-plugin-battery-status" spec="1.1.0" source="pgb" />
Reference this article for a comprehensive list of PhoneGap's plugins:
https://build.phonegap.com/plugins
Alternatively, you can add them through the Command Line interface (See the first answer to your question)

Where can we place config.xml in IOS phone gap?

Hi I am new to IOS and Xcode. Can we use config.xml in IOS xcode? If we are using where can we place the config.xml. While creating my project I did not get config.xml. Is it will be a problem?
Using the latest versions of cordova tipically update the required files (config.xml) for you when you use the CLI with command
Cordova plugin add
The documentation explain specific tweaks to the config.xml
http://cordova.apache.org/docs/en/3.1.0/config_ref_index.md.html#The%20config.xml%20File
From the doc
For projects created with the Cordova CLI (described in The Command-line Interface The Command-line Interface), this file can be found in the top-level www directory. Using the CLI to build a project regenerates versions of this file in various subdirectories within platforms .Ifyou usetheCLI to create a project, but then shift your workflow to an SDK, the platform-specific file serves as a source.

cordova console log can not be installed

I am trying to install the cordova console.log plugin.
when i am trying to install it using the command line using:
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-console.git
I am running it from the path of the root application path
the structure of the folder is:
projName
|-- --project
|-- projName
|-- cordova
|-- CordovaLib
|-- projName.xcodeproj
|-- plugin
And the error i receive in the command line is
[Error: Current working directory is not a Cordova-based project.]
i should mention that i build the cordova project manually using imports of the frameworks because in the xcode new project window there was not Cordova/Phonegap new project option.
I am using xCode 5.0.1 and cordova 3.1.0
The error you get is self-explanatory, it is because you have not created a cordova project as it must be created. Keep reading to solve the problem.
Cordova basic project preparation steps
Based on my comment and your answer, you should follow the cordova workflow for creating projects using the CLI (command line interface), it is strongly recommended to avoid unnecessary problems, and the steps are not hard to follow.
Please follow the steps described here (official doc), read it fully, it will save you more time later if you read it:
http://cordova.apache.org/docs/en/3.2.0/guide_cli_index.md.html#The%20Command-Line%20Interface
In brief the steps are:
Install node.js
Install cordova using npm
Create a cordova project / app using the CLI
Add the desired platform to the project (android, ios, etc, as needed)
Work on your project
Add cordova plugins as necessary (for example if you need notifications add the notificacion plugin)
Build the project
Test of device or emulator (in your case iPhone, iPad simulator)
iOS Platform Guide
Also check the iOS Platform Guide completely, it is very helpful, explain additional instructions to prepare and work with XCode.
http://cordova.apache.org/docs/en/3.2.0/guide_platforms_ios_index.md.html#iOS%20Platform%20Guide
Plugins
For each plugin in the API reference you will find instructions to install them using the CLI, as well as to "uninstall" them.
Only install the plugins you will need
Just for reference, the code to install notifications-dialogs plugin using the terminal (run the command inside your project folder):
$ cordova plugin rm org.apache.cordova.dialogs
On the other hand, the easier way to solve the problem is create the new project following the noted steps and later just add your existing code or files to the proper folders, as well as add the required plugins, not that difficult.

Resources