Search code examples
angularjsionic-frameworkcordova-plugins

TypeError: Cannot read property 'socialsharing' of undefined


Making app on ionic and getting the error:

TypeError: Cannot read property 'socialsharing' of undefined

Kindly check my code what mistake I done .Just make a simple image sharing App where I getting the error on social sharing plugin kindly check the code below and suggest me what should I do to correct that.

.controller('imageCtrl',function($scope,$cordovaSocialSharing,$http,$window) {
    $scope.myImages = [
        {id :'1',image:'1.jpg'},
        {id :'2',image:'2.jpg'},

    ];

    $scope.getImagePath = function(imageName) {
        return "img/" + imageName.image;
    }

    $scope.doit= function(index){
        console.log(index);

    }
    $scope.share = function(t, msg, img, link){
        if(t == 'w')
            window.plugins.socialsharing
                .shareViaWhatsApp(msg, img, link);
        else if(t == 'f')
            window.plugins.socialsharing
                .shareViaFacebook(msg, img, link);
        else if(t == 't')
            window.plugins.socialsharing
                .shareViaTwitter(msg, img, link);
        else if(t == 'sms')
            window.plugins.socialsharing
                .shareViaSMS(msg+' '+img+' '+link);
        else
        {
            var sub = 'Beautiful images inside ..';
            window.plugins.socialsharing
                .shareViaEmail(msg, sub, '');
        }
    }
});

App.js

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

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

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

HTML part

<ion-content ng-controller="imageCtrl">
    <div ng-repeat="myImage in myImages" ng-click="doit($index)">
        {{myImage.id}}
        <img class ="col  item item-image thumbnail" ng-src="{{getImagePath(myImage)}}"/>
        <div class="card gallary item item-divider">
            <div class="item item-text-wrap row">
                <button ng-click="share('w', '', myImage.image, '');"
                        class="button button-light col col-20">
                    <i class="icon ion-social-whatsapp"></i>
                </button>
                <button ng-click="share('f', 'myMessage', myImage.image, '');"
                        class="button button-light col col-20">
                    <i class="icon ion-social-facebook"></i>
                </button>
                <button ng-click="share('t', 'myMessage', myImage.image, '');"
                        class="button button-light col col-20">
                    <i class="icon ion-social-twitter"></i>
                </button>
            </div>
        </div>
    </div>
</ion-content>

Check the image enter image description here


Solution

  • Try is like its documented by using $cordovaSocialSharing service. PLease note that shareViaEmail() should be canShareViaEmail(). You may going to use switch instead of this if - if else - if else - if else - else pattern:

    $scope.share = function(t, msg, img, link){
        switch (t) {
            case "w":
                $cordovaSocialSharing.shareViaWhatsApp(msg, img, link);
                break;
            case "f":
                $cordovaSocialSharing.shareViaFacebook(msg, img, link);
                break;
            case "t":
                $cordovaSocialSharing.shareViaTwitter(msg, img, link);
                break;
            case "sms":
                $cordovaSocialSharing.shareViaSMS(msg + ' ' + img + ' ' + link);
                break;
    
            default:
                $cordovaSocialSharing.canShareViaEmail(msg, 'Beautiful images inside ..', '');
                break;
        }
    };
    

    Note: This will only work on mobile devices and not in a desktop browser case.