{"id":2085,"date":"2015-08-15T10:03:25","date_gmt":"2015-08-15T10:03:25","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=2085"},"modified":"2015-08-16T10:16:50","modified_gmt":"2015-08-16T10:16:50","slug":"check-network-information-change-with-ionic-famework","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/codeproject\/check-network-information-change-with-ionic-famework\/","title":{"rendered":"Check network information change with Ionic framework"},"content":{"rendered":"<h3>TL;DR<\/h3>\n<p>In this tutorial, I&#8217;m going to quickly show you how to get <strong>network information change<\/strong> with Ionic framework. What do I mean by <em>network information change<\/em>? Well, if you need to detect when your phone gets on the Internet, or loses connection, you can do so with this example code. Additionally, you can get the type of the network your phone is connected to (WIFI for example).<\/p>\n<h3>Finished project<\/h3>\n<p>You can clone the finished project from Github: <a href=\"https:\/\/github.com\/Hitman666\/IonicNetworkInfo\">https:\/\/github.com\/Hitman666\/IonicNetworkInfo<\/a>, and you can see how it actually looks like on the image below (nothing fancy, this is a test project after all ;)):<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/networkInfoIonic.png\" rel=\"lightbox[2085]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2089\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/networkInfoIonic.png\" alt=\"networkInfoIonic\" width=\"371\" height=\"328\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/networkInfoIonic.png 371w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/networkInfoIonic-300x265.png 300w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><\/a><\/p>\n<h3>Step by step on how to make this yourself<\/h3>\n<p>Here are the quick steps in recreating the exact same app:<\/p>\n<ol>\n<li>Start a new Ionic project by doing:\n<pre class=\"lang:default decode:true\">ionic start IonicNetworkInfo blank\r\n<\/pre>\n<\/li>\n<li>Then, change the directory to the newly created IonicNetworkInfo:\n<pre class=\"lang:default decode:true\">cd IonicNetworkInfo<\/pre>\n<\/li>\n<li>Install <a href=\"http:\/\/ngcordova.com\/\">ngCordova<\/a> with <a href=\"http:\/\/bower.io\/\">Bower<\/a>:\n<pre class=\"lang:default decode:true \">bower install ngCordova<\/pre>\n<p>If by some chance you don&#8217;t have bower installed, you can install it with <a href=\"https:\/\/www.npmjs.com\/\">npm<\/a>:<\/p>\n<pre class=\"lang:default decode:true \">npm install bower -g<\/pre>\n<p><!--<em>If you want to learn more about Bower, you can take a look at the tutorial I wrote for DigitalOcean <a href=\"willUpdatelinkWhenPublished\">Getting started with Bower<\/a>.<\/em>--><\/li>\n<li>Open up the <strong>www\/index.html<\/strong> file in your favorite editor, and add the reference to ngCordova (just above the cordova.js script):\n<pre class=\"lang:default decode:true\">&lt;!-- This is what you should add, the cordova below you'll already have --&gt;\r\n&lt;script src=\"lib\/ngCordova\/dist\/ng-cordova.min.js\"&gt;&lt;\/script&gt;\r\n\r\n&lt;!-- cordova script (this will be a 404 during development) --&gt;\r\n&lt;script src=\"cordova.js\"&gt;&lt;\/script&gt;<\/pre>\n<\/li>\n<li>Install the\u00a0<a href=\"http:\/\/ngcordova.com\/docs\/plugins\/network\/\">ngCordova network plugin<\/a>\u00a0by executing the following command in your Terminal\/Command prompt (you should do this from the root directory of your app; so, in our case the <strong>IonicNetworkInfo<\/strong>\u00a0directory):\n<pre class=\"lang:default decode:true\">cordova plugin add org.apache.cordova.network-information<\/pre>\n<p>To check if you have successfully installed the plugin, you can run the following command (from the root directory &#8211; I won&#8217;t be repeating this anymore; when I say you should run some command from the Terminal\/Command prompt that, in this case, means from the root directory of the application):<\/p>\n<pre class=\"lang:default decode:true \">cordova plugin list<\/pre>\n<p>You should see the following output:<\/p>\n<pre class=\"lang:default decode:true\">&gt; cordova plugin list                                                                                                                           \r\ncom.ionic.keyboard 1.0.4 \"Keyboard\"\r\norg.apache.cordova.network-information 0.2.15 \"Network Information\"<\/pre>\n<\/li>\n<li>Open up the <strong>www\/js\/app.js<\/strong> file and add <strong>ngCordova<\/strong> to the dependencies list, so that basically the first line looks like this:\n<pre class=\"lang:default decode:true\">angular.module('starter', ['ionic', 'ngCordova'])<\/pre>\n<\/li>\n<li>Create a new controller in the <strong>www\/js\/app.js<\/strong> file called <strong>MyCtrl<\/strong>, with the following content:\n<pre class=\"lang:default decode:true\">.controller('MyCtrl', function($scope, $cordovaNetwork, $rootScope) {\r\n    document.addEventListener(\"deviceready\", function () {\r\n\r\n        $scope.network = $cordovaNetwork.getNetwork();\r\n        $scope.isOnline = $cordovaNetwork.isOnline();\r\n        $scope.$apply();\r\n        \r\n        \/\/ listen for Online event\r\n        $rootScope.$on('$cordovaNetwork:online', function(event, networkState){\r\n            $scope.isOnline = true;\r\n            $scope.network = $cordovaNetwork.getNetwork();\r\n            \r\n            $scope.$apply();\r\n        })\r\n\r\n        \/\/ listen for Offline event\r\n        $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){\r\n            console.log(\"got offline\");\r\n            $scope.isOnline = false;\r\n            $scope.network = $cordovaNetwork.getNetwork();\r\n            \r\n            $scope.$apply();\r\n        })\r\n\r\n  }, false);\r\n})<\/pre>\n<p>In this controller you attach an event listener on the <strong>deviceready<\/strong>\u00a0event (because it could be that the device would not have been yet initialized when this code runs) and you get the network information with:<\/p>\n<pre class=\"lang:default decode:true \">$cordovaNetwork.getNetwork();<\/pre>\n<p>The information, about weather you&#8217;re connected to the internet is obtained with the following line:<\/p>\n<pre class=\"lang:default decode:true \">$scope.isOnline = $cordovaNetwork.isOnline();<\/pre>\n<p>Then, you register two events <strong>$cordovaNetwork:online<\/strong>\u00a0and <strong>$cordovaNetwork:online<\/strong>\u00a0which trigger when the device gets online\/offline. In them you then just update the $scope variables ().<\/p>\n<p>Just for reference, the whole content of the <strong>www\/js\/app.js<\/strong> file should be:<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Ionic Starter App\r\n\r\n\/\/ angular.module is a global place for creating, registering and retrieving Angular modules\r\n\/\/ 'starter' is the name of this angular module example (also set in a &lt;body&gt; attribute in index.html)\r\n\/\/ the 2nd parameter is an array of 'requires'\r\nangular.module('starter', ['ionic', 'ngCordova'])\r\n\r\n.run(function($ionicPlatform, $cordovaNetwork, $rootScope) {\r\n  $ionicPlatform.ready(function() {\r\n    \/\/ Hide the accessory bar by default (remove this to show the accessory bar above the keyboard\r\n    \/\/ for form inputs)\r\n    if(window.cordova &amp;&amp; window.cordova.plugins.Keyboard) {\r\n      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);\r\n    }\r\n    if(window.StatusBar) {\r\n      StatusBar.styleDefault();\r\n    }\r\n\r\n  });\r\n})\r\n\r\n.controller('MyCtrl', function($scope, $cordovaNetwork, $rootScope) {\r\n    document.addEventListener(\"deviceready\", function () {\r\n\r\n        $scope.network = $cordovaNetwork.getNetwork();\r\n        $scope.isOnline = $cordovaNetwork.isOnline();\r\n        $scope.$apply();\r\n        \r\n        \/\/ listen for Online event\r\n        $rootScope.$on('$cordovaNetwork:online', function(event, networkState){\r\n            $scope.isOnline = true;\r\n            $scope.network = $cordovaNetwork.getNetwork();\r\n            \r\n            $scope.$apply();\r\n        })\r\n\r\n        \/\/ listen for Offline event\r\n        $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){\r\n            console.log(\"got offline\");\r\n            $scope.isOnline = false;\r\n            $scope.network = $cordovaNetwork.getNetwork();\r\n            \r\n            $scope.$apply();\r\n        })\r\n\r\n  }, false);\r\n});<\/pre>\n<\/li>\n<li>In the <strong>index.html<\/strong> file, inside the <strong>ion-content<\/strong> tag paste the following content:\n<pre class=\"lang:default decode:true \">&lt;div class=\"card\"&gt;\r\n                &lt;div class=\"item item-text-wrap\"&gt;\r\n                    &lt;h1&gt;Network: {{network}}&lt;\/h1&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n\r\n\r\n            &lt;div class=\"card\"&gt;\r\n                &lt;div class=\"item item-text-wrap\"&gt;\r\n                    &lt;ion-toggle ng-model=\"isOnline\" ng-checked=\"item.checked\"&gt;\r\n                        &lt;h1 ng-show=\"isOnline\"&gt;I'm online&lt;\/h1&gt;\r\n                        &lt;h1 ng-show=\"! isOnline\"&gt;I'm offline&lt;\/h1&gt;\r\n                    &lt;\/ion-toggle&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;<\/pre>\n<p>Basically what we do here is we show the contents of the <strong>network<\/strong> variable (which is attached to the <strong>$scope<\/strong> via the controller). Also, by using the <strong>ion-toggle<\/strong>\u00a0component we show the &#8220;I&#8217;m online&#8221; \/ &#8220;I&#8217;m offline&#8221; notifications.<\/p>\n<p>Just for reference, the content of the whole <strong>index.html<\/strong> file should look like this:<\/p>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width\"&gt;\r\n    &lt;title&gt;&lt;\/title&gt;\r\n\r\n    &lt;link href=\"lib\/ionic\/css\/ionic.css\" rel=\"stylesheet\"&gt;\r\n    &lt;link href=\"css\/style.css\" rel=\"stylesheet\"&gt;\r\n\r\n    &lt;!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above\r\n    &lt;link href=\"css\/ionic.app.css\" rel=\"stylesheet\"&gt;\r\n    --&gt;\r\n\r\n    &lt;!-- ionic\/angularjs js --&gt;\r\n    &lt;script src=\"lib\/ionic\/js\/ionic.bundle.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;script src=\"lib\/ngCordova\/dist\/ng-cordova.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;!-- cordova script (this will be a 404 during development) --&gt;\r\n    &lt;script src=\"cordova.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- your app's js --&gt;\r\n    &lt;script src=\"js\/app.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body ng-app=\"starter\" ng-controller=\"MyCtrl\"&gt;\r\n\r\n    &lt;ion-pane&gt;\r\n        &lt;ion-header-bar class=\"bar-stable\"&gt;\r\n            &lt;h1 class=\"title\"&gt;Ionic Blank Starter&lt;\/h1&gt;\r\n        &lt;\/ion-header-bar&gt;\r\n      \r\n        &lt;ion-content padding=\"true\"&gt;\r\n            &lt;div class=\"card\"&gt;\r\n                &lt;div class=\"item item-text-wrap\"&gt;\r\n                    &lt;h1&gt;Network: {{network}}&lt;\/h1&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n\r\n\r\n            &lt;div class=\"card\"&gt;\r\n                &lt;div class=\"item item-text-wrap\"&gt;\r\n                    &lt;ion-toggle ng-model=\"isOnline\" ng-checked=\"item.checked\"&gt;\r\n                        &lt;h1 ng-show=\"isOnline\"&gt;I'm online&lt;\/h1&gt;\r\n                        &lt;h1 ng-show=\"! isOnline\"&gt;I'm offline&lt;\/h1&gt;\r\n                    &lt;\/ion-toggle&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n\r\n        &lt;\/ion-content&gt;\r\n    &lt;\/ion-pane&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<\/li>\n<\/ol>\n<h3>Testing time<\/h3>\n<p>In order to test this application you should run it on your device (because you can&#8217;t <a href=\"http:\/\/stackoverflow.com\/questions\/4808433\/is-it-possible-to-disable-the-network-in-ios-simulator\">disable network <\/a>in iOS simulator).<\/p>\n<p>First, in case you haven&#8217;t, add the desired platform (execute this command from the root of the directory). For iOs execute:<\/p>\n<pre class=\"lang:default decode:true\">ionic platform add ios<\/pre>\n<p>Or, for Android execute:<\/p>\n<pre class=\"lang:default decode:true \">ionic platform add android<\/pre>\n<p>Just a quick note &#8211; in order to build the app for iOs, you need to be running this on Mac machine (sure, there are ways around it, but TBH, I&#8217;m not a fan of them). As for Android, you can normally run it on your Windows machine (<em>if, of course you have all the SDKs in place. I&#8217;ll write about setting these up in another tutorial, so will update the link here when the post will be published, for those who want to see this kind of information<\/em>).<\/p>\n<p>If you have an <strong>Android device<\/strong> plugged to your computer (and all the SDKs in place) you can run the following to commands to get your application running on your Android device:<\/p>\n<pre class=\"lang:default decode:true \">ionic build android &amp;&amp; ionic run android<\/pre>\n<p>To get this application running on your <strong>iOS device<\/strong>, you first have to build the application, and you can do this by executing the following command:<\/p>\n<pre class=\"lang:default decode:true\">ionic build ios<\/pre>\n<p>Now, open up the <strong>IonicNetworkInfo\/platforms\/ios<\/strong> folder and you should see the following content:<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-15-at-11.52.18.png\" rel=\"lightbox[2085]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2087\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-15-at-11.52.18.png\" alt=\"Screen Shot 2015-08-15 at 11.52.18\" width=\"706\" height=\"357\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-15-at-11.52.18.png 706w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-15-at-11.52.18-300x152.png 300w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/a><\/p>\n<p>Next, open up <strong>IonicNetworkInfo.xcodeproj<\/strong> file with Xcode:<\/p>\n<p>Make sure that your iOS device is connected to your Mac computer, select it from the dropdown menu and click run:<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/XcodeRun.png\" rel=\"lightbox[2085]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2088\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/XcodeRun.png\" alt=\"XcodeRun\" width=\"441\" height=\"138\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/XcodeRun.png 441w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/08\/XcodeRun-300x94.png 300w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/a><\/p>\n<p>Now, experiment with the application by turning your WIFI on\/of, turning your Cellular network (if you have it) on\/off, and basically enjoy seing the information chage automatically :). Surely, you can do way more advanced stuff based on this simple example, but I hope this will get you started&#8230;<\/p>\n<p>That&#8217;s all folks, I hope this will prove to be useful to someone. If you have any questions, or you&#8217;ve noticed some blatant blunder that I may have made, please don&#8217;t hesitate to share it in the comments, I&#8217;ll make sure to reply promptly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR In this tutorial, I&#8217;m going to quickly show you how to get network information change with Ionic framework. What do I mean by network information change? Well,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,43],"tags":[],"class_list":["post-2085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeproject","category-ionic"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=2085"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2085\/revisions"}],"predecessor-version":[{"id":2821,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2085\/revisions\/2821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/2086"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=2085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=2085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=2085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}