Thursday, 21 July 2016

Video as background in Ionic app

Hello,

Last time,i was facing problem to play a video in background of my app. So, i got the solution for same.

Here i am introducing a way by which you can add this feature in your app-

Follow the following sets of command to start a blank app-

ionic start MyVideoApp blank

cd MyVideoApp

ionic platform add android

Now, add the following lines of code in your index.html file-

 <body ng-app="app">
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Awesome App</h1>
      </ion-header-bar>
           <video autoplay loop poster="" id="bgvid">
   
    <source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm">
   
    <source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/mp4">
   
    </video>
    <div ng-click="" class="button has-header"> CLICK ME TO PLAY VIDEO</div>
    </ion-pane>
  </body>

Now, add following lines of in your style.css file-

#bgvid {

position: fixed;

top: 50%;

left: 50%;

min-width: 100%;

min-height: 100%;

width: auto;

height: auto;

z-index: -100;

-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
-o-transform: translateX(-50%) translateY(-50%);

transform: translateX(-50%) translateY(-50%);

background: url(http://video.webmfiles.org/big-buck-bunny_trailer.webm) no-repeat;

background-size: cover;

}


Now,you can play video in your app.



Thanks....

Friday, 18 March 2016

Facebook integration in ionic project

Hello,

If your App needs Login or Registration functionality, then using 3rd party services such as Facebook Authentication are useful tools to utilize.


Creating a Facebook Developer App-

Every third party app that integrates Facebook login needs to create a Facebook developer app. A developer app helps Facebook track the third party app and provides additional configuration options.
To create a Facebook developer app for our Cordova app do the following:
Visit the Facebook Developer Apps Page and click Add a New App. Select Website as the platform.
Let’s start by creating a fresh Ionic Framework Android application:
ionic start FacebookApp blank
cd FacebookApp
ionic platform add android
There are two requirements to this project.  You must include the ngCordova library for $cordovaOauth and the Apache Cordova InAppBrowser plugin
Command to include InAppBrowser plugin-
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git
With the file in place, open your www/index.html file and include the library like so:
<script src="js/ng-cordova.min.js"></script>

In your app.js fill add the following lines of code-
var facebookApp = angular.module("starter", ["ionic", "ngCordova"]);

Our Facebook project is going to be composed of the following three screens managed by the AngularJS UI-Router:
  • Login (www/templates/login.html)
  • Profile (www/templates/profile.html)
Let’s start by adding the following code to our login template:

Add the following lines of code in login.html page-
<ion-view title="Facebook Example">
    <ion-content>
        <div class="padding">
            <button ng-click="login()" class="button button-block button-balanced">Oauth Sign In</button>
            <p>
                Clicking the above button will initiate an official browser sign in flow with Facebook.  You
                will be redirected to the Facebook website to enter your credentials.
            </p>
        </div>
    </ion-content>
</ion-view>

The only purpose the above template serves is to launch the InAppBrowser for the Oauth login flow.  The controller code can be demonstrated with the following:

facebookApp.controller("LoginController", function($scope, $cordovaOauth, $localStorage, $location) {
    $scope.login = function() {
        $cordovaOauth.facebook("CLIENT_ID_HERE", ["email", "read_stream", "user_website", "user_location", "user_relationships"]).then(function(result) {
            $localStorage.accessToken = result.access_token;
            $location.path("/profile");
        }, function(error) {
            alert("There was a problem signing in!  See the console for logs");
            console.log(error);
        });
    };
});

Notice that after a successful login, the access token is saved and we are redirected to our profile.  The access token will be used in every future API request for the application.
Let’s take a look at the profile template.  Add the following code to your www/templates/profile.html file:

<ion-view title="Profile" ng-init="init()">
    <ion-content>
        <div class="list card">
            <div class="item item-avatar">
                <img ng-src="{{profileData.picture.data.url}}">
                <h2>{{profileData.name}}</h2>
                <p>{{profileData.id}}</p>
            </div>
            <a href="#" class="item item-icon-left">
                <i class="icon ion-earth"></i>
                {{profileData.website}}
            </a>
            <a href="#" class="item item-icon-left">
                <i class="icon ion-home"></i>
                {{profileData.location.name}}
            </a>
            <a href="#" class="item item-icon-left">
                <i class="icon ion-heart"></i>
                {{profileData.relationship_status}}
            </a>
        </div>
    </ion-content>
</ion-view>

On the profile screen we will display the signed in users name, avatar, website, location, and relationship status. And the controller code for same HTML is-

facebookApp.controller("ProfileController", function($scope, $http, $localStorage, $location) {
     $scope.init = function() {
        if($localStorage.hasOwnProperty("accessToken") === true) {
            $http.get("https://graph.facebook.com/v2.2/me", { params: { access_token: $localStorage.accessToken, fields: "id,name,gender,location,website,picture,relationship_status", format: "json" }}).then(function(result) {
                $scope.profileData = result.data;
            }, function(error) {
                alert("There was a problem getting your profile.  Check the logs for details.");
                console.log(error);
            });
        } else {
            alert("Not signed in");
            $location.path("/login");
        }
    };
});
Thanks...

Friday, 11 March 2016

$rootScope in angularjs

Hi,

$rootScope-

Every application has a single root scope. All other scopes are descendant scopes of the root scope. Scopes provide separation between the model and the view, via a mechanism for watching the model for changes. They also provide event emission/broadcast and subscription facility. 


The $rootScope is the top-most scope. An app can have only one $rootScope which will be shared among all the components of an app. Hence it acts like a global variable. All other $scopes are children of the $rootScope.

Example-

Please run the following sets of command-

ionic start myApp blank
cd myApp
ionic platform add android


In your index.html file add following lines of code-

<div ng-controller="MyAppController" style="border:2px solid blue; padding:5px">
 Hello {{msg}}!
 <br />
 Hello {{name}}!
 (rootScope)
</div>

In your app.js file add following lines of code-

myApp.controller('MyAppController', function ($scope, $rootScope) {
 $scope.msg = 'World';
 $rootScope.name = 'AngularJS';
 });


Note-

When you use ng-model with $rootScope objects then AngularJS updates those objects under a specific $scope of a controller but not at global level $rootScope.
Create a private $scope for each controller to bind it to the view.





Thanks...






Friday, 4 March 2016

Drop down data binding from view to controller in Ionic

Hi,

Here i am sharing a way by which, we can bind data from view to controller by using drop down.

Run following sets of command in your terminal-

ionic start myApp blank
cd myApp
ionic platform add android

Add following lines of code in your index.html file-

<div ng-controller="MyAppCtrl">
<select
    ng-change="changed()"
    ng-model="regions"
    ng-options="data.name for data in options">
</select>
    <pre>{{ regions | json }}</pre>
</div>
  

And add following lines of code in your app.js file-


var myApp = angular.module('myApp',[]);

myApp.controller('MyAppCtrl',function( $scope){
 $scope.options = [
        {id: 1, name: 'A'},
        {id: 2, name: 'B'},
        {id: 3, name: 'C'}
    ];
 
    $scope.changed = function() {
        console.log($scope.regions);
    }
});
Here when you change option in your html page, you will get that value in your controller.

Thanks...
   





Friday, 19 February 2016

Device Calendar In Ionic Framework

Hi,

Here is a way by which, we can the device calendar in ionic framework.

ionic start IonicCalendar blank
cd IonicCalendar
ionic platform add android


The next step is to install the Cordova plugin for Calendar.  Using your Terminal or command prompt, run the following command:

cordova plugin add https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin.git


At this point you can use the native calendar in your application, but we are going to make easier by using ngCordova.  Download the latest ngCordova release and extract ng-cordova.min.js into your project’s www/js directory.
With the file included in your project you now need to include it in your www/index.html file:

<script src="js/ng-cordova.min.js"></script>

It is very important that the above line be included before cordova.js, otherwise we can't use the cordova plugin in our project.
Now we must include it into our angular.module directives list like the following in our www/js/app.js file:

var ionicApp = angular.module("starter", ["ionic", "ngCordova"]);

Open your www/js/app.js file and make it look like the following:

var ionicApp = angular.module('starter', ['ionic', 'ngCordova']);
 
ionicApp.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
});
 
ionicApp.controller("CalendarController", function($scope, $cordovaCalendar) {
 
    $scope.createEvent = function() {
        $cordovaCalendar.createEvent({
            title: 'Hello world',
            location: 'Home',
            notes: 'Bring sandwiches',
            startDate: new Date(2015, 0, 15, 18, 30, 0, 0, 0),
            endDate: new Date(2015, 1, 17, 12, 0, 0, 0, 0)
        }).then(function (result) {
            alert("Event created successfully");
        }, function (err) {
            alert("There was an error: " );
        });
    }
 
});

To use the createEvent() method above, open your www/index.html file and add the following code:

<ion-content ng-controller="CalendarController">
  <button class="button" ng-click="createEvent()">Create Event</button>
</ion-content>

If event created successfully then you will get alert message 'Event created successfully' otherwise you will get alert an error message like ' There was an error:'




Thanks...

Friday, 5 February 2016

Finding model,platform,UUID and manufacturer of a device in ionic

Hi,

 Create the ionic project using the below commands. Here we are creating the ionic blank project and name it is as “DeviceInformaion”

ionic start DeviceInformaion blank

cd DeviceInformaion

Then add the platform android and ios

ionic platform add android

ionic platform add ios

Then integrate the ng-Cordova library into your project.
Add the ng-cordova.min.js reference above to the cordova.js file in the index.html as mentioned in the below.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>

<body ng-app="starter">

ng-cordova integration is still not over. Open your app.js file and alter the angular.module line to look like the following.

angular.module('starter', ['ionic', 'ngCordova'])

The next thing we want to do is add the Apache Cordova device plugin. This can be done by running the following command.

cordova plugin add org.apache.cordova.device

Add get Device information button.

Then index.html file will look like-

<body ng-app="starter" ng-controller="DeviceController">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Device Information</h1>
</ion-header-bar>
<ion-content>
<div class="item item-text-wrap">
<ul class="list">
<li class="item">
Manufacturer : {{manufacturer}}
</li>
<li class="item">
Model : {{model}}
</li>
<li class="item">
Platform : {{platform}}
</li>
<li class="item">
UUID : {{uuid}}
</li>
</ul>
</div>
</ion-content>
</ion-pane>
</body>

Next create the controller for app.js file and add $ionicPlatform.ready function. Then get the device information using getDevice() function and assign the device detail values in respective list elements. Please find the full source code for app.js file in the below.

angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})

.controller('DeviceController', function($ionicPlatform, $scope, $cordovaDevice) {
$ionicPlatform.ready(function() {
$scope.$apply(function() {
// sometimes binding does not work! :/
// getting device infor from $cordovaDevice
var device = $cordovaDevice.getDevice();
$scope.manufacturer = device.manufacturer;
$scope.model = device.model;
$scope.platform = device.platform;
$scope.uuid = device.uuid;
});
});
})

Next we have to run the application using the below command lines. If you are running in emulator you can use emulate instead run.

ionic run ios
ionic run android

Once run the application it will get the device details in the ionic platform ready function.
And it will display on index.html page.



Thanks...






Friday, 29 January 2016

Popover in ionic

Hi

Popover can be created by using ion-popover-view element. This element should be added to HTML template and $ionicPopover service needs to be injected into controller.
There is a way of adding popover. fromTemplate method allows using inline template.
In your controller add the following line of code-
.controller('MyCtrl', function($scope, $ionicPopover) {
   $ionicPopover.fromTemplateUrl('popover.html', {
      scope: $scope
   }).then(function(popover) {
      $scope.popover = popover;
   });
   $scope.openPopover = function($event) {
      $scope.popover.show($event);
   };
   $scope.closePopover = function() {
      $scope.popover.hide();
   };
   //Cleanup the popover when we're done with it!
   $scope.$on('$destroy', function() {
      $scope.popover.remove();
   });
   // Execute action on hide popover
   $scope.$on('popover.hidden', function() {
      // Execute action
   });
   // Execute action on remove popover
   $scope.$on('popover.removed', function() {
      // Execute action
   });
})

Now we will add script with template to the HTML file we are using for calling popover function.

<script id = "popover.html" type = "text/ng-template">
   <ion-popover-view>
      <ion-header-bar>
         <h1 class = "title">Popover Title</h1>
      </ion-header-bar>
      <ion-content>
         Popover Content!
      </ion-content>
   </ion-popover-view>
</script>

Thanks...