public Task <Contact[]> GetAllAsync() { var taskCompletionSource = new TaskCompletionSource <Contact[]>(); NSError err; var ab = ABAddressBook.Create(out err); if (err != null) { // process error return(Task.FromResult(new Contact[0])); } // if the app was not authorized then we need to ask permission if (ABAddressBook.GetAuthorizationStatus() != ABAuthorizationStatus.Authorized) { ab.RequestAccess(delegate(bool granted, NSError error) { if (error != null) { taskCompletionSource.TrySetResult(new Contact[0]); } else if (granted) { Task.Run(() => taskCompletionSource.TrySetResult(GetContacts(ab))); } }); } else { Task.Run(() => taskCompletionSource.TrySetResult(GetContacts(ab))); } return(taskCompletionSource.Task); }
/// <summary> /// Gets ABRecords on contacts /// </summary> public static ABRecord[] GetAllContactRecords() { NSError error = new NSError(); var book = ABAddressBook.Create(null, error); var allPeople = book.CopyArrayOfAllPeople(); return(allPeople.Cast <ABRecord>().ToArray()); }
/// <summary> /// Construtor de nova instância da classe <see cref="GerenciadorDeAgendaIOS"/>. /// </summary> public GerenciadorDeAgendaIOS() { NSError error; this.agenda = ABAddressBook.Create(out error); if (error != null) { Console.WriteLine("Erro ao iniciar a agenda Local"); } }
public override void ViewDidLoad() { base.ViewDidLoad(); Title = "QuickContacts"; menuArray = new NSMutableArray(0); NSError error; addressBook = ABAddressBook.Create(out error); CheckAddressBookAccess(); }
public void RequestAddressBookAccess() { NSError error; addressBook = ABAddressBook.Create(out error); if (addressBook != null) { addressBook.RequestAccess(delegate(bool granted, NSError accessError) { InvokeOnMainThread(() => accessStatus.Text = "Access " + (granted ? "allowed" : "denied")); }); } }
public void RequestAddressBookAccess() { NSError error; addressBook = ABAddressBook.Create(out error); if (addressBook != null) { addressBook.RequestAccess(delegate(bool granted, NSError accessError) { ShowAlert(DataClass.Contacts, granted ? "granted" : "denied"); }); } }
void ShowActualContactPermissionAlert() { NSError error; var addressBook = ABAddressBook.Create(out error); addressBook.RequestAccess(delegate { BeginInvokeOnMainThread(delegate { FireContactPermissionCompletionHandler(); }); }); }
public List <Person> GetPeople() { NSError error; List <Person> result = new List <Person> (); Xamarin.Contacts.AddressBook book = new Xamarin.Contacts.AddressBook(); ABAddressBook nativeBook = ABAddressBook.Create(out error); nativeBook.ExternalChange += BookChanged; foreach (Xamarin.Contacts.Contact c in book) { if (c.Phones.Count() > 0) { Person p = new Person(Convert.ToInt32(c.Id), c.FirstName, c.MiddleName, c.LastName); p.ModificationDate = nativeBook.GetPerson(p.Id).ModificationDate; p.NickName = c.Nickname; if (c.Organizations.Count() > 0) { p.Organization = c.Organizations.ElementAt(0).Name; } p.Notes = String.Concat(c.Notes.Select(n => n.Contents)); StringBuilder sb = new StringBuilder(); foreach (Phone phone in c.Phones) { PhoneNumber pnb = new PhoneNumber() { Type = (PhoneNumberType)((int)phone.Type), Number = phone.Number }; p.Phones.Add(pnb); sb.AppendFormat("{0},{1};", (int)pnb.Type, pnb.Number); } p.DetailData = p.Details; p.PhoneData = sb.ToString(); result.Add(p); } } return(result); }
protected override void RequestAccess() { NSError error; ABAddressBook addressBook = ABAddressBook.Create(out error); if (addressBook == null) { return; } addressBook.RequestAccess((granted, accessError) => { string text = string.Format("Access {0}", granted ? "allowed" : "denied"); InvokeOnMainThread(() => AccessStatus.Text = text); }); }
private void addNewContact() { ABAddressBook addressBook = ABAddressBook.Create(); ABRecord person = ABPerson.Create(); person.SetValue(ABPerson.kABPersonFirstNameProperty, CFType.FromObject("vitapoly"), null); person.SetValue(ABPerson.kABPersonBirthdayProperty, CFType.FromObject(new DateTime(2000, 1, 1)), null); addressBook.AddRecord(person, null); addressBook.Save(null); Log("Added vitapoly to address book."); }
private ABAddressBook GetAddressBook() { if (this.addressBook == null) { NSError error; this.addressBook = ABAddressBook.Create(out error); if (addressBook == null) { throw new InvalidOperationException("Failed to create an AddressBook with error (" + error + ")."); } } return(this.addressBook); }
/// <summary> /// Gets all contact names in a form of string[] /// </summary> public static string[] GetAllContactNames() { NSError error = new NSError(); var book = ABAddressBook.Create(null, error); var allPeople = book.CopyArrayOfAllPeople(); string[] peopleNames = new string[allPeople.Length]; for (int i = 0; i < allPeople.Length; i++) { var cftype = allPeople[i] as CFType; var record = cftype.Cast <ABRecord>(); peopleNames[i] = record.CopyCompositeName(); } return(peopleNames); }
public void ReloadAddressBook() { #if DEBUG log("IPhoneUIApplicationDelegate creating address book instance"); #endif if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { NSError nsError = new NSError(); addressBook = ABAddressBook.Create(out nsError); #if DEBUG log("IPhoneUIApplicationDelegate creating address book result: " + ((nsError != null)?nsError.Description:"no error")); #endif } else { addressBook = new ABAddressBook(); } }
void AddContact(UIAlertAction action) { try { #if !DEBUG Xamarin.Insights.Track("Click", new Dictionary <string, string> { { "item", "add contact" }, { "name", viewModel.Place.Name }, }); #endif NSError error; addressBook = ABAddressBook.Create(out error); CheckAddressBookAccess(); } catch (Exception ex) { #if !DEBUG ex.Data ["call"] = "add contact"; Xamarin.Insights.Report(ex); #endif } }
public void UpdateAddressLine() { TestRuntime.CheckAddressBookPermission(); if (!TestRuntime.CheckSystemAndSDKVersion(6, 0)) { Assert.Inconclusive("System.EntryPointNotFoundException : ABAddressBookCreateWithOptions before 6.0"); } NSError err; var ab = ABAddressBook.Create(out err); Assert.IsNotNull(ab, "#1"); var people = ab.GetPeople(); if (people.Length < 1) { // TODO: return; } var p = people[0]; var all = p.GetAllAddresses(); var mutable = all.ToMutableMultiValue(); if (mutable.Count < 1) { // TODO: return; } var multi = mutable [0]; var addr = multi.Value; addr.Zip = "78972"; multi.Value = addr; p.SetAddresses(mutable); Assert.IsTrue(ab.HasUnsavedChanges); ab.Save(); }
public IPhoneUIApplicationDelegate(IntPtr ptr) : base(ptr) { #if DEBUG log("IPhoneUIApplicationDelegate constructor IntPtr"); #endif IPhoneServiceLocator.CurrentDelegate = this; eventStore = new EKEventStore( ); if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { NSError nsError = new NSError(); addressBook = ABAddressBook.Create(out nsError); #if DEBUG log("IPhoneUIApplicationDelegate creating address book result: " + ((nsError != null)?nsError.Description:"no error")); #endif } else { addressBook = new ABAddressBook(); } }
public void UpdateAddressLine() { TestRuntime.CheckAddressBookPermission(); NSError err; var ab = ABAddressBook.Create(out err); Assert.IsNotNull(ab, "#1"); var people = ab.GetPeople(); if (people.Length < 1) { // TODO: return; } var p = people[0]; var all = p.GetAllAddresses(); var mutable = all.ToMutableMultiValue(); if (mutable.Count < 1) { // TODO: return; } var multi = mutable [0]; var addr = multi.Value; addr.Zip = "78972"; multi.Value = addr; p.SetAddresses(mutable); Assert.IsTrue(ab.HasUnsavedChanges); ab.Save(); }
public AddressBookPrivacyManager() { NSError error; addressBook = ABAddressBook.Create(out error); }
public List <Contacto> GetLista() { Contacto c; lsc.Clear(); try { Version ver = new Version(UIDevice.CurrentDevice.SystemVersion); if (ver.Major <= 8) { NSError err; var iPhoneAddressBook = ABAddressBook.Create(out err); var authStatus = ABAddressBook.GetAuthorizationStatus(); if (authStatus != ABAuthorizationStatus.Authorized) { iPhoneAddressBook.RequestAccess(delegate(bool granted, NSError error) { if (granted) { ABPerson[] myContacts = iPhoneAddressBook.GetPeople(); string phone = string.Empty; foreach (ABPerson contact in myContacts) { ABMultiValue <string> phs = contact.GetPhones(); if (phs.Count() > 0) { foreach (var stel in phs) { c = new Contacto() { Name = contact.FirstName + " " + contact.LastName, Number = stel.Value, //phs.First().Value, Photo = (contact.HasImage ? GetBitmap(contact.Image) : null), }; lsc.Add(c); } } } } }); } else { ABPerson[] myContacts = iPhoneAddressBook.GetPeople(); string phone = string.Empty; foreach (ABPerson contact in myContacts) { ABMultiValue <string> phs = contact.GetPhones(); if (phs.Count() > 0) { foreach (var stel in phs) { c = new Contacto() { Name = contact.FirstName + " " + contact.LastName, Number = stel.Value, //phs.First().Value, Photo = (contact.HasImage ? GetBitmap(contact.Image) : null), }; lsc.Add(c); } } } } } if (ver.Major >= 9) { //ios 9 var store = new CNContactStore(); NSError error; var fetchKeys = new NSString[] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PhoneNumbers, CNContactKey.ThumbnailImageData }; string contid = store.DefaultContainerIdentifier; var predicate = CNContact.GetPredicateForContactsInContainer(contid); var contacts = store.GetUnifiedContacts(predicate, fetchKeys, out error); foreach (CNContact cc in contacts) { for (int i = 0; i < cc.PhoneNumbers.Count(); i++) { Contacto ct = new Contacto() { Id = cc.Identifier, Name = (!string.IsNullOrEmpty(cc.GivenName) ? cc.GivenName : "") + (!string.IsNullOrEmpty(cc.FamilyName) ? " " + cc.FamilyName : ""), Number = (cc.PhoneNumbers[i] != null ? cc.PhoneNumbers[i].Value.StringValue : ""), Photo = (cc.ThumbnailImageData != null ? GetBitmap(cc.ThumbnailImageData) : null), }; lsc.Add(ct); } } /*lsc = contacts.Select(x => new Contacto() * { * Id = x.Identifier, * Name = (!string.IsNullOrEmpty(x.GivenName) ? x.GivenName : "") + (!string.IsNullOrEmpty(x.FamilyName) ? " " + x.FamilyName : ""), * Number = (x.PhoneNumbers.FirstOrDefault() != null ? x.PhoneNumbers.FirstOrDefault().Value.StringValue : ""), * Photo = (x.ThumbnailImageData != null ? GetBitmap(x.ThumbnailImageData) : null), * }).ToList();*/ } } catch (Exception e) { } return(lsc); }
private void OnGUI() { KitchenSink.OnGUIBack(); GUILayout.BeginArea(new Rect(50, 50, Screen.width - 100, Screen.height / 2 - 50)); //this is the original way of how objective C use call back functions: Delegates using separate files. //in this case the file is PeoplePickerNavigationControllerDelegate.cs //we have have it easier to use delegates instead of creating new files for each delegate. //see examples above. Ex: PersonalXT.CalendarAccess += delegate( .... if (GUILayout.Button("pick/select from contacts", GUILayout.ExpandHeight(true))) { ABPeoplePickerNavigationController picker = new ABPeoplePickerNavigationController(); if (pickerDelegate == null) { pickerDelegate = new PeoplePickerNavigationControllerDelegate(); } picker.peoplePickerDelegate = pickerDelegate; UIApplication.deviceRootViewController.PresentViewController(picker, true, null); } if (GUILayout.Button("get all contacts", GUILayout.ExpandHeight(true))) { Log("Address book authorization status: " + ABAddressBook.GetAuthorizationStatus()); var addressBook = ABAddressBook.Create(null, null); addressBook.RequestAccess(delegate(bool granted, NSError error) { Log("Granted: " + granted); //convienent function to get the names of the contacts string[] contactList = PersonalXT.GetAllContactNames(); for (int i = 0; i < contactList.Length; i++) { Log("Contact " + i + ": " + contactList[i]); } }); } if (GUILayout.Button("add new contacts", GUILayout.ExpandHeight(true))) { addNewContact(); } if (GUILayout.Button("init Calendar and show events within 30 days", GUILayout.ExpandHeight(true))) { checkEventStoreAccessForCalendar(); } if (GUILayout.Button("add an event for tomorrow", GUILayout.ExpandHeight(true))) { addEventForTomorrow(); } if (GUILayout.Button("add alarm to events", GUILayout.ExpandHeight(true))) { createAlarmForEvents(); } if (GUILayout.Button("add reminder with geolocation of current location", GUILayout.ExpandHeight(true))) { PersonalXT.RequestReminderAccess(); } if (GUILayout.Button("reverse geocode happiest place on earth", GUILayout.ExpandHeight(true))) { CLLocation location = new CLLocation(33.809, -117.919); CLGeocoder geocoder = new CLGeocoder(); geocoder.ReverseGeocodeLocation(location, delegate(object[] placemarks, NSError error) { if (error != null) { Debug.Log(error.LocalizedDescription()); } else { foreach (var p in placemarks) { var placemark = p as CLPlacemark; Debug.Log("placemark: " + placemark.name + "\n" + ABAddressFormatting.ABCreateString(placemark.addressDictionary, true)); } } }); } if (GUILayout.Button("Significant location change", GUILayout.ExpandHeight(true))) { if (!CLLocationManager.LocationServicesEnabled() || !CLLocationManager.SignificantLocationChangeMonitoringAvailable()) { Debug.Log("Significant change monitoring not available."); } else { // CLLocationManager manager = new CLLocationManager(); manager.StartMonitoringSignificantLocationChanges(); } } //commented out remove all events and reminders so users don't accidentally remove important events /* * if (GUILayout.Button("remove all Events", GUILayout.ExpandHeight(true))) { * PersonalXT.RemoveAllEvents(); * Log ("Removed events"); * } * * if (GUILayout.Button("remove all Reminders", GUILayout.ExpandHeight(true))) { * PersonalXT.GetAllReminders(); //you can get all the reminders and handle them in line 59 above * //PersonalXT.RemoveAllReminders(); //or you can simply call removeAllReminders * }*/ GUILayout.EndArea(); OnGUILog(); }
public override void ViewDidLoad() { base.ViewDidLoad(); Title = "Address Book"; // add a button to the nav bar that will select a contact to edit UIButton btnSelectContact = UIButton.FromType(UIButtonType.RoundedRect); btnSelectContact.SetTitle("Select Contact", UIControlState.Normal); NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Action, SelectContact), false); // disable first two fields until the user select a contact EnableTextFields(false); // wire up keyboard hiding txtPhoneLabel.ShouldReturn += (t) => { t.ResignFirstResponder(); return(true); }; txtPhoneNumber.ShouldReturn += (t) => { t.ResignFirstResponder(); return(true); }; txtFirstName.ShouldReturn += (t) => { t.ResignFirstResponder(); return(true); }; txtLastName.ShouldReturn += (t) => { t.ResignFirstResponder(); return(true); }; // wire up event handlers btnSaveChanges.TouchUpInside += BtnSaveChangesTouchUpInside; btnAddPhoneNumber.TouchUpInside += BtnAddPhoneNumberTouchUpInside; #region -= Sample code showing how to loop through all the records =- //==== This block of code writes out each person contact in the address book and // each phone number for that person to the console, just to illustrate the code // neccessary to access each item // instantiate a reference to the address book NSError err; addressBook = ABAddressBook.Create(out err); if (err != null) { return; } // if you want to subscribe to changes addressBook.ExternalChange += (object sender, ExternalChangeEventArgs e) => { // code to deal with changes }; addressBook.RequestAccess(delegate(bool granted, NSError error) { if (!granted || error != null) { return; } // for each record foreach (ABRecord item in addressBook) { Console.WriteLine(item.Type.ToString() + " " + item.Id); // there are two possible record types, person and group if (item.Type == ABRecordType.Person) { // since we've already tested it to be a person, just create a shortcut to that // type ABPerson person = item as ABPerson; Console.WriteLine(person.FirstName + " " + person.LastName); // get the phone numbers ABMultiValue <string> phones = person.GetPhones(); foreach (ABMultiValueEntry <string> val in phones) { Console.Write(val.Label + ": " + val.Value); } } } // save changes (if you were to have made any) //addressBook.Save(); // or cancel them //addressBook.Revert(); }); //==== #endregion #region -= keyboard stuff =- // wire up our keyboard events NSNotificationCenter.DefaultCenter.AddObserver( UIKeyboard.WillShowNotification, delegate(NSNotification n) { KeyboardOpenedOrClosed(n, "Open"); }); NSNotificationCenter.DefaultCenter.AddObserver( UIKeyboard.WillHideNotification, delegate(NSNotification n) { KeyboardOpenedOrClosed(n, "Close"); }); #endregion }