Example #1
0
        void ShowPersonViewController()
        {
            var predicate = CNContact.GetPredicateForContacts("Appleseed");

            ICNKeyDescriptor[] toFetch = { CNContactViewController.DescriptorForRequiredKeys };

            CNContactStore store = new CNContactStore();

            NSError fetchError;

            CNContact[] contacts = store.GetUnifiedContacts(predicate, toFetch, out fetchError);

            if (contacts != null && contacts.Length > 0)
            {
                CNContact contact = contacts[0];
                var       peopleViewController = CNContactViewController.FromContact(contact);
                peopleViewController.AllowsEditing = true;

                NavigationController.PushViewController(peopleViewController, true);
            }
            else
            {
                var alert = UIAlertController.Create("Error", "Could not find Appleseed in the Contacts application", UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Default, null));
                PresentViewController(alert, true, null);
            }
        }
Example #2
0
        public void GetUnifiedContacts()
        {
            string identifier = null;

            TestRuntime.CheckContactsPermission();

            var     fetchKeys = new [] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName };
            NSError error;

            using (var predicate = CNContact.GetPredicateForContacts("Appleseed"))
                using (var store = new CNContactStore()) {
                    var contacts = store.GetUnifiedContacts(predicate, fetchKeys, out error);
                    // we can't be sure what's on devices, so check there's no error is the only thing we do
                    Assert.Null(error, "error");
                    // but it's in the default simulator build (but not the watchOS simulator)
#if !__WATCHOS__ && !MONOMAC
                    if (Runtime.Arch == Arch.SIMULATOR)
                    {
                        Assert.That(contacts.Length, Is.EqualTo(1), "Length");
                        identifier = contacts [0].Identifier;
                    }
#endif
                }

            // if we can't find the previous contact then we don't have an identifier for the GetUnifiedContact API
            // and we can't hardcode one as each simulator instance has a different identifier...
            if (identifier == null)
            {
                return;
            }

            using (var store = new CNContactStore()) {
                var contact = store.GetUnifiedContact(identifier, fetchKeys, out error);
                // it's in the default simulator build
#if !MONOMAC
                if (Runtime.Arch == Arch.SIMULATOR)
                {
                    Assert.Null(error, "error");
                    Assert.NotNull(contact, "contact");
                    Assert.False(contact.AreKeysAvailable(CNContactOptions.OrganizationName | CNContactOptions.Note), "AreKeysAvailable-1");
                    Assert.True(contact.AreKeysAvailable(CNContactOptions.None), "AreKeysAvailable-2");
                    Assert.True(contact.AreKeysAvailable(fetchKeys), "AreKeysAvailable-3");
                }
                else
                {
#endif
                // and it's a safe bet that id does not exists on any device
                Assert.NotNull(error, "error");                          // Updated Record Does Not Exist

#if !MONOMAC
            }
#endif
            }
        }
Example #3
0
        public void GetUnifiedContacts()
        {
            string identifier = null;

            TestRuntime.CheckContactsPermission();

            var     fetchKeys = new [] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName };
            NSError error;

            using (var predicate = CNContact.GetPredicateForContacts("Appleseed"))
                using (var store = new CNContactStore()) {
                    var contacts = store.GetUnifiedContacts(predicate, fetchKeys, out error);
                    // we can't be sure what's on devices, so check there's no error is the only thing we do
                    // but it's in the default simulator build (but not the watchOS simulator)
#if !MONOMAC && !__WATCHOS__ && !__MACCATALYST__
                    if ((error == null) && (Runtime.Arch == Arch.SIMULATOR))
                    {
                        Assert.That(contacts.Length, Is.EqualTo(1), "Length");
                        identifier = contacts [0].Identifier;
                    }
#endif
                }

            // if we can't find the previous contact then we don't have an identifier for the GetUnifiedContact API
            // and we can't hardcode one as each simulator instance has a different identifier...
            if (identifier == null)
            {
                return;
            }

            using (var store = new CNContactStore()) {
                var contact = store.GetUnifiedContact(identifier, fetchKeys, out error);
                // it's in the default simulator build
                if (TestRuntime.IsSimulatorOrDesktop)
                {
                    // it fails on some bots (watchOS 4.2 on jenkins) so we cannot assume it always work
                    if (error != null)
                    {
                        return;
                    }
                    Assert.NotNull(contact, "contact");
                    Assert.False(contact.AreKeysAvailable(CNContactOptions.OrganizationName | CNContactOptions.Note), "AreKeysAvailable-1");
                    Assert.True(contact.AreKeysAvailable(CNContactOptions.None), "AreKeysAvailable-2");
                    Assert.True(contact.AreKeysAvailable(fetchKeys), "AreKeysAvailable-3");
                }
            }
        }
        public Task <ContactInfo> GetContactInfoForId(string id)
        {
            var predicate = CNContact.GetPredicateForContacts(new string[] { id });
            var fetchKeys = new NSString[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.MiddleName };

            var         store        = new CNContactStore();
            var         contacts     = store.GetUnifiedContacts(predicate, fetchKeys, out NSError error);
            ContactInfo foundContact = null;

            if (contacts.Any())
            {
                var contact = contacts[0];
                foundContact = new ContactInfo
                {
                    Id             = id,
                    FamilyName     = contact.FamilyName,
                    AdditionalName = contact.MiddleName,
                    GivenName      = contact.GivenName
                };
            }
            return(Task.FromResult(foundContact));
        }
Example #5
0
        public static async void CreateContact(object sender, EventArgs e)
        {
            var     store = new CNContactStore();
            NSError error;

            if (CNContactStore.GetAuthorizationStatus(CNEntityType.Contacts) == CNAuthorizationStatus.Authorized)
            {
                CNContact currentUser = null;
                var       predicate   = CNContact.GetPredicateForContacts("Appleseed");
                var       fetchKeys   = new NSString[] { CNContactKey.GivenName, CNContactKey.FamilyName };
                NSError   cError;
                var       contacts = store.GetUnifiedContacts(predicate, fetchKeys, out cError);
                for (int i = 0; i < contacts.Count(); i++)
                {
                    if (contacts[i].GivenName == App.contactuser.FirstName && contacts[i].FamilyName == App.contactuser.LastName)
                    {
                        currentUser = contacts[i];
                    }
                }


                // Found?
                if (currentUser != null)
                {
                    bool k = await App.Current.MainPage.DisplayAlert("Exists", "Requested contact already exists...", "Edit", "OK");

                    if (k)
                    {
                        personViewController = CNContactViewController.FromContact(currentUser);
                        var done = new UIBarButtonItem(UIBarButtonSystemItem.Done);
                        done.Clicked += (s, ea) =>
                        {
                            personViewController.DismissViewController(false, null);
                        };
                        personViewController.NavigationItem.LeftBarButtonItem = done;

                        navController.PushViewController(personViewController, true);
                        UIApplication.SharedApplication.KeyWindow.RootViewController.ShowViewController(navController, null);
                    }
                }
                else
                {
                    var contact = new CNMutableContact();

                    contact.GivenName  = App.contactuser.FirstName;
                    contact.FamilyName = App.contactuser.LastName;

                    // Add email addresses
                    var email = new CNLabeledValue <NSString>(CNLabelKey.Home, new NSString(App.contactuser.email));
                    contact.EmailAddresses = new CNLabeledValue <NSString>[] { email };

                    // Add phone numbers
                    var cellPhone = new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber(App.contactuser.phoneNumber));
                    contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[] { cellPhone };
                    // Save new contact
                    var _store      = new CNContactStore();
                    var saveRequest = new CNSaveRequest();
                    saveRequest.AddContact(contact, _store.DefaultContainerIdentifier);

                    NSError cerror;
                    if (store.ExecuteSaveRequest(saveRequest, out cerror))
                    {
                        personViewController = CNContactViewController.FromContact(contact);
                        var done = new UIBarButtonItem(UIBarButtonSystemItem.Done);
                        done.Clicked += (s, ea) =>
                        {
                            personViewController.DismissViewController(false, null);
                        };
                        personViewController.NavigationItem.LeftBarButtonItem = done;
                        navController.PushViewController(personViewController, true);
                        UIApplication.SharedApplication.KeyWindow.RootViewController.ShowViewController(navController, null);
                    }
                    else
                    {
                        Console.WriteLine("Save error: {0}", cerror);
                    }
                }
            }
            else
            {
                store.RequestAccess(CNEntityType.Contacts, RequestAccepted);
            }
        }
Example #6
0
        private bool DeletarContato()
        {
            var predicate = CNContact.GetPredicateForContacts("Appleseed");

            return(false);
        }