14 Replies Latest reply on Aug 16, 2016 11:22 PM by hexrdesign

    iOS isn't executing script




      I made a phonegap app for both iOS and Android. The android version is executing the script after a button is pressed, thus updating inks in tabs. I exported that to an apk and it still works. Using the phonegap developer app on iOS, I press the button but the page just refreshes. It seems as though it isn't executing the script at all. Any ideas as to why this is happening?


      Thanks in advance

        • 1. Re: iOS isn't executing script
          VectorP Level 4

          Would be helpful to see your code.

          - What button?

          - How do you catch the click event?

          - What does the callback function look like?

          - Exactly where does the code fail to being executed? Did you do a line-by-line debug?

          - Do you receive an error message?


          And above all: why do you think this may be a Phonegap problem?

          • 2. Re: iOS isn't executing script
            hexrdesign Level 1

            The following two images are from my index.html





            Take note.js for example. The goal of it is to capture some text that is inside the textarea and display it. I'm using materialize and this works perfectly fine on android.


            Here is the notes.js



            • 3. Re: iOS isn't executing script
              kerrishotts Adobe Employee

              A few thoughts:


              1. Does the console.log() / toast ever get executed?

              2. The implementation of pv.saveNote might be an important detail; please share If you don't mind.

              3. You're loading some CSS from a CDN; don't! You'll run into problems if your user's device is offline (or has no/little/bad connectivity). Keep a copy of those styles local to the device.

              4. Load your local scripts AFTER cordova.js

              5. Wait for deviceready before starting the app

              6. Use DRY (don't repeat yourself)


              Re #6, I'd write it more like this (ignore extra lines; the text editor is a bit funky when it comes to those...):


              // you must wait for deviceready before using anything that uses
              // cordova
              document.addEventListener("deviceready", onDeviceReady, false);
              function onDeviceReady() {
                  // always use strong equality operators where you can so as to avoid
                  // unexpected type coercion
                  if (pv.getOption("autosave") === true) {
                      const textareaEl = $(".materalize-textarea");
                  if (localStorage.getItem("notes") === null) {
              function toast(str) {
                  Materialize.toast(str, 5000);
              function saveNote() {
                  const block = $(this).attr("data-block");
                  // If you're going ES6, you may as well go all the way and use
                  // template strings rather than concatenation
                  const note = $(`#block-${block}-notes`).val();
                  console.log(block,note); // is this logging anything to the console?
                                          // if not, try alert() and see if you get the
                                          // note appearing in an alert dialog
                  toast("Saved");          // this will be a problem should you ever
                                          // go international. Consider using something
                                          // amenable to translation and localization.
              window.onerror = function(...args) {
                  // see if an error is being generated somehwere
                  // alert should fire before refresh (hopefully)
              • 4. Re: iOS isn't executing script
                hexrdesign Level 1

                Could this be because I am testing this on the PhoneGap Developers App for iOS?

                • 5. Re: iOS isn't executing script
                  kerrishotts Adobe Employee

                  Could be. When encountering a problem like this, always try to replicate using a full build (PGB or CLI).

                  • 6. Re: iOS isn't executing script
                    hexrdesign Level 1

                    I did a full build for android and it works perfectly. I followed all the signing instructions for iOS and waited for the build. Phonegap Build builds the iOS app and i can download the .ipa file but when i try scanning the bar code, it doesn't work. Any ideas as to why?

                    • 7. Re: iOS isn't executing script
                      kerrishotts Adobe Employee

                      Ok -- define "doesn't work". Does it exhibit the same refresh behavior, or does it just sit there and do nothing? If the latter case, you should try to connect to the JavaScript console using Safari if you have a Mac, or put in a lot of alert() invocations so that you can track where things are failing.

                      • 8. Re: iOS isn't executing script
                        hexrdesign Level 1

                        By doesn't work I mean that scanning the qr code opens up a link in safari. It thens prompts me to download the app. I click yes and it starts installation. It installs all the way and then it just gives me an error message that its failed to install.

                        • 9. Re: iOS isn't executing script
                          kerrishotts Adobe Employee

                          When did we move from editing notes to scanning QR codes?

                          • 10. Re: iOS isn't executing script
                            hexrdesign Level 1

                            Hi there!


                            I think we got a little off topic!


                            Getting back to the point, we were worried that the reason iOS isn't executing scripts is because it wasn't packaged as an app. Everything worked perfectly on Android, with the testing, build and execution, but something is off with iOS. We're not sure if it's the way we're doing the scripts, or it's something specific to iOS. If it helps, you can find the source on GitHub.


                            Thanks for your help so far!

                            • 11. Re: iOS isn't executing script
                              RonSSA Level 1

                              Hi hexrdesign, I'm a noob so take this for what it's worth, sorry if I get some of the terminology wrong.  I found some of the functionalities don't work fully in pg developer like opening a tel: URL or Mailto:.  To do a build and load it to your IOS device you need to use a development certificate.  When I tried using a distribution certificate I got the results you described when trying to install by scanning the barcode.  Also, in Apple developer make sure your device's UDID is included in your development provisioning profile. I remember getting some kind of error when I left one of my test devices out.



                              1 person found this helpful
                              • 12. Re: iOS isn't executing script
                                hexrdesign Level 1

                                Thanks so much RonSSA for the tip about using development certificate instead of distribution certificate and including device UDID. We were successfully able to build our iOS app and run it on our devices. Unfortunately, it seems as though the problem is persisting. The scripts still are not running and therefore we cant save notes. On our github, take a look at our index.html and notes.js in our js folder in particular. Might it be that using Jquery is causing an error?


                                Thanks in advance!

                                • 13. Re: iOS isn't executing script
                                  VectorP Level 4

                                  You include several javascript files, which all define callback functions for the 'document.ready' event. Without really studying your code files, I immediately noticed at least three of them.

                                  The order of execution is thus undefined, which might lead to a situation where object 'pv' is used (for instance in notes.js), whereas it has not been instantiated (in main.js) yet.


                                  So, you should make sure that there's only one 'document.ready' handler function. Also make sure that the order of the actions are defined (using callback functions - be aware that javascript is asynchronous!) and objects are instantiated before calling any of their methods.

                                  • 14. Re: iOS isn't executing script
                                    hexrdesign Level 1

                                    Hi There!


                                    We tried it, as well as many other things and it works (finally)! We originally tried VectorP's solution, and it didn't fix anything. However, we were loading this script after the content near the end of the document and it had the defer attribute. When we moved the script to the header, it started working. Thus, my guess is PhoneGap / mobile phones don't handle scripts in the footer well, although I do it quite a bit, and it doesn't seem to influence it in generic web development.


                                    VectorP: While JavaScript does execute asynchronously, it does not load files asynchronously unless the async attribute is true. http://andydavies.me/blog/images/no-pre-loader-waterfall-ie7.png That image shows how files are loaded normally. Thus, as long as any js file loads after the pv object is initialized, it can be used. However, having multiple document.ready handler functions definately could cause other problems, as that's when JavaScript's async really shines!