javascript - 联系人插件 cordova 没有获取联系人也没有给出任何错误?

标签 javascript cordova ionic-framework phonegap-plugins cordova-plugins

我对混合应用程序开发的概念很陌生,并且不习惯任何脚本语言(例如 javascript)。

我正在尝试通过 cordova 联系人选择器插件访问设备电话簿,但我没有收到任何错误,也没有实现所需的功能。

这是我调用插件 api 的 View 部分。

<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">


<link href="../css/radio.css" rel="stylesheet">
  <!--<script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>-->


 <!-- ionic/angularjs js -->
    <script src="../lib/ionic/js/ionic.bundle.js"></script>

    <!-- your app's js -->
    <script src="../js/emergency.js"></script>

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


  </head>
  <body ng-app="emergency">



<ion-view title="My Profile - Emergency" ng-controller="SmsCtrl">
    <ion-content overflow-scroll="true" padding="true" class="has-header">
        <form class="list">
            <label class="item item-input">
                <span class="input-label"></span><textarea placeholder=""> I am in danger</textarea>
            </label>
        </form>
        <div class="spacer" style="width: 285px; height: 34px;"></div>

        <button class="button button-light button-icon button-small icon ion-android-add-circle" ng-click="doContactPicker()">Emergency contacts</button>
<div id="contactFetched"></div>


    </ion-content>
</ion-view>


  </body>
</html>

我编写的两个js文件是emergency.js和services.js

这是我的emergency.js,它由调用联系人选择器服务的 Controller 组成:-

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var finalContacts="";
var contactCount=0;
angular.module('emergency', ['ionic','ngCordova','NameService'])

.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('SmsCtrl', ['$scope','$ionicPlatform','$cordovaSms','ContactPicker', function ($scope, $ionicPlatform, $cordovaSms, ContactPicker) {
  console.log('enetered in ctrl');
  $scope.form={}
$scope.counter=contactCount;
/*
calling the contact picker service : ContactPicker which returns the merged contact details of the contacts picked
*/
$scope.doContactPicker=function() {
console.log(ContactPicker);
$scope.answer='answer';
$scope.answer =ContactPicker.pickedContact();

 $scope.$watch('answer', function() {
        document.getElementById("contactFetched").innerHTML =$scope.answer;
alert('sample alert displayed');
 });

};



/*
function to add contact data to the array of items
gentrating new div items on button click
*/
 $scope.users = [];

        $scope.add = function () {
          $scope.users.push({ 
            firstName: "",
            email: "",
            mobile: ""
          });
        };


/*
function to send sms using cordova message plugin api
input : form.number and form.message
*/
  $scope.sendSms = function(){
    console.log($scope.form.number);
    console.log($scope.form.message);
     var options = {
            replaceLineBreaks: false, // true to replace \n by a new line, false by default
            android: {
                intent: ''  // send SMS with the native android SMS messaging
                //intent: '' // send SMS without open any other app
            }
        };
    $ionicPlatform.ready(function(){
      $cordovaSms
      .send($scope.form.number, $scope.form.message, options)
      .then(function(result) {
        console.log(result);

      }, function(error) {
        console.log(error);
      })
    })
  }

}]);

最后是services.js文件

var finalContacts="";
var nameService=angular.module('NameService',[]);



nameService.service('getName',function() {
console.log("service created");
 this.nameFetched=function getUserName(c) {
console.log("inside picked contact");
    var name =c; 
    return name;
}
});



nameService.service('ContactPicker',function() {
console.log("service created");
this.pickedContact=function() {
console.log("inside picked contact");
//alert("inside");
    navigator.contacts.pickContact(function(contact){
//alert("inside");
//      console.log('The following contact has been selected:' + JSON.stringify(contact));
//alert(JSON.stringify(contact));
        //Build a simple string to display the Contact - would be better in Handlebars
        var s = "";
        //s += "<h2>"+getName.nameFetched('yatin data')+"</h2>";

        if(contact.emails && contact.emails.length) {
            s+= "Email: "+contact.emails[0].value+"<br/>";
        }

        if(contact.phoneNumbers && contact.phoneNumbers.length) {
            s+= "Phone: "+contact.phoneNumbers[0].value+"<br/>";
        }

        if(contact.photos && contact.photos.length) {
            s+= "<p><img src='"+contact.photos[0].value+"'></p>";
        }

        finalContacts+=s;

        //$("#selectedContact").html("hello world");

    },function(err){
        alert('Error: ' + err);
        console.log('Error: ' + err);

    });
return finalContacts;
}

});

控件在此函数调用时中断 navigator.contacts.pickContact(function(contact){

使用 ionic 服务,我在浏览器上测试了它,但由于所选的联系功能可以在设备本身上运行,因此我在浏览器中收到以下错误:-

TypeError: Cannot read property 'pickContact' of undefined

但是使用设备调试选项,我无法到达 navigator.contacts.pickContact(function(contact){ 内部,即不显示其中的警报。 请帮我解决这个问题。 谢谢

最佳答案

首先,cordova.js 的引用在哪里?即:

<script src="cordova.js"></script>

那么,你是否正确安装了 cordova-plugin-contacts

Cordova 插件仅在设备上可用,而不在浏览器中可用,因此您必须在设备上安装构建的应用程序,将其连接到计算机并通过 USB 进行调试(具体方式取决于特定平台 iOS/Android/...)。

关于javascript - 联系人插件 cordova 没有获取联系人也没有给出任何错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34721079/

相关文章:

javascript - 使用 javascript/jquery 进行复选框操作

javascript - polymer 含量插入点?

javascript - 在 cordova 移动应用程序上流式传输 Soundcloud

ios - Cordova /PhoneGap : Linker Error after installing calendarPlugin

javascript - $scope.$apply() 在状态导航后不工作

javascript - classList.add 已停止运行我的代码。我不知道为什么。你可以吗?

javascript - 禁用编辑不起作用

javascript - 从切换列表中获取值

ionic-framework - 如何使用 Ionic 2 下载文件到公共(public)目录?

javascript - blueimp Gallery 无边框缩略图