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); }
public void ShowContactsPermissionsWithTitle(string requestTitle, string message, string denyButtonTitle, string grantButtonTitle, ClusterPrePermissionCompletionHandler completionHandler) { if (requestTitle.Length == 0) { requestTitle = @"Access Contacts?"; } denyButtonTitle = GetTitleForType(ClusterTitleType.Deny, denyButtonTitle); grantButtonTitle = GetTitleForType(ClusterTitleType.Request, grantButtonTitle); ABAuthorizationStatus status = ABAddressBook.GetAuthorizationStatus(); if (status == ABAuthorizationStatus.NotDetermined) { _contactPermissionCompletionHandler = completionHandler; _preContactPermissionAlertView = new UIAlertView(requestTitle, message, new AlertViewDelegate(this), denyButtonTitle, grantButtonTitle); _preContactPermissionAlertView.Show(); } else { if (completionHandler != null) { completionHandler((status == ABAuthorizationStatus.Authorized), ClusterDialogResult.NoActionTaken, ClusterDialogResult.NoActionTaken); } } }
private void CheckStatus() { var info = NSBundle.MainBundle.InfoDictionary; if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { if (!info.ContainsKey(new NSString("NSContactsUsageDescription"))) { throw new UnauthorizedAccessException("On iOS 10 and higher you must set NSContactsUsageDescription in your Info.plist file to enable Authorization Requests for Photo Library access!"); } } if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { var status = ABAddressBook.GetAuthorizationStatus(); if (status != ABAuthorizationStatus.Authorized) { throw new System.Security.SecurityException("AddressBook has not been granted permission"); } } if (this.addressBook == null) { this.addressBook = new ABAddressBook(); this.provider = new ContactQueryProvider(this.addressBook); } }
void FireContactPermissionCompletionHandler() { ABAuthorizationStatus status = ABAddressBook.GetAuthorizationStatus(); if (_contactPermissionCompletionHandler != null) { ClusterDialogResult userDialogResult = ClusterDialogResult.Granted; ClusterDialogResult systemDialogResult = ClusterDialogResult.Granted; if (status == ABAuthorizationStatus.NotDetermined) { userDialogResult = ClusterDialogResult.Denied; systemDialogResult = ClusterDialogResult.NoActionTaken; } else if (status == ABAuthorizationStatus.Authorized) { userDialogResult = ClusterDialogResult.Granted; systemDialogResult = ClusterDialogResult.Granted; } else if (status == ABAuthorizationStatus.Denied) { userDialogResult = ClusterDialogResult.Granted; systemDialogResult = ClusterDialogResult.Denied; } else if (status == ABAuthorizationStatus.Restricted) { userDialogResult = ClusterDialogResult.Granted; systemDialogResult = ClusterDialogResult.ParentallyRestricted; } _contactPermissionCompletionHandler((status == ABAuthorizationStatus.Authorized), userDialogResult, systemDialogResult); _contactPermissionCompletionHandler = null; } }
void CheckAddressBookAccess() { switch (ABAddressBook.GetAuthorizationStatus()) { case ABAuthorizationStatus.Authorized: DispatchQueue.MainQueue.DispatchAsync(ShowNewContactViewController); break; case ABAuthorizationStatus.NotDetermined: RequestAddressBookAccess(); break; case ABAuthorizationStatus.Denied: case ABAuthorizationStatus.Restricted: var alert = UIAlertController.Create(string.Empty, "contacts_permission_request_denied".LocalizedString("Contacts permission request denied"), UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("ok".LocalizedString("OK title for button"), UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); break; default: break; } }
public static void CheckAddressBookPermission(bool assert_granted = false) { #if __MACCATALYST__ NUnit.Framework.Assert.Ignore("CheckAddressBookPermission -> Ignore in MacCat, it hangs since our TCC hack does not work on BS."); #endif switch (ABAddressBook.GetAuthorizationStatus()) { case ABAuthorizationStatus.NotDetermined: if (IgnoreTestThatRequiresSystemPermissions()) { NUnit.Framework.Assert.Ignore("This test would show a dialog to ask for permission to access the address book."); } // We don't request access here, because there's no global method to request access (an addressbook instance is required). // Interestingly there is a global method to determine if access has been granted... break; case ABAuthorizationStatus.Restricted: case ABAuthorizationStatus.Denied: if (assert_granted) { NUnit.Framework.Assert.Fail("This test requires permission to access the address book."); } break; } }
public Task <bool> RequestPermission() { var info = NSBundle.MainBundle.InfoDictionary; if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { if (!info.ContainsKey(new NSString("NSContactsUsageDescription"))) { throw new UnauthorizedAccessException("On iOS 10 and higher you must set NSContactsUsageDescription in your Info.plist file to enable Authorization Requests for Photo Library access!"); } } var tcs = new TaskCompletionSource <bool>(); if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { var status = ABAddressBook.GetAuthorizationStatus(); if (status == ABAuthorizationStatus.Denied || status == ABAuthorizationStatus.Restricted) { tcs.SetResult(false); } else { if (this.addressBook == null) { this.addressBook = new ABAddressBook(); this.provider = new ContactQueryProvider(this.addressBook); } if (status == ABAuthorizationStatus.NotDetermined) { this.addressBook.RequestAccess((s, e) => { tcs.SetResult(s); if (!s) { this.addressBook.Dispose(); this.addressBook = null; this.provider = null; } }); } else { tcs.SetResult(true); } } } else { tcs.SetResult(true); } return(tcs.Task); }
public void AddressBook_Constants() { // we want to ensure we can get the constants without authorization (on iOS 6.0+) so this application // needs to be unauthorized (in settings.app). Note: authorization checks only occurs on devices if ((Runtime.Arch == Arch.DEVICE) && UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { Assert.That(ABAddressBook.GetAuthorizationStatus(), Is.Not.EqualTo(ABAuthorizationStatus.Authorized), "Please deny access to contacts for this this application (it's important for this test)"); } Assert.IsNotNull(ABPersonAddressKey.City, "ABPersonAddressKey"); }
public void AddressBook_Constants() { // we want to ensure we can get the constants without authorization (on iOS 6.0+) so this application // needs to be unauthorized (in settings.app). Note: authorization checks only occurs on devices if ((Runtime.Arch == Arch.DEVICE) && UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { Assert.That(ABAddressBook.GetAuthorizationStatus(), Is.Not.EqualTo(ABAuthorizationStatus.Authorized), "Please deny access to contacts for this this application (it's important for this test)"); } TestRuntime.AssertSystemVersion(PlatformName.MacCatalyst, 14, 0, throwIfOtherPlatform: false); // The AddressBook framework was introduced in Mac Catalyst 14.0 Assert.IsNotNull(ABPersonAddressKey.City, "ABPersonAddressKey"); }
public static async Task <bool> RequestAddressBookAuthorization() { ManualResetEventSlim waiter = new ManualResetEventSlim(); if (ABAddressBook.GetAuthorizationStatus() == ABAuthorizationStatus.NotDetermined) { var addressBook = new ABAddressBook(); addressBook.RequestAccess(delegate(bool arg1, NSError arg2) { waiter.Set(); }); await Task.Run(() => waiter.Wait()); } return(ABAddressBook.GetAuthorizationStatus() == ABAuthorizationStatus.Authorized); /*Version version = new Version (MonoTouch.Constants.Version); * if (version < new Version (6,0)) { * return true; * } * * switch(ABAddressBook.GetAuthorizationStatus ()) { * case ABAuthorizationStatus.Authorized: * return true; * case ABAuthorizationStatus.NotDetermined: * return await Task.Run( () => { * var waiter = new ManualResetEventSlim(); * bool result = true; * * NSError error; * var addressBook = ABAddressBook.Create(out error); * if (error != null) * return false; * * addressBook.RequestAccess(delegate(bool arg1, NSError arg2) { * if (arg2 != null) * SystemLog.Error(arg2.LocalizedDescription); * SystemLog.Debug("REQUESTED ACCESS - {0}", arg1); * result = arg1; * waiter.Set(); * }); * //await Task.Run(() => waiter.Wait()); * waiter.Wait(); * return result; * }); * default: * return false; * }*/ /*SystemLog.Debug("RequestAddressBookAuthorization->Before GetAuthorization"); * var xx = ABAddressBook.GetAuthorizationStatus(); * SystemLog.Debug("RequestAddressBookAuthorization->After GetAuthorization - {0}", xx); * return xx == ABAuthorizationStatus.Authorized;*/ }
public async Task <IEnumerable <Contact> > GetContacts() { return(await Task.Run(() => { var res = new List <Contact>(); var status = ABAddressBook.GetAuthorizationStatus(); if (status == ABAuthorizationStatus.Denied || status == ABAuthorizationStatus.Restricted) { return res; } else { var addressBook = new ABAddressBook(); var valorizeRes = new Action(() => { var peoples = addressBook.GetPeople(); foreach (var item in peoples) { res.Add(new Contact { Name = $"{item.FirstName} {item.LastName}" }); } }); if (status == ABAuthorizationStatus.NotDetermined) { addressBook.RequestAccess((granted, e) => { if (granted) { valorizeRes.Invoke(); } }); } else { valorizeRes.Invoke(); } return res; } })); }
private void CheckStatus() { if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { var status = ABAddressBook.GetAuthorizationStatus(); if (status != ABAuthorizationStatus.Authorized) { throw new System.Security.SecurityException("AddressBook has not been granted permission"); } } if (this.addressBook == null) { this.addressBook = new ABAddressBook(); this.provider = new ContactQueryProvider(this.addressBook); } }
public Task <Boolean> RequestPermission() { var tcs = new TaskCompletionSource <Boolean>(); if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { var status = ABAddressBook.GetAuthorizationStatus(); if (status == ABAuthorizationStatus.Denied || status == ABAuthorizationStatus.Restricted) { tcs.SetResult(false); } else { if (addressBook == null) { addressBook = new ABAddressBook(); provider = new ContactQueryProvider(addressBook); } if (status == ABAuthorizationStatus.NotDetermined) { addressBook.RequestAccess( (s, e) => { tcs.SetResult(s); if (!s) { addressBook.Dispose(); addressBook = null; provider = null; } }); } else { tcs.SetResult(true); } } } else { tcs.SetResult(true); } return(tcs.Task); }
public ClusterAuthorizationStatus ContactsPermissionAuthorizationStatus() { var status = ABAddressBook.GetAuthorizationStatus(); switch (status) { case ABAuthorizationStatus.Authorized: return(ClusterAuthorizationStatus.Authorized); case ABAuthorizationStatus.Denied: return(ClusterAuthorizationStatus.Denied); case ABAuthorizationStatus.Restricted: return(ClusterAuthorizationStatus.Restricted); default: return(ClusterAuthorizationStatus.UnDetermined); } }
public void CheckAddressBookAccess() { ABAuthorizationStatus status = ABAddressBook.GetAuthorizationStatus(); switch (status) { case ABAuthorizationStatus.NotDetermined: ShowAlert(DataClass.Contacts, "not determined"); break; case ABAuthorizationStatus.Restricted: ShowAlert(DataClass.Contacts, "restricted"); break; case ABAuthorizationStatus.Denied: ShowAlert(DataClass.Contacts, "denied"); break; case ABAuthorizationStatus.Authorized: ShowAlert(DataClass.Contacts, "granted"); break; } }
void CheckAddressBookAccess() { switch (ABAddressBook.GetAuthorizationStatus()) { case ABAuthorizationStatus.Authorized: AccessGrantedForAddressBook(); break; case ABAuthorizationStatus.NotDetermined: RequestAddressBookAccess(); break; case ABAuthorizationStatus.Denied: case ABAuthorizationStatus.Restricted: UIAlertController alert = UIAlertController.Create("Privacy", "Permission was not granted for Contacts", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); break; default: break; } }
public void AtualizarAgendaDoAparelho(List <Contato> contatos) { // if the app was not authorized then we need to ask permission if (ABAddressBook.GetAuthorizationStatus() != ABAuthorizationStatus.Authorized) { agenda.RequestAccess(delegate(bool granted, NSError error) { if (error != null) { Console.WriteLine("Erro ao verificar a permissão de acesso a agenda local."); AtualizarAgenda(contatos); } else if (granted) { Console.WriteLine("Permissão a agenda local permitida."); } }); } else { Console.WriteLine("PERMISSÃO JÁ CONCEDIDA, LISTANDO."); AtualizarAgenda(contatos); } }
private ABAuthorizationStatus GetAuthorizationStatus() { return(ABAddressBook.GetAuthorizationStatus()); }
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 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); }
protected override string CheckAccess() { return(ABAddressBook.GetAuthorizationStatus().ToString()); }
public string CheckAddressBookAccess() { return(ABAddressBook.GetAuthorizationStatus().ToString()); }