Search code examples
androidangularjscordovaionic-frameworksocialshare

Ionic: $cordovaSocialSharing cannot read property of undefined


I want to use this plugin: $cordovaSocialSharing since it's the only plugin I've found for native social buttons on Ionic...

I get this error when I'm running my code in Chrome console: "Cannot read property 'socialsharing' of undefined" @ (ng-cordova.js:6715)

I reinstalled ng-Cordova as it says in ngcordova.com and the plugin multiple times but it seems it doesnt' work...I tried in an Android emulator and neither does.

Here my code: In my controllers.js:

angular.module('starter.controllers', ['ionic', 'ionic-ratings', 'onezone-datepicker', 'ngCordova'])

then,

.controller('CaravanDetailCtrl', function($rootScope, $scope, $cordovaSocialSharing, sweetAlert) {
   $scope.socialsharingFacebook = function() {

            $cordovaSocialSharing
                .shareViaFacebook("msg", "img", "url")
                .then(function(result) {
                    SweetAlert.swal({   
                        title: "",   
                        text: "success",   
                        type: "success",   
                        showCancelButton: false,   
                        confirmButtonColor: "rgba(5, 60, 84, 0.8)",   
                        confirmButtonText: "OK",   
                        closeOnConfirm: true 
                    });
                }, function(err) {
                    SweetAlert.swal({   
                        title: "",   
                        text: "sorry",   
                        type: "error",   
                        showCancelButton: false,   
                        confirmButtonColor: "rgba(5, 60, 84, 0.8)",   
                        confirmButtonText: "OK",   
                        closeOnConfirm: true 
                    });
                });
    }

}

I tried with this too (in controllers.js):

$ionicPlatform.ready(function() {
   $cordovaSocialSharing
            .shareViaFacebook("msg", "img", "url")
            .then(function(result) {
                SweetAlert.swal({   
                    title: "",   
                    text: "success",   
                    type: "success",   
                    showCancelButton: false,   
                    confirmButtonColor: "rgba(5, 60, 84, 0.8)",   
                    confirmButtonText: "OK",   
                    closeOnConfirm: true 
                });
            }, function(err) {
                SweetAlert.swal({   
                    title: "",   
                    text: "sorry",   
                    type: "error",   
                    showCancelButton: false,   
                    confirmButtonColor: "rgba(5, 60, 84, 0.8)",   
                    confirmButtonText: "OK",   
                    closeOnConfirm: true 
                });
            });
}

Any idea how to solve this or alternatively, any idea of another plugin that actually works on Ionic?


Solution

  • You need to use real device, most Cordova plugins are not available in browser.

    Detect That Your Application Is Running in Cordova/Phonegap

      if (window.cordova) {
         $cordovaSocialSharing
              .shareViaFacebook(message, image, link)
              .then(function(result) {
          // Success!
         }, function(err) {
             // An error occurred. Show a message to the user
         });
      } else {
        console.log('Not a device');
      }