i.brod
i.brod

Reputation: 4623

Nativescript: how to use different environment vars for an emulator and a real device?

I'm building a project with Nativescript-Vue. I have standard a setup with Webpack.

I understand that i can pass different vars, using something like this:

tns build android --bundle --env.development --env.property=value

How can i "distinguish" between the two different platforms(emulator and a real device, they are both "development"), in order to pass different API url's?

Upvotes: 0

Views: 164

Answers (2)

Manoj
Manoj

Reputation: 21908

You can do something like this,

        if (isIOS) {
            const description = String(NSString.stringWithUTF8String(NXGetLocalArchInfo()[0].description));
            this.isSimulator = description.indexOf("x86") !== -1 || description.indexOf("i386") !== -1;
        }
        if (isAndroid) {
            this.isSimulator = android.os.Build.FINGERPRINT.startsWith("generic")
                || android.os.Build.FINGERPRINT.startsWith("unknown")
                || android.os.Build.MODEL.indexOf("google_sdk") !== -1
                || android.os.Build.MODEL.indexOf("Emulator") !== -1
                || android.os.Build.MODEL.indexOf("Android SDK built for x86") !== -1
                || android.os.Build.MANUFACTURER.indexOf("Genymotion") !== -1
                || (android.os.Build.BRAND.startsWith("generic") && android.os.Build.DEVICE.startsWith("generic"))
                || android.os.Build.PRODUCT === "google_sdk";
        }

The Android version is almost same as the one @Narendra linked in his answer but more of the NativeScript / JavaScript version.

Playground Sample

Upvotes: 1

Narendra
Narendra

Reputation: 4574

You can check the Build properties from android.os.Build and see if Build.BRAND.startsWith("generic") or Build.DEVICE.startsWith("generic")) or Build.PRODUCT.contains("emulator").

You may refer to few more properties as described here.

Upvotes: 1

Related Questions