如何在ios xamarin中获取所有联系人记录
我想获取iOS Xamarin中的所有联系人记录。 我使用了代码--> 代码:如何在ios xamarin中获取所有联系人记录,ios,xamarin,fetch,contact,Ios,Xamarin,Fetch,Contact,我想获取iOS Xamarin中的所有联系人记录。 我使用了代码--> 代码: public override void ViewDidLoad() { base.ViewDidLoad(); Util.CurrentView = this; _View = this; var predicate = CNContact.GetPredicateForContacts("Appleseed"); v
public override void ViewDidLoad()
{
base.ViewDidLoad();
Util.CurrentView = this;
_View = this;
var predicate = CNContact.GetPredicateForContacts("Appleseed");
var fetchKeys = new NSString[] { CNContactKey.GivenName, CNContactKey.FamilyName };
var store = new CNContactStore();
NSError error;
var contacts = store.GetUnifiedContacts(predicate, fetchKeys, out error);
}
错误代码:
此错误:Foundation.MonoTouchException:Objective-C异常
扔。名称:NSInvalidArgumentException原因:+[CNContact]
PredicateForContactsMachingName::发送到的选择器无法识别
等级0x1a3e1ec
我已经添加了[Export(“predicateForContactsMachingName:”)]
,但没有帮助。“Appleseed”是示例中使用的搜索词。您的不匹配似乎是因为没有一个联系人与谓词匹配
无论如何,在我自己的实现过程中,我遇到了许多问题。下面是在iOS Xamarin中获取所有联系人的完整解决方案
首先:在info.plist中添加权限
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>
NSContactsUsageDescription
此应用需要联系人访问才能正常运行。
第二:为联系人信息创建模型
在下面的示例中,我只添加了3个字段
using System.Collections;
/// <summary>
///
/// </summary>
namespace YourNameSpace
{
/// <summary>
///
/// </summary>
public class UserContact
{
public UserContact()
{
}
/// <summary>
///
/// </summary>
/// <param name="givenName"></param>
/// <param name="familyName"></param>
/// <param name="emailId"></param>
public UserContact(string givenName, string familyName, IList emailId)
{
GivenName = givenName;
FamilyName = familyName;
EmailId = emailId;
}
public bool IsSelected { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public IList EmailId { get; set; }
}
}
使用系统集合;
///
///
///
名称空间YourNameSpace
{
///
///
///
公共类用户联系人
{
公共用户联系人()
{
}
///
///
///
///
///
///
公共用户联系人(字符串givenName、字符串familyName、IList emailId)
{
GivenName=GivenName;
FamilyName=FamilyName;
EmailId=EmailId;
}
公共布尔值被选为{get;set;}
公共字符串GivenName{get;set;}
公共字符串FamilyName{get;set;}
公共IList电子邮件ID{get;set;}
}
}
第三:读取联系人
public IEnumerable<UserContact> GetAllContactsAndEmails()
{
var keysTOFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses };
NSError error;
CNContact[] contactList;
var ContainerId = new CNContactStore().DefaultContainerIdentifier;
using (var predicate = CNContact.GetPredicateForContactsInContainer(ContainerId))
using (var store = new CNContactStore())
{
contactList = store.GetUnifiedContacts(predicate, keysTOFetch, out error);
}
var contacts = new List<UserContact>();
foreach (var item in contactList)
{
if (null != item && null != item.EmailAddresses)
{
contacts.Add(new UserContact
{
GivenName = item.GivenName,
FamilyName = item.FamilyName,
EmailId = item.EmailAddresses.Select(m => m.Value.ToString()).ToList()
});
}
}
return contacts;
}
public IEnumerable GetAllContactsAndEmails()
{
var keystefetch=new[]{CNContactKey.GivenName,CNContactKey.FamilyName,CNContactKey.EmailAddresses};
n误差;
CNContact[]联系人列表;
var ContainerId=新的CNContactStore().DefaultContainerIdentifier;
使用(var predicate=CNContact.GetPredicateForContactsInContainer(ContainerId))
使用(var store=new CNContactStore())
{
contactList=store.GetUnifiedContacts(谓词、keysteFetch、out错误);
}
var contacts=新列表();
foreach(联系人列表中的var项)
{
if(null!=项目&&null!=项目.EmailAddresses)
{
添加(新用户联系人)
{
GivenName=item.GivenName,
FamilyName=item.FamilyName,
EmailId=item.EmailAddresses.Select(m=>m.Value.ToString()).ToList()
});
}
}
返回联系人;
}
确保在keystefetch数组中包含所需的联系人属性无法复制。