简体   繁体   中英

XCode Build not updating JS and HTML

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.

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.

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.

Other answers in this thread either didn't work for me or screwed up cordova plugins eg 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

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.

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

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

@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/"

simply type:

cordova prepare

on your terminal and run your project again in Xcode

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)

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!

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.

我发现有时候Cordova会感到困惑......你需要做一个“cordova platform rm ios”(只需单独留下你的插件),然后做一个“cordova平台添加iOS”,它将把你所有的插件重新安装到你的平台上。 ..然后再次尝试你的构建。

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM