{"id":14987882,"url":"https://github.com/apache/cordova-plugin-network-information","last_synced_at":"2025-05-14T02:07:39.198Z","repository":{"id":9085305,"uuid":"10860605","full_name":"apache/cordova-plugin-network-information","owner":"apache","description":"Apache Cordova Network Information Plugin","archived":false,"fork":false,"pushed_at":"2024-10-26T01:34:21.000Z","size":437,"stargazers_count":463,"open_issues_count":23,"forks_count":322,"subscribers_count":46,"default_branch":"master","last_synced_at":"2025-05-10T17:16:22.582Z","etag":null,"topics":["android","cordova","hacktoberfest","ios","java","javascript","library","mobile","nodejs","objective-c"],"latest_commit_sha":null,"homepage":"https://cordova.apache.org/","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apache.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-06-22T07:00:18.000Z","updated_at":"2025-05-09T05:33:30.000Z","dependencies_parsed_at":"2024-11-19T16:32:50.812Z","dependency_job_id":"f7ba36e2-1a22-4708-8850-6cb2bc5f1c46","html_url":"https://github.com/apache/cordova-plugin-network-information","commit_stats":{"total_commits":277,"total_committers":73,"mean_commits":"3.7945205479452055","dds":0.8014440433212997,"last_synced_commit":"c9a7b0b06fd8da8a3d9252b2d4815b23cccf9bbb"},"previous_names":[],"tags_count":55,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-network-information","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-network-information/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-network-information/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apache%2Fcordova-plugin-network-information/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apache","download_url":"https://codeload.github.com/apache/cordova-plugin-network-information/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254052901,"owners_count":22006717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","cordova","hacktoberfest","ios","java","javascript","library","mobile","nodejs","objective-c"],"created_at":"2024-09-24T14:15:37.990Z","updated_at":"2025-05-14T02:07:39.135Z","avatar_url":"https://github.com/apache.png","language":"Objective-C","readme":"---\ntitle: Network Information\ndescription: Get information about wireless connectivity.\n---\n\u003c!--\n# license: Licensed to the Apache Software Foundation (ASF) under one\n#         or more contributor license agreements.  See the NOTICE file\n#         distributed with this work for additional information\n#         regarding copyright ownership.  The ASF licenses this file\n#         to you under the Apache License, Version 2.0 (the\n#         \"License\"); you may not use this file except in compliance\n#         with the License.  You may obtain a copy of the License at\n#\n#           http://www.apache.org/licenses/LICENSE-2.0\n#\n#         Unless required by applicable law or agreed to in writing,\n#         software distributed under the License is distributed on an\n#         \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n#         KIND, either express or implied.  See the License for the\n#         specific language governing permissions and limitations\n#         under the License.\n--\u003e\n\n# cordova-plugin-network-information\n\n[![Android Testsuite](https://github.com/apache/cordova-plugin-network-information/actions/workflows/android.yml/badge.svg)](https://github.com/apache/cordova-plugin-network-information/actions/workflows/android.yml) [![Chrome Testsuite](https://github.com/apache/cordova-plugin-network-information/actions/workflows/chrome.yml/badge.svg)](https://github.com/apache/cordova-plugin-network-information/actions/workflows/chrome.yml) [![iOS Testsuite](https://github.com/apache/cordova-plugin-network-information/actions/workflows/ios.yml/badge.svg)](https://github.com/apache/cordova-plugin-network-information/actions/workflows/ios.yml) [![Lint Test](https://github.com/apache/cordova-plugin-network-information/actions/workflows/lint.yml/badge.svg)](https://github.com/apache/cordova-plugin-network-information/actions/workflows/lint.yml)\n\n\nThis plugin provides an implementation of an old version of the\n[Network Information API](http://www.w3.org/TR/2011/WD-netinfo-api-20110607/).\nIt provides information about the device's cellular and\nwifi connection, and whether the device has an internet connection.\n\n\u003e To get a few ideas how to use the plugin, check out the [sample](#sample) at the bottom of this page or go straight to the [reference](#reference) content.\n\n## Installation\n\n    cordova plugin add cordova-plugin-network-information\n\n## Supported Platforms\n\n- Android\n- Browser\n- iOS\n- Windows\n\n# Connection\n\n\u003e The `connection` object, exposed via `navigator.connection`,  provides information about the device's cellular and wifi connection.\n\n## Properties\n\n- connection.type\n\n## Constants\n\n- Connection.UNKNOWN\n- Connection.ETHERNET\n- Connection.WIFI\n- Connection.CELL_2G\n- Connection.CELL_3G\n- Connection.CELL_4G\n- Connection.CELL\n- Connection.NONE\n\n## connection.type\n\nThis property offers a fast way to determine the device's network\nconnection state, and type of connection.\n\n### Quick Example\n\n```js\nfunction checkConnection() {\n    var networkState = navigator.connection.type;\n\n    var states = {};\n    states[Connection.UNKNOWN]  = 'Unknown connection';\n    states[Connection.ETHERNET] = 'Ethernet connection';\n    states[Connection.WIFI]     = 'WiFi connection';\n    states[Connection.CELL_2G]  = 'Cell 2G connection';\n    states[Connection.CELL_3G]  = 'Cell 3G connection';\n    states[Connection.CELL_4G]  = 'Cell 4G connection';\n    states[Connection.CELL]     = 'Cell generic connection';\n    states[Connection.NONE]     = 'No network connection';\n\n    alert('Connection type: ' + states[networkState]);\n}\n\ncheckConnection();\n```\n\n### iOS Quirks\n\n- \u003ciOS7 can't detect the type of cellular network connection.\n    - `navigator.connection.type` is set to `Connection.CELL` for all cellular data.\n\n### Windows Quirks\n\n- When running in the Phone 8.1 emulator, always detects `navigator.connection.type` as `Connection.ETHERNET`.\n\n### Browser Quirks\n\n- Browser can't detect the type of network connection.\n`navigator.connection.type` is always set to `Connection.UNKNOWN` when online.\n\n# Network-related Events\n\n## offline\n\nThe event fires when an application goes offline, and the device is\nnot connected to the Internet.\n\n    document.addEventListener(\"offline\", yourCallbackFunction, false);\n\n### Details\n\nThe `offline` event fires when a previously connected device loses a\nnetwork connection so that an application can no longer access the\nInternet.  It relies on the same information as the Connection API,\nand fires when the value of `connection.type` becomes `NONE`.\n\nApplications typically should use `document.addEventListener` to\nattach an event listener once the `deviceready` event fires.\n\n### Quick Example\n\n```js\ndocument.addEventListener(\"offline\", onOffline, false);\n\nfunction onOffline() {\n    // Handle the offline event\n}\n```\n\n### Quirks\n\nThis plugin is unable to broadcast events while in the background. Use `navigator.connection.type` to check connection status on the [resume](https://cordova.apache.org/docs/en/latest/cordova/events/events.html#resume) event instead.\n\n### iOS Quirks\n\nDuring initial startup, the first offline event (if applicable) takes at least a second to fire.\n\n## online\n\nThis event fires when an application goes online, and the device\nbecomes connected to the Internet.\n\n    document.addEventListener(\"online\", yourCallbackFunction, false);\n\n### Details\n\nThe `online` event fires when a previously unconnected device receives\na network connection to allow an application access to the Internet.\nIt relies on the same information as the Connection API,\nand fires when the `connection.type` changes from `NONE` to any other\nvalue.\n\nApplications typically should use `document.addEventListener` to\nattach an event listener once the `deviceready` event fires.\n\n### Quick Example\n\n```js\ndocument.addEventListener(\"online\", onOnline, false);\n\nfunction onOnline() {\n    // Handle the online event\n}\n```\n\n### Quirks\n\nThis plugin is unable to broadcast events while in the background. Use `navigator.connection.type` to check connection status on the [resume](https://cordova.apache.org/docs/en/latest/cordova/events/events.html#resume) event instead.\n\n### iOS Quirks\n\nDuring initial startup, the first `online` event (if applicable) takes\nat least a second to fire, prior to which `connection.type` is\n`UNKNOWN`.\n\n## Sample: Upload a File Depending on your Network State \u003ca name=\"sample\"\u003e\u003c/a\u003e\n\nThe code examples in this section show examples of changing app behavior using the online and offline events and your network connection status.\n\nTo start with, create a new FileEntry object (data.txt) to use for sample data. Call this function from the `deviceready` handler.\n\n\u003e*Note* This code example requires the File plugin.\n\n```js\nvar dataFileEntry;\n\nfunction createSomeData() {\n\n    window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {\n\n        console.log('file system open: ' + fs.name);\n        // Creates a new file or returns an existing file.\n        fs.root.getFile(\"data.txt\", { create: true, exclusive: false }, function (fileEntry) {\n\n          dataFileEntry = fileEntry;\n\n        }, onErrorCreateFile);\n\n    }, onErrorLoadFs);\n}\n```\n\nNext, add listeners for the online and offline events in the `deviceready` handler.\n\n```js\ndocument.addEventListener(\"offline\", onOffline, false);\ndocument.addEventListener(\"online\", onOnline, false);\n```\n\nThe app's `onOnline` function handles the online event. In the event handler, check the current network state. In this app, treat any connection type as good except Connection.NONE. If you have a connection, you try to upload a file.\n\n```js\nfunction onOnline() {\n    // Handle the online event\n    var networkState = navigator.connection.type;\n\n    if (networkState !== Connection.NONE) {\n        if (dataFileEntry) {\n            tryToUploadFile();\n        }\n    }\n    display('Connection type: ' + networkState);\n}\n```\n\nWhen the online event fires in the preceding code, call the app's `tryToUploadFile` function.\n\nIf the upload fails, then call the app's `offlineWrite` function to save the current data somewhere.\n\n\u003e*Note* For simplicity, file reading \u0026 writing was omitted. Refer to the [cordova-plugin-file](https://github.com/apache/cordova-plugin-file#cordova-plugin-file) documentation for more information on file handling.\n\n```js\nfunction tryToUploadFile() {\n    // !! Assumes variable fileURL contains a valid URL to a text file on the device,\n    var fileURL = getDataFileEntry().toURL();\n    \n    getFileBlobSomehow(fileURL, function(fileBlob) {\n        var success = function (r) {\n            console.log(\"Response = \" + r.response);\n            display(\"Uploaded. Response: \" + r.response);\n        };\n\n        var fail = function (error) {\n            console.log(\"An error has occurred: Code = \" + error.code || error.status);\n            offlineWrite(\"Failed to upload: some offline data\");\n        }\n\n        var xhr = new XMLHttpRequest();\n\n        xhr.onerror = fail;\n        xhr.ontimeout = fail;\n        xhr.onload = function() {\n            // If the response code was successful...\n            if (xhr.status \u003e= 200 \u0026\u0026 xhr.status \u003c 400) {\n                success(xhr);\n            }\n            else {\n                fail(xhr)\n            }\n        }\n\n        // Make sure you add the domain of your server URL to the\n        // Content-Security-Policy \u003cmeta\u003e element in index.html.\n        xhr.open(\"POST\", encodeURI(SERVER));\n\n        xhr.setRequestHeader(\"Content-Type\", \"text/plain\");\n\n        // The server request handler could read this header to\n        // set the filename.\n        xhr.setRequestHeader(\"X-Filename\", fileURL.substr(fileURL.lastIndexOf(\"/\") + 1));\n\n        xhr.send(fileBlob);\n    });\n};\n```\n\nHere is the code for the `offlineWrite` function.\n\n\u003e*Note* This code examples requires the File plugin.\n\n```js\nfunction offlineWrite(offlineData) {\n    // Create a FileWriter object for our FileEntry.\n    dataFileEntry.createWriter(function (fileWriter) {\n\n        fileWriter.onwriteend = function () {\n            console.log(\"Successful file write...\");\n            display(offlineData);\n        };\n\n        fileWriter.onerror = function (e) {\n            console.log(\"Failed file write: \" + e.toString());\n        };\n\n        fileWriter.write(offlineData);\n    });\n}\n```\n\nIf the offline event occurs, just do something like notify the user (for this example, just log it).\n\n```js\nfunction onOffline() {\n    // Handle the offline event\n    console.log(\"lost connection\");\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapache%2Fcordova-plugin-network-information","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapache%2Fcordova-plugin-network-information","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapache%2Fcordova-plugin-network-information/lists"}