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

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.

Related

Ionic app loads using Ionic serve but not with Xcode

I'm currently stuck on a problem related to my Ionic project.
I have an Ionic project on GitHub which i've been working on in the past few weeks, i have a few Cordova plugins as part of the project.
When downloading the project from GitHub, i can run the app using Ionic Serve without a problem. However, when trying to setup the project for xCode to run on local device(s), i run into a multitude of errors
Some include:
When running ionic 'cordova platform add ios' the following error appears
Error: Could not install from "ios" as it does not contain a package.json file.
When trying to run 'ionic cordova run ios -l --external'
Error: Error: terminating with uncaught exception of type NSException
I have not yet tried to transfer my code into a new Ionic project, as i wish to see if my problem can be solved without that hassle.
View project code here: https://github.com/designsheikh/bitsandbobs.git
I figured it out. You have the exact same issue I've had with the android platform.
As I pointed it out in the comments, it is indeed caused by a "corrupted" project architecture.
How it happened
Initially, I had an issue with the cordova generated resources, somehow not being copied into the android platform directory. I tried plenty of things with the cordova-res CLI, including using the --copy and the --ios-project flags to force the copy to happen in the directory of my choice.
I couldn't manage to fix my issue, and then decided to rollback all my changes and start over on a clean project to try something else. Since then, I was not able to add the android platform anymore. Every single time, the following error was being thrown :
Error: Could not install from "android" as it does not contain a package.json file.
Explanation
With all my testing, I noticed that when using cordova to add the platform or copy the resources over, somehow, it had created the android platform in the wrong directory, at the root of my project instead of in the platforms folder. And that's where the issue comes from.
I can't explain it, but you will encounter this error every single time if you try to add a platform with cordova while having a folder with this platform name at the root of your project. I have no idea why this would prevent a platform from being added to the project, maybe this folder is used as a temp folder by the CLI.
Solution
The fix is to simply remove any ios or android directory at the root of your project.
The error will go away and you will be able to add the platform again.
#CodingJunkie
Regarding the Github repository you provided in your post, you will likely run into another error while trying to add the ios platform if you use cordova#>=9.0.0.
Using "requireCordovaModule" to load non-cordova module "xcode" is not supported.
Instead, add this module to your dependencies and use regular "require" to load it.
Check this issue#1033 for a workaround. However, it's not recommended to directly edit the plugin source code, you might want to look for another cordova Firebase plugin.
I also noticed you kinda mixed up cordova and capacitor in your project.
Make sure to use one or the other to avoid potential errors.
Hope this helped !
#Reqven
Thank you for your support! - i was sure that i had done this step while trying to fix the issue. It is such an absurd problem, it seems that there is a new project architecture as you mentioned which needs to be followed. I followed your steps with also one minor additional step as im working on the iIOS platform:
Delete iOS folder
cordova platform add ios
cordova prepare ios
ionic build
If step 2 throws an error causing the version not to be found or integrated into the project run - include '#latest' this also solved my problem

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

‘Cordova/CDV.h’ file not found in Xcode 8.2.1

I have one hybrid Cordova project, which is running fine Xcode 5.1.1, but when I am trying to Archive the project in Xcode 8.2.1 with ‘Generic iOS Device’ I am getting the error ‘Cordova/CDV.h’ is not found.
I read many articles about this problem, but none could solve my problem. Most answers are telling to follow the below steps:
"Add this line to your Build Settings -> Header Search Paths:
$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include
"
I have did this but problem not solved, getting another error ‘ld: library not found for -lcrt1.3.1.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)’
Also I have changed the bitcode YES to NO in target Build Settings, but no fruitful result.
Project cordova version 3.4.0 where installed cordova version in machine 6.4.0
Has anybody experience with this kind of problem?
Please, can anyone help? I've been working on this for days.
I got it work by following below steps
In your Xcode project settings select TARGETS
Within TARGETS select the Build Settings tab
In the sub-options under the Build Settings tab be sure to select All
Scroll down to the Search Paths section
Locate Header Search Paths
Add the following configuration to the Release option (as a new value on its own line): $(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include
Clean the project: Command + Shift + K
Restart Xcode
I wanted to report that the response provided by Vinay worked for me. I had to make sure I was in the share extension target, and I added it to the path exactly as suggested above in the Release option, and did not even have to do a clean to get it to regenerate the PCH.
BUT, I was puzzled by this after looking at the setting further and I realized this path was already in the setting. I edited it again, and removed the redundant path, hit 'run' again, and it loaded onto my iPhone and ran fine.
So, I believe that just setting this in the search path forced a recompile on the share extension PCH and all is working again.
I hope this helps others.
I know its too late to answer on this question but still many developers facing the above issue and as per the latest Cordova & ionic version below steps will work in the above error.
As per new update in Cordova some time due to some permission issue or directly run or build iOS platform it's unable to add below file or folders in your iOS project platform :
CordovaLib
so somehow above folder will not add in your project and that's why it's not building your iOS project.
To overcome this issue you need to remove iOS platform from your ionic project and again add it.
Remove iOS platform: sudo ionic cordova platform remove ios
Add it again: sudo ionic cordova platform add ios
By doing this cordova download CordovaLib folder from server and add it into your iOS project and after that users can able to build their project without any error.
recently I faced this issue and I solved by doing the above steps.
Hope this helps others.
Happy coding!
Seems like the "Cordova" folder isn't getting copied into the project during cordova platform add ios
So, you'll have to copy the "Cordova" source folder into your project manually. But you'll have to download the ios source packages from github first, which is located at:
https://github.com/apache/cordova-ios
After downloading the "cordova-ios-master.zip" package and unzipping it, copy the folder named "CordovaLib" into your project next to the Xcode project file:
PROJECT_FOLDER/platforms/ios/PROJECT_NAME
PROJECT_FOLDER/platforms/ios/PROJECT_NAME.xcodeproj
PROJECT_FOLDER/platforms/ios/PROJECT_NAME.xcworkspace
PROJECT_FOLDER/platforms/ios/cordova
PROJECT_FOLDER/platforms/ios/CordovaLib <------- here
PROJECT_FOLDER/platforms/ios/ios.json
PROJECT_FOLDER/platforms/ios/platform_www
PROJECT_FOLDER/platforms/ios/pods-debug.xcconfig
PROJECT_FOLDER/platforms/ios/pods-release.xcconfig
PROJECT_FOLDER/platforms/ios/www

Missing dependency target "CordovaLib (from CordovaLib.xcodeproj)"

Missing dependency target "CordovaLib (from CordovaLib.xcodeproj)" When running ios with Xcode
A different solution
Before reading this MAKE SURE you are opening YourProject.xcworkspace and NOT YourProject.xcodeproj
I couldn't get this to work using #manzapanza 's solution
The Problem
CordovaLib.xcodeproj is missing from your xcode project
Solution (for mac users)
Download a copy of CordovaLib.xcodeproj and drag it into your xcode project.
$ sudo npm install -g cordova (installs a "global" copy of cordova to your computer, you might be able to skip this step)
$ find ~/ -name 'CordovaLib.xcodeproj' (mine was at /Users/Jackson//.cordova/lib/npm_cache/cordova-ios/4.3.1/package/tests/spec/unit/fixtures/ios-config-xml/CordovaLib/CordovaLib.xcodeproj)
Open folder the folder path found above $ open [path] (open /Users/Jackson//.cordova/lib/npm_cache/cordova-ios/4.3.1/package/tests/spec/unit/fixtures/ios-config-xml/CordovaLib/)
Drag CordovaLib.xcodeproj to your xcode project
This is what your file navigator should look like at this point:
Delete red (missing) file
Select your app in the navigator (very top of file tree). Go to Targets > [Your Project Name] > Build Phases > Link Binary With Libraries > "+"
type in / look for libCordova.a and add it
Compile project
For some reasons the xcode project was corrupted. Generally, to solve this kind of problems, you can try removing and re-adding the platform ios:
ionic platform rm ios
ionic platform add ios
ionic build ios
Attention: Any changes that you applied directly from xcode will be lost and you will need to apply manually after you will have run these commands. Do a backup of platforms/ios before.
Same solution for Apache Cordova project.
Remove platform and re-add:
cordova platform remove ios
cordova platform add ios
cordova prepare ios
cordova run ios
Successful!
I downgraded my Cordova version to 6.2.0 and it started working.
I had multiple issues with my app that I was able to resolve when I was missing CordovaLib from my XCode project.
First I ran
cordova requirements ios
and found that I was missing ios-deploy and CocoaPods. These were not requirements for Cordova 6.2.0 but are as of Cordova 6.4.0. I did not try 6.3.0 so I do not know the status for that version.
In addition, I had some icon files in my config.xml for iOS that did not exist because I was using a 3rd party cordova-icon maker to generate them and the fact that they were missing was apparently breaking the add platform command mid-process. Again 6.2.0 did not have this issue with the same config file, but 6.4.0 is definitely strict about not having any errors. My guess is that they were either continuing past the errors in prior version of Cordova or they re-arranged the way things are built so that the CordovaLib is now last and if there are any errors before that step, you will be missing the folder and its contents.

how add manually a cordova plugin to ios existing project

i've a cordova project created via cordova cli command.
i build the project for Android and iOS platform but... i copied the build project into respective workspace (eclipse/xcode) and i updated that single project.
Now i need to add a plugin.
The project into cordova project folder is now so outdate so i would add the plugin via cli and copy the plugin to the respective project.
I copied successfully the generated plugin into the android project, but i'm not able to do the same thing with iOS.
For example i added the console plugin for iOS
cordova plugin add org.apache.cordova.console
if i open and run the build project it work and i see my message in console, but if i try to copy the generated plugin into my existing project it doesn't work.
nobody could help me to understand how add manually a plugin into my existing project?
update
yes i know, i'll try to explain better
i have my cordova project under folder /Document/cordova
when i made an ios build via command line cordova put the built project under /Document/cordova/platforms/ios
well i copied the folder ios under /workspace/my-project-name
after some work under workspace i see that i need to add a cordova plugin.
i added the plugin via command line and it was added to /Document/cordova/platforms/ios
at this poin i have a project under /Document with plugin but asset outdated and one project updated without plugin under /workspace
i'm trying to copy from built project to /workspace project.
i copied
/workspace/my-project-name/my-project-name/Plugins
/workspace/my-project-name/my-project-name/config.xml
/workspace/my-project-name/www/plugins
/workspace/my-project-name/www/cordova.js
/workspace/my-project-name/www/cordova_plugins.js
but it doesn't work... i missed somethings?
After reading it several times I think I get more less the idea...
Well, I am not sure why do you want to copy the ios project outside the current location. I suppose you have a good reason to do it, however you can always tweak that project instead of going back and forward, cordova will not impact non cordova related files each time you run "cordova build".
Are you familiar with control versioning? Git, SVN, mercurial? If so it is simple just we which files were impacted once you add the plugin and copy those... If no, I suggest using control versioning anyways to help keep track of your code changes.
NOTE: Be aware if you build the cordova projet and then you just want to update the files noted above it is highly likely that it won't work, that is why you run "build" is not a simple file sync. I recommend taking the new cordova project with the plugin added and then just add you personal tweaks on the ios in the other folder. Or just tweak the ios folder inside platforms folder...
I hope you are doing for good reason. Anyway you are missing to copy below file to work
/workspace/my-project-name/my-project-name/platforms/ios/my-project-name/config.xml
Copying the cordova iOS platform folder is not a good idea. The reason is, cordova builds an Xcode project in the platform folder, and there is some sort of caching/built in references to absolute location of the original Xcode project. Therefore it would be unclear to you and to Xcode which file is being referenced from within Xcode. This may have caused some other unnoticed errors in the project as well. So therefore, instead of keeping original project path available along with the new path, I would recommend renaming the original project folder in order to remove the ambiguity. This way, you will assure that the right file versions are referenced.

Resources