public string sendMsg() { var keysToFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers }; NSError error; bool check = false; 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); foreach (var item in containerResults) { var num = item.PhoneNumbers; foreach (var cn in num) { string mob = cn.Value.StringValue; if (mob == ProfileContentView.phone) { check = true; } } } } } catch { } } } if (check == false) { var store = new CNContactStore(); var contact = new CNMutableContact(); var cellPhone = new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.Mobile, new CNPhoneNumber(ProfileContentView.phone)); var phoneNumber = new[] { cellPhone }; contact.PhoneNumbers = phoneNumber; contact.GivenName = ProfileContentView.name; var saveRequest = new CNSaveRequest(); saveRequest.AddContact(contact, store.DefaultContainerIdentifier); } return("1"); }
public void SaveContact(ContactModel contact) { var store = new CNContactStore(); var iosContact = new CNMutableContact(); List <CNLabeledValue <CNPhoneNumber> > phoneNumbers = new List <CNLabeledValue <CNPhoneNumber> >(); List <CNLabeledValue <NSString> > emails = new List <CNLabeledValue <NSString> >(); List <CNLabeledValue <NSString> > websites = new List <CNLabeledValue <NSString> >(); foreach (PhoneField phoneField in contact.PhoneNumbers) { phoneNumbers.Add(new CNLabeledValue <CNPhoneNumber>(PhoneNumberTypeToKey(phoneField.Type), new CNPhoneNumber(phoneField.Number))); } foreach (EmailField emailField in contact.Emails) { emails.Add(new CNLabeledValue <NSString>(EmailTypeToKey(emailField.Type), new NSString(emailField.Email))); } foreach (ContactField websiteField in contact.Websites) { websites.Add(new CNLabeledValue <NSString>(CNLabelKey.UrlAddressHomePage, new NSString(websiteField.Text))); } iosContact.PhoneNumbers = phoneNumbers.ToArray(); iosContact.FamilyName = contact.LastName; iosContact.GivenName = contact.FirstName; iosContact.OrganizationName = !string.IsNullOrEmpty(contact.Company.Text) ? contact.Company.Text : string.Empty; iosContact.EmailAddresses = emails.ToArray(); iosContact.UrlAddresses = websites.ToArray(); if (!string.IsNullOrEmpty(contact.ProfileImage)) { iosContact.ImageData = NSData.FromFile(contact.ProfileImage); } var saveRequest = new CNSaveRequest(); saveRequest.AddContact(iosContact, store.DefaultContainerIdentifier); if (store.ExecuteSaveRequest(saveRequest, out NSError error)) { Console.WriteLine("New contact saved"); } else { Console.WriteLine("Save error: {0}", error); } }
public int WriteContact(Contact contact) { var contactToAdd = new CNMutableContact(); contactToAdd.GivenName = contact.FirstName; var phoneNumber = new CNLabeledValue <CNPhoneNumber>(label: CNLabelKey.Home, value: new CNPhoneNumber(stringValue: contact.Number)); contactToAdd.PhoneNumbers = new [] { phoneNumber }; var store = new CNContactStore(); var saveRequest = new CNSaveRequest(); saveRequest.AddContact(contactToAdd, null); var error = new NSError(); try { store.ExecuteSaveRequest(saveRequest, out error); return(0); } catch { return(1); } }
public bool AddContacts(QContact qc) { Console.WriteLine("export contacts ios"); var contact = new CNMutableContact(); // Set standard properties contact.GivenName = PreventNull(qc.FirstName); contact.FamilyName = PreventNull(qc.LastName); // Add email addresses var homeEmail = new CNLabeledValue <NSString>(CNLabelKey.Home, new NSString(PreventNull(qc.Email))); var email = new[] { homeEmail }; contact.EmailAddresses = email; // Add work address var workAddress = new CNMutablePostalAddress() { Street = PreventNull(qc.Addr) }; contact.PostalAddresses = new[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Work, workAddress) }; // ADD BIRTHday string[] birth = PreventNull(qc.Birthday.ToString("MM/dd/yyyy")).Split('/'); if (birth.Length == 3) { var birthday = new NSDateComponents() { Month = int.Parse(birth[0]), Day = int.Parse(birth[1]), Year = int.Parse(birth[2]) }; contact.Birthday = birthday; } // add company contact.OrganizationName = PreventNull(qc.Company); // add others-> fb StringBuilder sb = new StringBuilder(); sb.Append("Facebook:").Append(PreventNull(qc.Facebook)).Append(", Instagram:").Append(PreventNull(qc.Instagram)) .Append(", Linkedin:").Append(PreventNull(qc.LinkedIn)).Append(", Skype:").Append(PreventNull(qc.Skype)) .Append(", Twitter:").Append(PreventNull(qc.Twitter)); contact.Note = sb.ToString(); // add url var url = new CNLabeledValue <NSString>(CNLabelKey.UrlAddressHomePage, new NSString(PreventNull(qc.URL))); var myUrl = new[] { url }; contact.UrlAddresses = myUrl; //mobile var cellPhone = new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.Mobile, new CNPhoneNumber(PreventNull(qc.Mobile))); //var phoneNumber = new[] { cellPhone }; //contact.PhoneNumbers = phoneNumber; //home phone var homePhone = new CNLabeledValue <CNPhoneNumber>("HOME", new CNPhoneNumber(PreventNull(qc.HomePhone))); //work phone var workPhone = new CNLabeledValue <CNPhoneNumber>("WORK", new CNPhoneNumber(PreventNull(qc.WorkPhone))); //homefax var homeFax = new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.HomeFax, new CNPhoneNumber(PreventNull(qc.HomeFax))); //workFax var workFax = new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.WorkFax, new CNPhoneNumber(PreventNull(qc.WorkFax))); var phoneNumber = new[] { cellPhone, homePhone, workPhone, homeFax, workFax }; contact.PhoneNumbers = phoneNumber; // Save new contact var store = new CNContactStore(); var saveRequest = new CNSaveRequest(); saveRequest.AddContact(contact, store.DefaultContainerIdentifier); NSError error; if (store.ExecuteSaveRequest(saveRequest, out error)) { Console.WriteLine("New contact saved"); return(true); } else { Console.WriteLine("Save error: {0}", error); return(false); } }
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); } }