List <CNContact> GetIosContacts() { var result = new List <CNContact>(); using (var store = new CNContactStore()) { NSError error; var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, _keys, out error); result.AddRange(containerResults); } } catch (Exception ex) { ExceptionHandler.HandleException(ex); } } } return(result); }
public IEnumerable <CNContact> GetAllRaw(NSString[] keys) { List <CNContact> results = new List <CNContact>(); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out var error); if (error != null) { throw new Exception($"iOS Exception: {error}"); } foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keys, out error); results.AddRange(containerResults); } } catch (Exception ex) { Console.WriteLine($"DeviceContacts_iOS Error: {ex}"); // ignore missed contacts from errors } } } return(results); }
/// <summary> /// Gets contact in main thread /// !!!Not Recommended /// </summary> public IEnumerable <Contact> GetContactList(Func <Contact, bool> filter = null) { //try //{ var keysToFetch = new[] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.PhoneNumbers, CNContactKey.ImageDataAvailable, CNContactKey.ThumbnailImageData }; NSError error; //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, you can specify that here var contactList = new List <CNContact>(); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch (Exception ex) { Debug.WriteLine("\n\n\n" + ex.ToString() + "\n\n\n"); if (ex.GetType() != typeof(NullReferenceException)) { Debug.WriteLine(ex.ToString()); } continue; } } } //var contacts = new List<Contact>(); var result = new List <Contact>(); foreach (var item in contactList) { if (item.GivenName == null) { continue; } Contact _contact = new Contact(); if (filter != null && !filter(_contact)) { continue; } result.Add(_contact); } return(result); }
public async Task <IEnumerable <PhoneContact> > GetAllContactsAsync() { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers }; NSError error; //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, // you can specify that here var contactList = new List <CNContact>(); await Task.Run(() => { using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch { // ignore missed contacts from errors } } } }); var contacts = new List <PhoneContact>(); foreach (var item in contactList) { var numbers = item.PhoneNumbers; if (numbers != null) { foreach (var item2 in numbers) { contacts.Add(new PhoneContact { FirstName = item.GivenName, LastName = item.FamilyName, PhoneNumber = item2.Value.StringValue }); } } } return(contacts); }
public async Task <IEnumerable <PhoneContact> > GetAllContacts() { //Permission if (!(await PermissionGranting())) { return(new List <PhoneContact>()); } //Contacts var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.ThumbnailImageData }; //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, you can specify that here var contactList = new List <CNContact>(); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out NSError error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch (Exception e) { Console.WriteLine(e.Message); } // ignore missed contacts from errors } } var contacts = new List <PhoneContact>(); foreach (var item in contactList) { var emails = item.EmailAddresses; if (emails != null) { try { var stream = item.ThumbnailImageData?.AsStream(); contacts.Add(new PhoneContact { FirstName = item.GivenName, LastName = item.FamilyName, FullName = $"{item.GivenName} {item.FamilyName}", Email = (emails.GetLength(0) > 0) ? emails[0].Value : String.Empty, PhotoThumbnail = "ic_user"//Xamarin.Forms.ImageSource.FromStream(() => stream) }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } return(contacts); }
private static IEnumerable <CNContact> ReadRawContactList(IEnumerable <NSString> keysToFetch) { //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, you can specify that here using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out _); return(ContactListFromAllContainers(keysToFetch, allContainers, store)); } }
public IEnumerable <Contact> GetContacts() { var keys = CNContactFormatter.GetDescriptorForRequiredKeys(CNContactFormatterStyle.FullName); var keysToFetch = new [] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.PhoneNumbers, CNContactKey.ImageData, CNContactKey.ImageDataAvailable, CNContactKey.ThumbnailImageData, }; NSError error; //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, you can specify that here var contactList = new List <CNContact> (); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch (Exception ex) { continue; } } foreach (var item in contactList) { if (item.GivenName == null) { continue; } var image = item.ImageDataAvailable ? CacheImage(item.ImageData) : null; var thumb = CacheImage(item.ThumbnailImageData); var formatterKey = new [] { CNContactFormatter.GetDescriptorForRequiredKeys(CNContactFormatterStyle.FullName) }; var altItem = store.GetUnifiedContact <ICNKeyDescriptor> (item.Identifier, formatterKey, out error); var contact = new Contact() { FirstName = item.GivenName, LastName = item.FamilyName, FullName = CNContactFormatter.GetStringFrom(altItem, CNContactFormatterStyle.FullName), ImagePath = image, ThumbnailPath = thumb }; contact.Numbers.AddRange(ToPhoneNumbers(item.PhoneNumbers)); yield return(contact); } } }
public IEnumerable <Contact> GetAllContacts() { var contacts = new List <Contact>(); // Define fields to be searched var fetchKeys = new NSString[] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.Nickname, CNContactKey.ImageData, CNContactKey.PhoneNumbers }; NSError error; store = new CNContactStore(); if (CNContactStore.GetAuthorizationStatus(CNEntityType.Contacts) != CNAuthorizationStatus.Denied) { var cnContainers = store.GetContainers(null, out error); if (cnContainers != null) { foreach (var container in cnContainers) { // Create predicate to locate requested contact var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier); var cnContacts = store.GetUnifiedContacts(predicate, fetchKeys, out error); if (CNContactStore.GetAuthorizationStatus(CNEntityType.Contacts) == CNAuthorizationStatus.Authorized) { foreach (var c in cnContacts) { var con = new Contact(); con.Email = new List <string>(); foreach (var email in c.EmailAddresses) { con.Email.Add(email.ToString()); } con.PhoneNumber = new List <string>(); foreach (var phone in c.PhoneNumbers) { con.PhoneNumber.Add(phone.ToString()); } con.ID = c.Identifier; con.FirsName = c.GivenName; con.LastName = c.FamilyName; con.ImageSource = c.ImageData != null?ImageSource.FromStream(() => c.ImageData.AsStream()) : null; con.DisplayName = c.GivenName + " " + c.FamilyName; con.NickName = c.Nickname; contacts.Add(con); } } } } } return(contacts); }
public Task <List <ContactModel> > GetContactListAsync() { var contactList = new List <ContactModel>(); try { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers }; using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out var _); foreach (var container in allContainers) { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out _); foreach (var contact in containerResults) { var name = string.Concat(contact.GivenName, " ", contact.FamilyName); for (var i = 0; i < contact.PhoneNumbers.Length; i++) { var number = contact.PhoneNumbers[i]; contactList.Add(new ContactModel { Name = string.Concat(name, i > 0 ? $" {i + 1}" : ""), Number = number.Value.StringValue }); } } } } } } catch (Exception e) { Debug.WriteLine($"--- Error: {e.StackTrace}"); } return(Task.FromResult(contactList)); }
public string sendMsg() { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers }; NSError error; bool check = false; using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); foreach (var item in containerResults) { var num = item.PhoneNumbers; foreach (var cn in num) { string mob = cn.Value.StringValue; if (mob == ProfileContentView.phone) { check = true; } } } } } catch { } } } if (check == false) { var store = new CNContactStore(); var contact = new CNMutableContact(); var cellPhone = new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.Mobile, new CNPhoneNumber(ProfileContentView.phone)); var phoneNumber = new[] { cellPhone }; contact.PhoneNumbers = phoneNumber; contact.GivenName = ProfileContentView.name; var saveRequest = new CNSaveRequest(); saveRequest.AddContact(contact, store.DefaultContainerIdentifier); } return("1"); }
public List <Contact> GetContactList() { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers, CNContactKey.EmailAddresses }; NSError error; var contactList = new List <CNContact>(); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch (Exception) { // ignore missed contacts from errors throw; } } } var contacts = new List <Contact>(); foreach (var item in contactList) { var numbers = item.PhoneNumbers; if (numbers != null) { foreach (var item2 in numbers) { contacts.Add(new Contact { Name = item.GivenName, Number = item2.Value.StringValue, }); } } } return(contacts); }
public Task <IEnumerable <Contact> > GetAllAsync(CancellationToken cancellationToken) { var keys = new[] { CNContactKey.Identifier, CNContactKey.NamePrefix, CNContactKey.GivenName, CNContactKey.MiddleName, CNContactKey.FamilyName, CNContactKey.NameSuffix, CNContactKey.EmailAddresses, CNContactKey.PhoneNumbers, CNContactKey.Type }; var store = new CNContactStore(); var containers = store.GetContainers(null, out _); if (containers == null) { return(Task.FromResult <IEnumerable <Contact> >(Array.Empty <Contact>())); } return(Task.FromResult(GetEnumerable())); IEnumerable <Contact> GetEnumerable() { foreach (var container in containers) { using var pred = CNContact.GetPredicateForContactsInContainer(container.Identifier); var contacts = store.GetUnifiedContacts(pred, keys, out var error); if (contacts == null) { continue; } foreach (var contact in contacts) { yield return(ConvertContact(contact)); } } } }
public IList <Contact> GetContactList() { try { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses }; NSError error; //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, you can specify that here var contactList = new List <CNContact>(); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch (Exception ex) { Debug.WriteLine("\n\n\n" + ex.ToString() + "\n\n\n"); if (ex.GetType() != typeof(NullReferenceException)) { Debug.WriteLine(ex.ToString()); } continue; } } } var contacts = new List <Contact>(); foreach (var item in contactList) { if (item.GivenName == null) { continue; } Contact _contact = new Contact(); _contact.Name = item.GivenName + " " + item.FamilyName; if (item.PhoneNumbers != null) { foreach (var number in item.PhoneNumbers) { _contact.Number = number?.Value?.ToString(); _contact.Numbers.Add(number?.Value?.ToString()); } } if (item.EmailAddresses != null) { foreach (var email in item.EmailAddresses) { _contact.Email = email?.Value?.ToString(); _contact.Emails.Add(email?.Value?.ToString()); } } contacts.Add(_contact); } return(contacts); } catch (Exception ex) { Debug.WriteLine("\n\n\n" + ex.ToString() + "\n\n\n"); return(new List <Contact>()); } }
public List <ContactInfo> GetContactInfo() { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers, CNContactKey.PostalAddresses }; //var containerId = new CNContactStore().DefaultContainerIdentifier; // using the container id of null to get all containers. // If you want to get contacts for only a single container type, you can specify that here var contactList = new List <CNContact>(); using (var store = new CNContactStore()) { var allContainers = store.GetContainers(null, out NSError error); foreach (var container in allContainers) { try { using (var predicate = CNContact.GetPredicateForContactsInContainer(container.Identifier)) { var containerResults = store.GetUnifiedContacts(predicate, keysToFetch, out error); contactList.AddRange(containerResults); } } catch { // ignore missed contacts from errors } } } var contacts = new List <ContactInfo>(); foreach (var item in contactList) { ContactInfo info = new ContactInfo { Name = item.GivenName + " " + item.FamilyName }; info.Name = info.Name.Trim(); if (item.PhoneNumbers != null && item.PhoneNumbers.Length != 0) { List <PhonesSave> phones = new List <PhonesSave>(); foreach (var i in item.PhoneNumbers) { string label = i.Label.Substring(4); label = label.Remove(label.Length - 4); phones.Add(new PhonesSave { Label = label, Phone = i.Value.StringValue }); } info.Phones = JsonConvert.SerializeObject(phones.ToArray()); } else { info.Phones = "{}"; } if (item.PostalAddresses != null && item.PostalAddresses.Length != 0) { List <AddressSave> address = new List <AddressSave>(); foreach (var i in item.PostalAddresses) { string s = i.Value.Country; s += " " + i.Value.State; s += " " + i.Value.City; s += " " + i.Value.Street; s += " " + i.Value.PostalCode; string label = i.Label.Substring(4); label = label.Remove(label.Length - 4); address.Add(new AddressSave { Address = s, Label = label }); } info.Addresses = JsonConvert.SerializeObject(address.ToArray()); } else { info.Phones = "{}"; } contacts.Add(info); } return(contacts); }