Nativescript: How to check internet connectivity status in nativescript?

Created on 4 Jan 2017  路  10Comments  路  Source: NativeScript/NativeScript

Hi every one,

I have to check whether the user having internet or not, so i've used connectivity module but am getting a gigantic error,which is tied below. I think it's asking some permissions but that are also configured in Androidmanifest.xml file.but wondering why is throwing?? and It is working in ios but not in android.

(https://cloud.githubusercontent.com/assets/8955270/21639917/ba1c74d4-d298-11e6-886e-66d8bbdaf68b.png)

android question

Most helpful comment

but this only show the type of connection, but if you have for example, wifi with no internet it says wifi, but doesnt say if you have internet or not... is there something to check internet??

All 10 comments

Hello @narender56

The error you are seeing is as you stated because of missing permissions in your _AndroidManifest.xml_ file. Keep in mind that any changes made in AndroidMManifest.xml will require a full rebuild of your application (livesync won't apply changes outside _app_ folder).

This is the necessary permission
located in _app/App_Resources/AndroidManifest.xml_.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

With saved changes rebuild your app with tns run android or tns build android.

Test application with working connectivity module here - you can check it on your side and let us know if everything worked out for you.

hey @NickIliev , thanks for your help,it's working now but getting another issue with that permissions. i'm directly getting permission denied.How to alert the user for taking the permissions.

var permissions = require("nativescript-permissions")
var connectivity = require("connectivity");
permissions.requestPermission(android.Manifest.permission.ACCESS_NETWORK_STATE, "I need these permissions because I'm cool")
.then(() => {
console.log("Permission Granted!");

            switch (connectivity.getConnectionType()) {
                case connectivity.connectionType.none:
                    this.connectWith = "None";
                    console.log("none");
                    break;
                case connectivity.connectionType.wifi:
                    this.connectWith = "Wi-Fi";
                    console.log("wifi");
                    break;
                case connectivity.connectionType.mobile:
                    this.connectWith = "Mobile";
                    console.log("mobile");
                    break;
                default:
                    break;
            }
        })
        .catch(() => {
            console.log("Permission Denied!");
        });

output : Permission Denied!

@narender56 have you added the permission in your AndroidManifest.xml file prior to building your application!?

Apart from that here is an example on how to grant permissions using the nativescript-permissions plugin - in this example I have asked for permission on navigatedTo page event. Note that you will still have to explicitly set the list of wanted permission in your AndroidManifest.xml

@NickIliev thanks for your help. It's working now.

but this only show the type of connection, but if you have for example, wifi with no internet it says wifi, but doesnt say if you have internet or not... is there something to check internet??

I want to know if I have access to Internet, not if I have my wireless board turned on or off.

@NickIliev I have the same need as @JorgeHawkins and @slasho. I need to know if there's internet connection or not - being connected to a wifi network that doesn't have internet for instance should tell me there's no internet

@JorgeHawkins @fhackenb you guys found any solution?

@fhackenb @Omi231 @slasho the connectivity module in NativeScript is using the native APIs and is working just as it would work in native Android or iOS (for example here is the Android implementation which is using the native getActiveNetworkInfo method).

So if that said even the native Android developers are facing the same scenario where the connectivity module is assuming that there is a connection (and there might be one even if it is very slow and practically there is almost no internet connection). In similar cases and an additional check is required and, for example, you could implement kind of manual ping. There are multiple suggestions provided in tthe very same thread, but as far as I saw none of them is actually covering all scenarios that can occur.

All of the above considered, if anyone has a good and bullet-proof solution for real Internet check, we are open for PRs and contributions.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OscarLopezArnaiz picture OscarLopezArnaiz  路  3Comments

Pourya8366 picture Pourya8366  路  3Comments

nirsalon picture nirsalon  路  3Comments

rLoka picture rLoka  路  3Comments

yclau picture yclau  路  3Comments