19

I am using XCode 5.0.2 and Cordova 3.4.0-0.1.3 - What I find is after creating the project using the Cordova CLI and opening in XCode, no changes to the index.html file and index.js file are ever carried over to the simulator when I click run. I have to open terminal and issues a Cordova Build command and then run the simulator and it works

I followed all the instructions here:

Phonegap - developing and launching app on simulator

xcode 4 + phonegap ... not update JS upon build?

And none of it works! any one a have a solution to this, because having to switch back and forth is becoming a pain.

Community
  • 1
  • 1
ksliman
  • 595
  • 1
  • 8
  • 21

15 Answers15

12

You can add a pre-action script to your XCode project's build. To do this:

  1. Select Product > Scheme > Edit Scheme from the menu (or ⌘ < on keyboard)
  2. Select Build > Pre-actions from the left
  3. Click + and select "New Run Script Action"
  4. Add a script like this:

    cd /path/to/your/cordova/project/
    cordova prepare ios > xcode-prepare-results.txt
    

Now XCode should always run cordova prepare before building your project so you don't have to jump to terminal. You can see the output of prepare in the file xcode-prepare-results.txt.

Note, that depending on how your cordova executable is set up and which shell you use, you might have to either change the shell or modify your PATH in order for the script to find cordova.

ville
  • 350
  • 1
  • 5
  • 1
    No Go, this did not work either! It creates the xcode-prepare-results.txt file but there is nothing in the file. I wonder if it is an xcode problem or a PhoneGap/Cordova problem. – ksliman Mar 10 '14 at 12:50
  • worked for me. more generic; cd ${SRCROOT} cd .. cordova prepare ios – kaya Feb 04 '17 at 22:00
11

So after much searching I seem to have found a solution that works, here is what I did. After looking at other Stackoverflow questions I found someone that said this worked for them.

Find the file called copy-www-build-step.sh.

Mine was in [project_folder]/platforms/ios/cordova/lib/copy-www-build-step.sh

In that file, find the lines beginning rsync -a "...

Add -c to the rsync lines, so they ready rsync -a -c "...

Well I tried that and it did not work on its own. I also tried the answer from Ville and that pulled closer but no cigar. Finally I took what the command from Ville and put it in the copy-www-build-step.sh file

so my top line is now

cd /path/to/your/cordova/project/
cordova prepare

SRC_DIR="www/"
DST_DIR="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/www"
COPY_HIDDEN=
ORIG_IFS=$IFS
IFS=$(echo -en "\n\b")
.....
.....
.....etc etc 

And now I make change , and click run , bam all is updated. I hope this helps someone else.

ksliman
  • 595
  • 1
  • 8
  • 21
  • 1
    For me, just added a 'cd $SRCROOT/../..' line worked. I wish cordova can add it by default. – Gordon Sun Jun 21 '14 at 10:17
  • 4
    DO NOT USE THE SOLUTION OR THE ONE I SAID ABOVE!!! They both causes a lot of strange problems. In my case, it causes the inappbrowser plugin not properly deployed to the simulator or real device. I think we should stick with `cordova build` – Gordon Sun Jun 27 '14 at 08:16
  • @GordonSun you are right. while it does keep the code up to date, it fails to deploy the cordova plugins properly. how did you solve it? – Yossi Shasho Nov 25 '14 at 14:41
  • This is an excellent fix! in V 6.2 of Cordova, the fix coding is a little different as it's now running via node. The fix in this case is to add the following lines of code after the shell variable is defined: shell.cd('~/Documents/GitHub/Cognitive-Adviser/iscaa'); shell.exec('cordova prepare'); – Bob Dill Jun 02 '16 at 14:37
7

Other answers in this thread either didn't work for me or screwed up cordova plugins e.g. InAppBrowser, so i finally came up with this:

Edit the file copy-www-build-step.sh and add the following row in the beginning:

cp -fR ../../www/ www/

so it should look like:

...
cp -fR ../../www/ www/ # new code

SRC_DIR="www/"
...

This way your code will be updated properly and your plugins will work

Yossi Shasho
  • 3,632
  • 31
  • 47
5

I also edited the copy-www-build-step.sh file, however you don't want to use an absolute path from your User folder. If you are working with other developers you would have to change that every time you check out code.

It's not a big deal, just change:

SRC_DIR="www/"

To:

SRC_DIR="../../www/"

UPDATE Worked for me on Cordova and Phonegap.

Ryan Ore
  • 1,315
  • 17
  • 23
5

To have the source files automatically copied from the www source directory to the platforms/ios/www directory when you click the Run button in XCode:

In XCode, choose Product->Scheme->Edit Scheme...
Expand the triangle for Build->Pre-actions
Click the "+" to create a new Pre-action

You can leave the "Shell" setting blank.
Set "Provide build settings from" to the project you are building. This is important.

In the script area enter:

cd ${PROJECT_DIR}/../..
echo "--- Start ---" > xcode-prepare-ios-results.txt
echo "Running cordova prepare ios command..."
pwd >> xcode-prepare-ios-results.txt
cordova prepare ios --verbose >> xcode-prepare-ios-results.txt
echo "--- Finished ---"  >> xcode-prepare-ios-results.txt

This works for me with XCode 8.2 and Apache Cordova 6.x

CodeWeenie
  • 51
  • 1
  • 1
2

I suggest you clean before build your xcode project. And one more thing, make sure you build again your project using cordova build, not inside xcode because it's totally different.

cordova build yourproject
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • Yeah, I understand that, but why the extra mile, I thought adding the run-script would take care of having to go to terminal and type "cordova build yourproject" but using ville's answer I tried adding the prepare command and build an neither seem to do anything. For now every time I want to run my project on the sim or in my device I have to switch to a terminal window and type the CLI build command for my project otherwise no changes ever get sent to the device or simulator – ksliman Mar 12 '14 at 19:07
  • Yes,you absolutely correct. You need to switch CLI to build the project again before entering xcode. That's how cordova works. – Nurdin Mar 12 '14 at 19:10
2

@Ksliman I modified you code a bit to work on my system and make slight bit more generic.

top of file copy-www-build-step.sh

## New Code Begin ###

cd ../../
PATH=${PATH}:/usr/local/bin
cordova prepare ios

## New Code End ###

SRC_DIR="www/"
Karman De Lange
  • 531
  • 5
  • 6
  • That's great, glad it worked for you. I have since left Cordova for native tools, its a much steeper learning curve. – ksliman May 14 '14 at 15:54
  • DO NOT USE THE SOLUTION OR THE ONE I SAID ABOVE!!! They both causes a lot of strange problems. In my case, it causes the inappbrowser plugin not properly deployed to the simulator or real device. I think we should stick with cordova build – Gordon Sun Jun 27 '14 at 08:22
2

simply type:

cordova prepare

on your terminal and run your project again in Xcode

armnov
  • 641
  • 1
  • 7
  • 16
  • This always worked for me until recently. I think it was because of a botched attempt to add Windows as a platform and abandoning it. – Trip Sep 14 '16 at 18:43
2

Simple solutions is to build the app with cordova using:

sudo cordova build ios

Then go to xcode > Product > Clean

Then go to xcode > Run (Play Button)

Alex Harper
  • 201
  • 1
  • 2
  • 8
2

Run "cordova prepare ios" during the building phases:

  1. Open Xcode.
  2. Select your project.
  3. Go to "Build Phases".
  4. Go on "+" to add a "New Run Script Phase".
  5. Move this section before "Copy www directory".
  6. Add the following lines (for Mac):
    • cd /Users/*/YOUR_PROJECT_FOLDER (Change your project folder)
    • /Users/*/.nvm/versions/node/v?.?.?/bin/node node_modules/cordova/bin/cordova prepare ios (Change the path to node version)

After "Play" the www-folder will automatically refresh!

Sbat
  • 21
  • 1
1

All I had to do was to include the full absolute src path for my project. For example:

  1. From your project directory, vim platforms/ios/cordova/lib/copy-www-build-step.sh
  2. change SRC_DIR to the absolute path of your www, SRC_DIR="/Users/michael/Documents/Development/SampleMobileApp/www/"
  3. Save, build, and you should be good to go.
Michael Bordash
  • 1,072
  • 1
  • 10
  • 20
0

I found that sometimes Cordova gets confused... you need to do a "cordova platform rm ios" (just leave your plugins alone), and then do a "cordova platform add iOS", which will reinstall all your plugins into your platform... then try your build again.

0

Every time I use cordova build its overrides content in my platform specific www file. So I use cordova compile instead.

From help:

compile <platforms> compiles platform project without preparing it

It is important to just compile the changes not to build entire projects especially if you creating mobile app for many platforms and you have to do some platform specific improvements.

Navidot
  • 327
  • 3
  • 10
0

For me, there was an error in my code. I simply needed to run npm start to locate it.

Trip
  • 26,756
  • 46
  • 158
  • 277
0

If you are directly changing html from the Xcode, then you should change the code/html of the staging folder, not main folder

In the staging folder you will get same structure which you have in the main folder, but it will reflected directly in the xcode (iOS) build

Staging folder

Note: The changes you made in the xcode will be replace when you build again from the cordova build/run, so please copy your changes in the main folder before doing firing cordova commands

You can get more information from this answer: Purpose of Staging folder in PhoneGap 3.4? Only changes to index.html in this folder get recognized?

This is the correct solution taken from:

Cordova + Xcode 7.3 (html + Javascript) not changed or updated when build and emulate

KJD
  • 1