简体   繁体   中英

Phonegap app working on preview but not on Android device - Promise

I have a Phonegap app which runs perfectly on the preview app but when I create an APK using Phonegap Build and install it on an Android device it works partially excepting any code that is inside a Promise. I added a few alerts and I noticed it stops working right before a Promise.

I have the following code:

    {
        alert('passwordreset before promise');
        return new Promise((resolve, reject) => {
            alert('inside promise');
            this.api.call( '/' + this.endpoint + '/recover', {email: email})
            .then(response => 
            { alert('promise succes');
                resolve(response);
            })
            .catch(errors => 
            {
                reject(errors);
            });

        });
    } 

The first alert (password before promise) is executed but not the others.

Do you have any idea what might be going on?

Do you use Android studio? If so check what log do you have in LogCat tab. Did you set proper CORS settings? It looks like you are making some api requests. In MSDN there's a nice article describing what settings you may be missing: https://docs.microsoft.com/en-us/visualstudio/cross-platform/tools-for-cordova/security/whitelists?view=toolsforcordova-2017

Since Android 9 clear text is disabled by default. If the problem still persist verify your network_security_settings.xml (if it's set).

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
  <domain includeSubdomains="true">10.0.1.1</domain> <!-- Set local api -->
</network-security-config>

You can set network settings file in your Android Manifest

<?xml version="1.0" encoding="utf-8"?>
 <manifest>
 <application android:networkSecurityConfig="@xml/network_security_config">
    ...
 </application>
 </manifest>

Promises are not supported in webview. You need to transpile/compile your JS code for ES5.

Promises that are part of the ES2015 JavaScript specification (also referred to as ES6).

If you need to polyfill, check out https://www.promisejs.org/

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