//1、ABPeoplePickerNavigationControllerDelegate 的 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person; 方法:
var peoplePickerNavController = plus.ios.newObject("ABPeoplePickerNavigationController");
//实现代理方法【- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person;】
//同时生成遵守ABPeoplePickerNavigationControllerDelegate协议的代理对象peoplePickerDelegate
var peoplePickerDelegate = plus.ios.implements("ABPeoplePickerNavigationControllerDelegate", {
"peoplePickerNavigationController:didSelectPerson:":function(peoplePicker, person){
//这里的peoplePicker竟然是CNContact实例对象,person是undefined
console.log(JSON.stringify(peoplePicker));
console.log(JSON.stringify(person));
console.log(typeof person);
//所以之前的代码不用改
var contact = peoplePicker;
//姓名/公司
var name = "";
//姓氏
var familyName = contact.plusGetAttribute("familyName");
//名字
var givenName = contact.plusGetAttribute("givenName");
//公司
var organizationName = contact.plusGetAttribute("organizationName");
name = familyName+givenName;
if (name.length <= 0) {
name = organizationName;
}
//电话号码
var phoneNo = "";
var phoneNumbers = contact.plusGetAttribute("phoneNumbers");
if (phoneNumbers.plusGetAttribute("count") > 0) {
var phone = phoneNumbers.plusGetAttribute("firstObject");
var phoneNumber = phone.plusGetAttribute("value");
phoneNo = phoneNumber.plusGetAttribute("stringValue");
}
if (callBack) {
callBack(name, phoneNo);
}
}
});
//给通讯录控制器peoplePickerNavController设置代理
plus.ios.invoke(peoplePickerNavController, "setPeoplePickerDelegate:", peoplePickerDelegate);
/*
//获取当前UIWebView视图
var currentWebview = plus.ios.currentWebview();
//根据当前UIWebView视图获取当前控制器
var currentVC = nativeCommon.contacts.ios.getViewControllerByView(currentWebview);
//由当前控制器present到通讯录控制器
plus.ios.invoke(currentVC, "presentViewController:animated:completion:", peoplePickerNavController, true, null);
*/
//获取跟控制器
var rootVc = nativeCommon.contacts.ios.getRootViewController();
//由跟控制器present到通讯录控制器
plus.ios.invoke(rootVc, "presentViewController:animated:completion:", peoplePickerNavController, true, function(){
if (finishedLoad) {finishedLoad();}
});
//2、CNContactPickerDelegate 的 - (void)contactPicker:(CNContactPickerViewController )picker didSelectContact:(CNContact )contact;方法:
var contactPickerVC = plus.ios.newObject("CNContactPickerViewController");
//实现代理方法【- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;】
//同时生成遵守CNContactPickerDelegate协议的代理对象delegate
var delegate = plus.ios.implements("CNContactPickerDelegate", {
"contactPicker:didSelectContact:":function(picker, contact){
console.log(JSON.stringify(picker));
console.log(JSON.stringify(contact));
//姓名/公司
var name = "";
//姓氏
var familyName = contact.plusGetAttribute("familyName");
//名字
var givenName = contact.plusGetAttribute("givenName");
//公司
var organizationName = contact.plusGetAttribute("organizationName");
name = familyName+givenName;
if (name.length <= 0) {
name = organizationName;
}
//电话号码
var phoneNo = "";
var phoneNumbers = contact.plusGetAttribute("phoneNumbers");
if (phoneNumbers.plusGetAttribute("count") > 0) {
var phone = phoneNumbers.plusGetAttribute("firstObject");
var phoneNumber = phone.plusGetAttribute("value");
phoneNo = phoneNumber.plusGetAttribute("stringValue");
}
if(callBack){
callBack(name, phoneNo);
}
}
});
//给通讯录控制器contactPickerVC设置代理
plus.ios.invoke(contactPickerVC, "setDelegate:", delegate);
/*
//获取当前UIWebView视图
var currentWebview = plus.ios.currentWebview();
//根据当前UIWebView视图获取当前控制器
var currentVC = nativeCommon.contacts.ios.getViewControllerByView(currentWebview);
//由当前控制器present到通讯录控制器
plus.ios.invoke(currentVC, "presentViewController:animated:completion:", contactPickerVC, true, null);
*/
//获取跟控制器
var rootVc = nativeCommon.contacts.ios.getRootViewController();
//由跟控制器present到通讯录控制器
plus.ios.invoke(rootVc, "presentViewController:animated:completion:", contactPickerVC, true, function(){
if (finishedLoad) {finishedLoad();}
});
3 个回复
DCloud_iOS_XHY
NJS 只适用于简单的方法调用,5+api有提供通讯录的详情 https://www.html5plus.org/doc/zh_cn/contacts.html ,如果不满足需求,建议你通过扩展原生插件的方式实现
l***@yuan-mai.com
请问解决了吗?遇到了同样的问题
FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866
通讯录选择联系人、可单选、多选、详情(ios):https://ext.dcloud.net.cn/plugin?id=9107