离子cordova社交分享插件 – 无法读取未定义的属性“社交分享”

所以我已经在这个3天了…试了几个这样的教程,并尝试在这里的原始页面,并阅读GitHub描述千次。

我正在使用的版本:

$ npm -v 3.7.3 $ cordova -v 6.1.0 (cordova-lib@undefined) $ ionic -v 1.7.14 

我在Chrome浏览器中遇到的错误: 无法读取未定义的属性“社交分享” 。 在android或ios手机上,如果按下button,则不会发生任何事情。 甚至没有错误的函数调用。

app.js +控制器:(注意我正在尝试使用window.plugins,而且也没有使用.plugins!)

 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); cordova.plugins.Keyboard.disableScroll(true); } if (window.StatusBar) { // org.apache.cordova.statusbar required StatusBar.styleDefault(); } }); }) .controller('shareCtrl',['$scope',function($scope) { $scope.facebookShare=function(){ window.socialsharing.shareViaFacebook('Digital Signature Maker', null /* img */, "https://play.google.com/store/apps/details?id=com.prantikv.digitalsignaturemaker" /* url */, null, function(errormsg){alert("Error: Cannot Share")}); } $scope.whatsappShare=function(){ window.plugins.socialsharing.shareViaWhatsApp('Digital Signature Maker', null /* img */, "https://play.google.com/store/apps/details?id=com.prantikv.digitalsignaturemaker" /* url */, null, function(errormsg){alert("Error: Cannot Share")}); 

}

我试图安装甚至手动,这里有一个不同的错误。 当我将SocialSharing.js插入到index.html(cordova.js之前)时,Chrome控制台表示: 未捕获的ReferenceError:require未定义 ,位于SocialSharing.js的第1行:

 var cordova = require('cordova'); 

所以我已经安装require.sj(npm install -g requirejs)成功,然后手动尝试,但出现了不同types的错误。

尝试重新安装插件或删除和添加平台,但没有改变。

不知道是否相关,但试图使用ngCordova的$ cordovaFileOpener2,并总是给cordova未定义的错误。 (也没有在实际的电话工作)

感谢任何帮助! 谢谢

更新:我粘贴了上面的所有.js文件内容。

请注意,我已经成功安装了共享插件:

 cordova plugin add cordova-plugin-x-socialsharing Fetching plugin "cordova-plugin-x-socialsharing" via npm Installing "cordova-plugin-x-socialsharing" for android 

这是我的index.html:

 <!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"> <!-- ionic/angularjs js --> <script src="lib/ionic/js/ionic.bundle.js"></script> <!-- cordova script (this will be a 404 during development) --> <script src="lib/ng-cordova.min.js"></script> <script src="cordova.js"></script> <!-- your app's js --> <script src="js/app.js"></script> </head> <body ng-app="starter"> <ion-view view-title="Share"> <ion-content ng-controller="shareCtrl"> <div class="card"> <div class="item item-divider"> <b> Share this app</b> </div> <ul class="list"> <li class="item" id="displayLabel"> <button class="button button-block button-royal item-icon-left" ng-click="whatsappShare()"> <i class="icon ion-social-whatsapp"></i> WhatsApp </button> </li> <li class="item" id="displayLabel"> <button class="button button-block button-royal item-icon-left" ng-click="facebookShare()"> <i class="icon ion-social-twitter"></i> facebook </button> </li> <li class="item" id="displayLabel"> <button class="button button-block button-royal item-icon-left" ng-click="OtherShare()"> <i class="icon ion-android-share-alt"></i> Other </button> </li> </ul> </div> </ion-content> </ion-view> </body> </html> 

实际上,插件提供对基于Web的应用程序通常不可用的设备和平台function的访问。 所以你可能不能在浏览器上实现插件function。

您可以使用插件或ngCordova.js文件在设备上(android / ios / emulator)实现

使用插件

安装: cordova插件添加cordova-plugin-x-socialsharing

用法:

 .controller('ShareCtrl', function ($scope) { $scope.whatsappShare = function(){ if(window.plugins.socialsharing) { window.plugins.socialsharing.canShareVia('whatsapp', 'msg', null, null, null, function (e) { //do something }, function (e) { //error occured }); } } }) 

使用ng-Cordova

安装:将ng-cordova文件添加到项目中,并将其包含在index.html文件中

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

用法

 .controller('ShareCtrl', function ($scope,$cordovaSocialSharing) { $scope.shareByWhatsApp = function() { $cordovaSocialSharing .shareViaWhatsApp('sharedMsg', "", shareAppLink) .then(function(result) { }, function(err) { // An error occurred. Show a message to the user alert("error : "+err); }); }; })