【问题标题】:Ionic 2 Contacts Native Plugin not working as expected in real Android deviceIonic 2 Contacts Native Plugin 在真实的 Android 设备中无法正常工作
【发布时间】:2017-01-10 20:16:55
【问题描述】:

我按照本文档中的说明安装了 Ionic 2 的联系人插件:

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

该参考资料没有给我们任何关于如何使用插件的解释,也没有给出如何查找和处理错误的示例。

在模拟器中,我的应用运行良好。它列出了所有联系人。但在实际设备中,它给了我一个空错误,并且没有将联系人写入列表。

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

error 对象为空,警报打印为 {}

我通过两种不同的方式安装了应用程序:

1- sudo 离子运行 android 2- 将 android-debug.apk 复制到下载文件夹并通过包管理器安装它。我授予了请求的权限。

这两行在我的 Manifest.xml 中

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

有人知道如何调试它吗?或者这个错误可能是什么?

@edit

这是一个 Android 4.4.2,KitKat

@edit 2

我将代码放在 home.ts 的构造函数中的 ready 事件中。

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

});

【问题讨论】:

  • 可以尝试将其添加到platform.ready()
  • 查看编辑。它在 home.ts 构造函数中
  • 来自文档,类型应该是ContactFieldType
  • 嘿 Suraj,我不确定你的意思。我在智能手机中尝试了我的真实联系人列表,列表中有大约 300 个联系人
  • 是的,raj,但是我如何获得这个联系字段?我看到了传递字符串的示例,它可以在模拟器中运行。

标签: android ionic-framework ionic2 cordova-plugins


【解决方案1】:

我有类似的错误,在 console.log 中显示此消息:

异常:未捕获(承诺):TypeError:navigator.contacts 未定义 联系人

这是我的代码:

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]));
        })      
    });
  }
}

【讨论】:

  • 这不是答案。请参阅How to Answer。此页面不是留言板,而是问答网站。
  • 我正在为 Android 构建项目。在 Genymotion 模拟器中运行良好。
猜你喜欢
  • 2019-02-04
  • 2021-03-01
  • 1970-01-01
  • 2022-01-20
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多