Пример #1
0
        public void GetDescriptorForRequiredKeys_PhoneticFullName()
        {
            var keys = CNContactFormatter.GetDescriptorForRequiredKeys(CNContactFormatterStyle.PhoneticFullName);

            // while most input for ICNKeyDescriptor are done with NSString
            // the output is opaque and an internal type
            // note: this is not very robust - but I want to know if this changes during the next betas
            Assert.True(keys.Description.StartsWith("<CNAggregateKeyDescriptor:", StringComparison.Ordinal), "type");
            Assert.True(keys.Description.Contains("kind=Formatter style: 1"), "kind");
        }
Пример #2
0
        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);
                }
            }
        }
Пример #3
0
        public static string GetFormattedName(this CNContact contact)
        {
            var name = contact != null?CNContactFormatter.GetStringFrom(contact, CNContactFormatterStyle.FullName)?.Trim() : null;

            return(!string.IsNullOrEmpty(name) ? name : "No Name");
        }