Nativescript联系人列表插件

时间:2018-04-10 13:04:48

标签: angular plugins nativescript

我正在使用带有angular的nativescript来创建联系人列表应用程序,我正在使用nativescript-contacts插件(https://www.npmjs.com/package/nativescript-contacts)。开发是在mac high sierra上的ios仿真器上完成的。 我按照文档,一切顺利,直到我到达newContact.save();点,我的应用程序变黑,然后退出。当.save()被注释掉时,我可以控制日志记录newContact对象并看到它已正确设置。

我的组件如下:

import { Component, OnInit, ViewChild } from "@angular/core";
import { Router } from '@angular/router’

import { NewCardService } from './newcard.service';
import * as Toast from 'nativescript-toast';

@Component({
 selector: "Contact",
 moduleId: module.id,
 templateUrl: "./contact.component.html",
 styleUrls: ["./contact-common.css", "./contact.component.css"]
})
export class ContactComponent implements OnInit {
    public first_name: string="";
    public last_name: string="";
    public profession: string="";
    public phone: number = null;
    public email: string="";


constructor( private router: Router,
             private appStorage: AppStorage,
             private newCardService: NewCardService
) { }

ngOnInit(): void {    }

submit(){
    const date = new Date();
    const newContact = {
        id:  this.last_name + date.getTime(),
        first_name : this.first_name,
        last_name : this.last_name,
        profession : this.profession,
        phone: this.phone,
        email: this.email
    }
    this.newCardService.saveToContactList(newContact)
    const toast = Toast.makeText("Saved!");
    toast.show();
    this.router.navigate(['home'])
}
}

保存联系人的服务如下:

import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';

import * as contacts from "nativescript-contacts";
import * as imageSource from "image-source" ;

@Injectable()
export class ContactService {

constructor(  private http: Http  ) { }

saveToContactList(contact) {
    var newContact = new contacts.Contact();
    newContact.name.given = contact.first_name;
    newContact.name.family = contact.last_name;
    newContact.phoneNumbers.push({ label: contacts.KnownLabel.WORK, value: contact.phone });
    newContact.organization = contact.profession
    newContact.photo = imageSource.fromFileOrResource(contact.cardPictureURL);
    newContact.save();
}
}

我试过寻找这个问题的解决方案,没有运气。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

这可能是因为最近的iOS版本中有更严格的权限请求。尝试在app/App_Resources/iOS/Info.plist

中添加以下内容
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access for whatever reason.</string>