Пример #1
0
 void ShowUnknownPersonViewController()
 {
     using (var unknowContact = new CNMutableContact()) {
         try
         {
             var unknownContactVC = CNContactViewController.FromUnknownContact(unknowContact);
             unknownContactVC.ContactStore  = new CNContactStore();
             unknownContactVC.AllowsActions = true;
             unknownContactVC.AlternateName = "John Appleseed";
             unknownContactVC.Title         = "John Appleseed";
             unknownContactVC.Message       = "Company, Inc";
             NavigationController.PushViewController(unknownContactVC, true);
         }
         catch (Exception)
         {
             var alert = UIAlertController.Create("Error", "Could not create unknown user.", UIAlertControllerStyle.Alert);
             alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Default, null));
             PresentViewController(alert, true, null);
         }
     }
 }
Пример #2
0
        private void PerformContactAction(UITableViewCell selectedCell)
        {
            if (selectedCell == createNewContactCell)
            {
                var contactViewController = CNContactViewController.FromNewContact(null);
                contactViewController.Delegate = this;
                base.NavigationController.PushViewController(contactViewController, true);
            }

            else if (selectedCell == createNewContactExistingDataCell)
            {
                var contact = new CNMutableContact
                {
                    FamilyName = Name.Family,
                    GivenName  = Name.Given,
                };

                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[]
                {
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.IPhone, new CNPhoneNumber(PhoneNumber.IPhone)),
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.Mobile, new CNPhoneNumber(PhoneNumber.Mobile))
                };

                var homeAddress = new CNMutablePostalAddress
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode
                };
                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                //Erstelle einen Kontakt-View-Controller mit unserem Kontakt
                var contactViewController = CNContactViewController.FromNewContact(contact);
                contactViewController.Delegate = this;
                base.NavigationController.PushViewController(contactViewController, true);
            }

            else if (selectedCell == editContactCell)
            {
                var contact = new CNMutableContact();

                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[] { new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber(PhoneNumber.Mobile)) };
                var homeAddress = new CNMutablePostalAddress()
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode
                };
                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                var contactViewController = CNContactViewController.FromUnknownContact(contact);
                contactViewController.AllowsEditing = true;
                contactViewController.ContactStore  = new CNContactStore();
                contactViewController.Delegate      = this;

                base.NavigationController.PushViewController(contactViewController, true);
            }

            else if (selectedCell == displayEditCell)
            {
                var name = $"{Name.Given} {Name.Family}";
                FetchContact(name, (contacts) =>
                {
                    if (contacts.Any())
                    {
                        var contactViewController           = CNContactViewController.FromContact(contacts[0]);
                        contactViewController.AllowsEditing = true;
                        contactViewController.AllowsActions = true;
                        contactViewController.Delegate      = this;

                        var highlightedPropertyIdentifiers = contacts[0].PhoneNumbers.FirstOrDefault()?.Identifier;
                        if (!string.IsNullOrEmpty(highlightedPropertyIdentifiers))
                        {
                            contactViewController.HighlightProperty(new NSString("phoneNumbers"), highlightedPropertyIdentifiers);
                        }
                        else
                        {
                            this.ShowAlert($"Could not find {name} in Contacts.");
                        }
                    }
                });
            }
        }
Пример #3
0
        private void PerformContactAction(UITableViewCell selectedCell)
        {
            if (selectedCell == this.createNewContactCell)
            {
                // Create an empty contact view controller.
                var contactViewController = CNContactViewController.FromNewContact(null);
                // Set its delegate.
                contactViewController.Delegate = this;
                // Push it using the navigation controller.
                base.NavigationController.PushViewController(contactViewController, true);
            }
            // Called when users tap "Create New Contact With Existing Data" in the UI.
            // Create and launch a contacts view controller with pre - filled fields.
            else if (selectedCell == this.createNewContactExistingData)
            {
                var contact = new CNMutableContact
                {
                    // Given and family names.
                    FamilyName = Name.Family,
                    GivenName  = Name.Given,
                };

                // Phone numbers.
                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[]
                {
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.IPhone,
                                                       new CNPhoneNumber(PhoneNumber.IPhone)),
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.Mobile,
                                                       new CNPhoneNumber(PhoneNumber.Mobile))
                };

                // Postal address.
                var homeAddress = new CNMutablePostalAddress
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode,
                };

                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                // Create a contact view controller with the above contact.
                var contactViewController = CNContactViewController.FromNewContact(contact);
                // Set its delegate.
                contactViewController.Delegate = this;
                // Push it using the navigation controller.
                base.NavigationController.PushViewController(contactViewController, true);
            }
            // Called when users tap "Edit Unknown Contact" in the UI.
            // The view controller displays some contact information that you can either add to an existing contact or use them to create a new contact.
            else if (selectedCell == this.editContactCell)
            {
                var contact = new CNMutableContact();

                // Phone number.
                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[] { new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber(PhoneNumber.Mobile)) };

                // Postal address.
                var homeAddress = new CNMutablePostalAddress()
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode,
                };

                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                // Create a view controller that allows editing.
                var contactViewController = CNContactViewController.FromUnknownContact(contact);
                contactViewController.AllowsEditing = true;
                contactViewController.ContactStore  = new CNContactStore();
                contactViewController.Delegate      = this;

                // Push the unknown contact in the view controler.
                base.NavigationController.PushViewController(contactViewController, true);
            }
            // Called when users tap "Display and Edit Contact" in the UI.
            // Searches for the contact specified whose last name and first name are respectively specified by contact.family and contact.given
            else if (selectedCell == this.displayEditCell)
            {
                var name = $"{Name.Given} {Name.Family}";
                this.FetchContact(name, (contacts) =>
                {
                    if (contacts.Any())
                    {
                        var contactViewController           = CNContactViewController.FromContact(contacts[0]);
                        contactViewController.AllowsEditing = true;
                        contactViewController.AllowsActions = true;
                        contactViewController.Delegate      = this;

                        /*
                         *  Set the view controller's highlightProperty if
                         *  highlightedPropertyIdentifier exists. Thus, ensuring
                         *  that the contact's phone number specified by
                         *  highlightedPropertyIdentifier will be highlighted in the
                         *  UI.
                         */

                        var highlightedPropertyIdentifiers = contacts[0].PhoneNumbers.FirstOrDefault()?.Identifier;
                        if (!string.IsNullOrEmpty(highlightedPropertyIdentifiers))
                        {
                            contactViewController.HighlightProperty(new NSString("phoneNumbers"), highlightedPropertyIdentifiers);
                        }

                        // Show the view controller.
                        base.NavigationController.PushViewController(contactViewController, true);
                    }
                    else
                    {
                        this.ShowAlert($"Could not find {name} in Contacts.");
                    }
                });
            }
        }