示例#1
0
 void RequestAddressBookAccess()
 {
     addressBook.RequestAccess((bool granted, NSError error) => {
         if (!granted)
         {
             return;
         }
         DispatchQueue.MainQueue.DispatchAsync(() => AccessGrantedForAddressBook());
     });
 }
 void RequestAddressBookAccess()
 {
     addressBook.RequestAccess((bool granted, NSError error) => {
         if (!granted)
         {
             return;
         }
         DispatchQueue.MainQueue.DispatchAsync(() => ShowNewContactViewController());
     });
 }
        public Task RequestAccess()
        {
            if (addressBook == null)
            {
                return(Task.FromResult <object>(null));
            }

            var tcs = new TaskCompletionSource <object> ();

            addressBook.RequestAccess((granted, accessError) => tcs.SetResult(null));
            return(tcs.Task);
        }
示例#4
0
        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;*/
        }
示例#5
0
        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"));
                });
            }
        }
示例#6
0
        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");
                });
            }
        }
示例#7
0
        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;
                }
            }));
        }
        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);
            });
        }
示例#9
0
        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 override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // query the address book directly

            using (var addressBook = new ABAddressBook()) {
                // Get all people with a specfied name
                //var people = addressBook.GetPeopleWithName ("John Doe");
                //people.ToList ().ForEach (p => Console.WriteLine ("{0} {1} - ", p.FirstName, p.LastName));

                // Use Linq to query contacts based upon any criteria
                addressBook.RequestAccess((bool x, NSError z) => {});
                var people = addressBook.GetPeople();
                people.ToList().FindAll(p => p.LastName == "Smith").ForEach(
                    p => Console.WriteLine("{0} {1}", p.FirstName, p.LastName));
            }
        }
示例#11
0
        Task <PermissionStatus> RequestContactsPermission()
        {
            if (ContactsPermissionStatus != PermissionStatus.Unknown)
            {
                return(Task.FromResult(ContactsPermissionStatus));
            }

            addressBook = new ABAddressBook();

            var tcs = new TaskCompletionSource <PermissionStatus>();


            addressBook.RequestAccess((success, error) =>
            {
                tcs.SetResult((success ? PermissionStatus.Granted : PermissionStatus.Denied));
            });

            return(tcs.Task);
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad ();

            // query the address book directly

            using (var addressBook = new ABAddressBook ()) {

                // Get all people with a specfied name
                //var people = addressBook.GetPeopleWithName ("John Doe");
                //people.ToList ().ForEach (p => Console.WriteLine ("{0} {1} - ", p.FirstName, p.LastName));

                // Use Linq to query contacts based upon any criteria
                addressBook.RequestAccess((bool x, NSError z) => {});
                var people = addressBook.GetPeople ();
                people.ToList ().FindAll (p => p.LastName == "Smith").ForEach (
                p => Console.WriteLine ("{0} {1}", p.FirstName, p.LastName));
            }
        }
示例#13
0
        internal static Task <PermissionStatus> RequestContactsPermission()
        {
#if __IOS__
            if (ContactsPermissionStatus != PermissionStatus.Unknown)
            {
                return(Task.FromResult(ContactsPermissionStatus));
            }

            var addressBook = new ABAddressBook();

            var tcs = new TaskCompletionSource <PermissionStatus>();

            addressBook.RequestAccess((success, error) =>
            {
                tcs.TrySetResult((success ? PermissionStatus.Granted : PermissionStatus.Denied));
            });

            return(tcs.Task);
#elif __TVOS__
            return(Task.FromResult(PermissionStatus.Granted));
#endif
        }
示例#14
0
 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);
     }
 }
		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");
				});
			}
		}
        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
        }
        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
        }