Share images, text, messages via Facebook, Twitter, Email, SMS, WhatsApp, etc With ng-cordova in Ionic Framework
PhoneGap Social Sharing for Android, iOS and Windows Phone
The following Content will show you how to make use of social media sharing in your Android and Windows and iOS mobile application using Ionic Framework.
To implement this , we’ll be using the AngularJS extension set, ngCordova, along with the Apache Cordova plugin SocialSharing.
1.Create the project
ionic start Test blank
cd Test
ionic platform add android
2.Add Plugin (Share images, text, messages via Facebook, Twitter, Email, SMS, WhatsApp, etc using this plugin.)
cordova plugin add https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git
3.Implementation
Download the latest stable ngCordova release and copy ng-cordova.js to your www/js directory. With the library in place, you now need to add it to your projects index.html file similar to the following:
<!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>
<!-- ng-cordova -->
<script src="js/ng-cordova.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>
<script src="js/controllers.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content ng-controller="ShareController">
<br><br>
<button class="button" ng-click="shareAnywhere()">Share anywhere</button>
<!-- Place Your Image file www/img directory -->
<button class="button" ng-click="shareViaTwitter('Demo message', 'www/img/image1.jpeg', 'http://santoshshinde2012.blogspot.com')">Share on Twitter</button>
<br><br>
<button class="button" ng-click="shareViaWhatsApp('Demo message', 'www/img/image1.jpeg', 'http://santoshshinde2012.blogspot.com')">Share on WhatsApp</button>
<button class="button" ng-click="shareViaFacebook('Demo message', 'www/img/image1.jpeg', 'http://santoshshinde2012.blogspot.com')">Share on Facebook</button>
</ion-content>
</ion-pane>
</body>
</html>
Now need to add it to your projects app.js file similar to the following:
var app = angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
Now need to add it to your projects Controllers.js file similar to the following:
app.controller("ShareController", function($scope, $cordovaSocialSharing) {
$scope.shareAnywhere = function() {
$cordovaSocialSharing.share("My Demo Message", "My Demo Subject", "www/img/image1.jpeg", "http://santoshshinde2012.blogspot.com");
}
$scope.shareViaTwitter = function(message, image, link) {
$cordovaSocialSharing
.shareViaTwitter(message, image, link)
.then(function(result) {
// Success!
alert("success : "+result);
}, function(err) {
// An error occurred. Show a message to the user
alert("Cannot share on Twitter");
});
}
$scope.shareViaWhatsApp = function(message, image, link) {
$cordovaSocialSharing
.shareViaWhatsApp(message, image, link)
.then(function(result) {
alert(result);
// Success!
}, function(err) {
// An error occurred. Show a message to the user
alert("Cannot share on WhatsApp");
});
}
$scope.shareViaFacebook = function(message, image, link) {
$cordovaSocialSharing
.shareViaFacebook(message, image, link)
.then(function(result) {
// Success!
}, function(err) {
// An error occurred. Show a message to the user
alert("Cannot share on Facebook");
});
}
// access multiple numbers in a string like: '0612345678,0687654321'
$scope.shareViaSMS = function(message, number) {
$cordovaSocialSharing
.shareViaSMS(message, number)
.then(function(result) {
alert(result);
// Success!
}, function(err) {
// An error occurred. Show a message to the user
});
}
// TO, CC, BCC must be an array, Files can be either null, string or array
$scope.shareViaFacebook = function(message, subject, toArr, bccArr, file) {
$cordovaSocialSharing
.shareViaEmail(message, subject, toArr, bccArr, file)
.then(function(result) {
// Success!
}, function(err) {
// An error occurred. Show a message to the user
});
}
$scope.shareViaFacebook = function(socialType, message, image, link) {
$cordovaSocialSharing
.canShareVia(socialType, message, image, link)
.then(function(result) {
// Success!
}, function(err) {
// An error occurred. Show a message to the user
});
}
});
Comments
Post a Comment