/// <summary> /// Gets all contact from phone. /// </summary> /// <returns>The all contact from phone.</returns> public Dictionary <string, object> GetAllContactFromPhone() { NSError error; var keyToFetchs = new NSString[] { CNContactKey.Birthday, CNContactKey.Dates, CNContactKey.DepartmentName, CNContactKey.EmailAddresses, CNContactKey.FamilyName, CNContactKey.GivenName, CNContactKey.Identifier, CNContactKey.ImageData, CNContactKey.ImageDataAvailable, CNContactKey.InstantMessageAddresses, CNContactKey.JobTitle, CNContactKey.MiddleName, CNContactKey.NamePrefix, CNContactKey.NameSuffix, CNContactKey.Nickname, CNContactKey.NonGregorianBirthday, CNContactKey.Note, CNContactKey.OrganizationName, CNContactKey.PhoneNumbers, CNContactKey.PhoneticFamilyName, CNContactKey.PhoneticGivenName, CNContactKey.PhoneticMiddleName, CNContactKey.PhoneticOrganizationName, CNContactKey.PostalAddresses, CNContactKey.PreviousFamilyName, CNContactKey.Relations, CNContactKey.SocialProfiles, CNContactKey.ThumbnailImageData, CNContactKey.Type, CNContactKey.UrlAddresses }; CNContactStore store = new CNContactStore(); CNContactFetchRequest request = new CNContactFetchRequest(keyToFetchs); request.SortOrder = CNContactSortOrder.GivenName; totalContactListWithoutGrouping = new List <ContactItem>(); store.EnumerateContacts(request, out error, HandleCNContactStoreListContactsHandler); var dict = new Dictionary <string, object> { { "Group", totalContactList }, { "List", totalContactListWithoutGrouping } }; return(dict); }
static Task <IEnumerable <PhoneContact> > PlataformGetContacts(int pageSize) { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers, CNContactKey.EmailAddresses }; var contactList = new List <CNContact>(); using (var store = new CNContactStore()) { var request = new CNContactFetchRequest(keysToFetch); store.EnumerateContacts(request, out NSError error, new CNContactStoreListContactsHandler( (CNContact contact, ref bool stop) => contactList.Add(contact))); } var contacts = new List <PhoneContact>(); foreach (var item in contactList) { var numbers = item.PhoneNumbers.Select(x => x.Value.StringValue); var emails = item.EmailAddresses.Select(x => x.Value.ToString()); var bd = item.Birthday.Date.ToString(); var address = item.PostalAddresses.Select(x => x.Value.ToString()); contacts.Add(new PhoneContact( item.GivenName + item.FamilyName, numbers, emails, bd)); } return(Task.FromResult(contacts.AsEnumerable())); }
public SUGARContact[] GetAllContacts() { List <SUGARContact> resultList = new List <SUGARContact>(); CNContactStore store = new CNContactStore(); CNContactFetchRequest request = new CNContactFetchRequest( CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers); store.EnumerateContacts(request, out NSError err, (CNContact contact, ref Boolean stop) => { CNLabeledValue <CNPhoneNumber>[] phoneNumberValues = contact.PhoneNumbers; List <string> phoneNumbersAsStrings = new List <string>(); List <long> phoneNumbersAsLongs = new List <long>(); foreach (var labeledValue in phoneNumberValues) { string stringValue = labeledValue.Value.StringValue; phoneNumbersAsStrings.Add(stringValue); phoneNumbersAsLongs.Add(ParseNumberAsLong(stringValue)); } SUGARContact nextContact = new SUGARContact($"{contact.GivenName} {contact.FamilyName}", phoneNumbersAsStrings, phoneNumbersAsLongs); resultList.Add(nextContact); }); return(resultList.ToArray()); }
public void Ctor_Mixed() { var keys = new INativeObject [] { CNContactKey.GivenName, CNContactVCardSerialization.GetDescriptorFromRequiredKeys() }; using (var cfr = new CNContactFetchRequest(keys)) { Assert.That(2, Is.EqualTo(cfr.KeysToFetch.Count), "KeysToFetch"); } }
public void Ctor_NSString() { var keys = new [] { CNContactKey.GivenName }; using (var cfr = new CNContactFetchRequest(keys)) { Assert.That(keys [0].Description, Is.EqualTo(cfr.KeysToFetch.GetItem <NSString> (0).Description), "KeysToFetch"); } }
public void Ctor_ICNKeyDescriptorArray() { var keys = new [] { CNContactVCardSerialization.GetDescriptorFromRequiredKeys() }; using (var cfr = new CNContactFetchRequest(keys)) { Assert.That(keys [0].Description, Is.EqualTo(cfr.KeysToFetch.GetItem <NSObject> (0).Description), "KeysToFetch"); } }
/// <summary> /// Load contacts asynchronously, fact reading method of address book. /// </summary> /// <returns></returns> async Task <IList <Contact> > LoadContactsAsync() { IList <Contact> contacts = new List <Contact>(); var hasPermission = await RequestPermissionAsync(); if (hasPermission) { NSError error = null; var keysToFetch = new[] { CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.ImageDataAvailable, CNContactKey.ThumbnailImageData }; var request = new CNContactFetchRequest(keysToFetch: keysToFetch); request.SortOrder = CNContactSortOrder.GivenName; using (var store = new CNContactStore()) { var result = store.EnumerateContacts(request, out error, new CNContactStoreListContactsHandler((CNContact c, ref bool stop) => { string path = null; if (c.ImageDataAvailable) { path = path = Path.Combine(Path.GetTempPath(), $"{ThumbnailPrefix}-{Guid.NewGuid()}"); if (!File.Exists(path)) { var imageData = c.ThumbnailImageData; imageData?.Save(path, true); } } var contact = new Contact() { Name = string.IsNullOrEmpty(c.FamilyName) ? c.GivenName : $"{c.GivenName} {c.FamilyName}", Image = path, PhoneNumbers = c.PhoneNumbers?.Select(p => p?.Value?.StringValue).ToArray(), Emails = c.EmailAddresses?.Select(p => p?.Value?.ToString()).ToArray(), }; if (!string.IsNullOrWhiteSpace(contact.Name)) { OnContactLoaded?.Invoke(this, new ContactEventArgs(contact)); contacts.Add(contact); } stop = requestStop; })); } } return(contacts); }
public IEnumerable <PhoneContact> GetAllContacts() { 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 var contactList = new List <CNContact>(); //test using (var store = new CNContactStore()) { var request = new CNContactFetchRequest(keysTOFetch); store.EnumerateContacts(request, out error, new CNContactStoreListContactsHandler((CNContact contact, ref bool stop) => contactList.Add(contact))); } var contacts = new List <PhoneContact>(); foreach (var item in contactList) { var numbers = item.PhoneNumbers; if (numbers != null) { if (item.GivenName == "SPAM") { foreach (var item2 in numbers) { var givenName = item2.Label.Split('-'); contacts.Add(new PhoneContact { FirstName = givenName.Length > 1 ? givenName[1] : "", LastName = item.FamilyName, PhoneNumber = item2.Value.StringValue }); } } else { foreach (var item2 in numbers) { contacts.Add(new PhoneContact { FirstName = item.GivenName, LastName = item.FamilyName, PhoneNumber = item2.Value.StringValue }); } } } } return(contacts); }
public void GetContacts(Action <string, Contact[]> callback) { if (isFetchingAllContacts) { callback(FetchingContactsThreadRunningMessage, null); return; } isFetchingAllContacts = true; new Thread(() => { try { List <Contact> result = new List <Contact>(); var contactStore = InteropObjectFactory <CNContactStore> .Create( () => new CNContactStore(), c => c.ToPointer()); CNContactFetchRequest request = CNContactFetchRequest.InitWithKeysToFetch(GetPropertyKeys()); request.SortOrder = CNContactSortOrder.CNContactSortOrderUserDefault; request.UnifyResults = true; NSError error = null; contactStore.EnumerateContactsWithFetchRequest(request, out error, (CNContact cnContact, out bool stop) => { stop = false; result.Add(cnContact.ToContact()); }); if (error != null && !string.IsNullOrEmpty(error.ToString())) { Debug.LogError(error.ToString()); } RuntimeHelper.RunOnMainThread(() => callback(null, result.ToArray())); } catch (Exception e) { Debug.LogError(e.Message); RuntimeHelper.RunOnMainThread(() => callback(e.Message, null)); } finally { isFetchingAllContacts = false; } }).Start(); }
public async Task <IList <Contact> > LoadContactsAsync() { try { IList <Contact> contacts = new List <Contact>(); var hasPermission = await RequestPermissionAsync(); if (hasPermission) { NSError error = null; var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses }; var request = new CNContactFetchRequest(keysToFetch: keysToFetch); request.SortOrder = CNContactSortOrder.GivenName; using (var store = new CNContactStore()) { var result = store.EnumerateContacts(request, out error, new CNContactStoreListContactsHandler((CNContact c, ref bool stop) => { var contact = new Contact() { Name = string.IsNullOrEmpty(c.FamilyName) ? c.GivenName : $"{c.GivenName} {c.FamilyName}", Emails = c.EmailAddresses?.Select(p => p?.Value?.ToString()).ToArray(), }; if (!string.IsNullOrWhiteSpace(contact.Name) && (contact.Emails.Count() > 0)) { contacts.Add(contact); } stop = requestStop; })); } } return(contacts); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
private Dictionary <string, List <string> > createSection() { #region declaration NSError error; contactStore = new CNContactStore(); if (tableSectionData == null) { tableSectionData = new Dictionary <string, List <string> > (); } else { tableSectionData.Clear(); } if (nameData == null) { nameData = new List <string> (); } bool isLetter = true; #endregion //var predicate = CNContact.GetPredicateForContactsInContainer (containerID); #region fetchContacts var keys = new NSString[] { CNContactKey.FamilyName, CNContactKey.GivenName, CNContactKey.PhoneticFamilyName, CNContactKey.PhoneticGivenName, CNContactKey.PhoneNumbers }; CNContactFetchRequest feq = new CNContactFetchRequest(keys); feq.MutableObjects = false; feq.UnifyResults = true; contactStore.EnumerateContacts(feq, out error, (CNContact contact, bool stop) => { if (contact.PhoneNumbers.Length > 0 && (contact.GivenName != "" || contact.FamilyName != "")) { string name; if (contact.GivenName == "") { name = contact.FamilyName; } else if (contact.FamilyName == "") { name = contact.GivenName; } else { name = contact.GivenName + " " + contact.FamilyName; } string key = ""; foreach (var v in contact.GivenName.ToCharArray()) { if (!IsbasicLetter(v)) { isLetter = false; } } foreach (var v in contact.FamilyName.ToCharArray()) { if (!IsbasicLetter(v)) { isLetter = false; } } if (!isLetter) { if (contact.GivenName != "") { key = GetPinyin(contact.GivenName).ToCharArray()[0].ToString(); } else { key = GetPinyin(contact.FamilyName).ToCharArray()[0].ToString(); } } key = key.ToUpper(); if (tableSectionData.ContainsKey(key)) { tableSectionData [key].Add(name); } else { List <string> list = new List <string> () { name }; tableSectionData.Add(key, list); } nameData.Add(name); } }); #endregion /*try{ * contacts = contactStore.GetUnifiedContacts (predicate, keys, out error); * } * catch (Exception ex){ * * Console.WriteLine (ex.Message); * }*/ //Create Keys keysArray = new string[tableSectionData.Keys.Count]; tableSectionData.Keys.CopyTo(keysArray, 0); return(tableSectionData); }