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); } }
public void DidComplete(CNContactViewController viewController, CNContact contact) { base.NavigationController.PopViewController(true); if (contact != null) { base.NavigationController.ShowAlert($"{contact.GetFormattedName()} was successsfully added."); } }
void ShowNewPersonViewController() { var newContact = new CNContact(); var newContactViewController = CNContactViewController.FromNewContact(newContact); newContactViewController.Delegate = this; var navController = new UINavigationController(newContactViewController); PresentViewController(navController, true, null); }
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); } } }
public void DidComplete(CNContactViewController viewController, CNContact contact) { //NavigationController.PopViewController(true); DismissViewController(true, null); }
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); } }
public bool ShouldPerformDefaultAction(CNContactViewController viewController, CNContactProperty property) { return(false); }
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."); } } }); } }
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."); } }); } }