简体   繁体   English

Ionic 2 Contacts本机插件在实际的Android设备中无法正常工作

[英]Ionic 2 Contacts Native Plugin not working as expected in real Android device

I installed the Contacts plugin for Ionic 2 as explained in this documentation: 如本文档所述,我为Ionic 2安装了Contacts插件:

https://ionicframework.com/docs/v2/native/contacts/ https://ionicframework.com/docs/v2/native/contacts/

The reference don't give us any explanation on how to use the plugin and no example of how to find and handle errors. 该参考资料没有给我们任何有关如何使用插件的解释,也没有提供有关如何查找和处理错误的示例。

In an emulator my app works just fine. 在模拟器中,我的应用程序可以正常运行。 It lists all the contacts. 它列出了所有联系人。 But in real device it gives me an empty error and no contact is written to the list. 但是在实际设备中,它给我一个空错误,并且没有联系人被写入列表。

Contacts.find(['phoneNumbers']).then((phone_contacts) => {
   self.contacts = {};
  // Some code
}).catch((error) => {
   alert('Contacts error: '+JSON.stringify(error));
});

The error object is empty and alert prints {} . error对象为空,警报显示{}

I installed the app by two different ways: 我通过两种不同的方式安装了该应用程序:

1- sudo ionic run android 2- copying the android-debug.apk to downloads folder and install it via package manager. 1- sudo ionic运行android 2-复制android-debug.apk到downloads文件夹并通过包管理器安装。 I granted the requested permissions. 我授予了请求的权限。

These two lines are in my Manifest.xml 这两行在我的Manifest.xml中

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

Anyone got a clue on how I can debug it? 任何人都知道如何调试它? Or what could be this error? 还是这个错误可能是什么?

@edit @编辑

It's a Android 4.4.2, KitKat 这是Android 4.4.2,KitKat

@edit 2 @edit 2

I put the code in my home.ts , in constructor, in the ready event. 我将代码放置在home.ts ,构造函数中的ready事件中。

platform.ready().then(() => {

});

I have a similar error, in the console.log show this message: 我有一个类似的错误,在console.log中显示以下消息:

EXCEPTION: Uncaught (in promise): TypeError: navigator.contacts is undefined Contacts 例外:未捕获(承诺):TypeError:navigator.contacts是未定义的联系人

This is my code: 这是我的代码:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { Contacts } from 'ionic-native';
import { HomePage } from '../pages/home/home';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage = HomePage;

  constructor(platform: Platform) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();

    Contacts.find(['displayName','name','nickname'], {filter:''})
      .then(
        cont=>{
          console.log(cont[0]);
          alert(JSON.stringify(cont[0]));
        })      
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM