示例#1
0
        //================================================================================

        //================================================================================
        /// <summary>
        /// Called when a phone number is swiped for deletion. Illustrates how to delete a multivalue property
        /// </summary>
        protected void DeletePhoneNumber(int phoneNumberID)
        {
            using (ABAddressBook addressBook = new ABAddressBook())
            {
                ABPerson contact = addressBook.GetPerson(this._contactID);

                //---- get the phones and copy them to a mutable set of multivalues (so we can edit)
                ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                //---- loop backwards and delete the phone number
                for (int i = phones.Count - 1; i >= 0; i--)
                {
                    if (phones[i].Identifier == phoneNumberID)
                    {
                        phones.RemoveAt(i);
                    }
                }

                //---- attach the phones back to the contact
                contact.SetPhones(phones);

                //---- save the changes
                addressBook.Save();

                //---- show an alert, letting the user know the number deletion was successful
                new UIAlertView("Alert", "Phone Number Deleted", null, "OK", null).Show();

                //---- repopulate the page
                this.PopulatePage(contact);
            }
        }
示例#2
0
        //================================================================================

        //================================================================================
        /// <summary>
        ///
        /// </summary>
        protected void BtnAddPhoneNumberTouchUpInside(object sender, EventArgs e)
        {
            //---- get a reference to the contact
            using (ABAddressBook addressBook = new ABAddressBook())
            {
                ABPerson contact = addressBook.GetPerson(this._contactID);

                //---- get the phones and copy them to a mutable set of multivalues (so we can edit)
                ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                //---- add the phone number to the phones via the multivalue.Add method
                phones.Add(new NSString(this.txtPhoneLabel.Text), new NSString(this.txtPhoneNumber.Text));

                //---- attach the phones back to the contact
                contact.SetPhones(phones);

                //---- save the address book changes
                addressBook.Save();

                //---- show an alert, letting the user know the number addition was successful
                new UIAlertView("Alert", "Phone Number Added", null, "OK", null).Show();

                //---- update the page
                this.PopulatePage(contact);

                //---- we have to call reload to refresh the table because the action didn't originate
                // from the table.
                this.tblPhoneNumbers.ReloadData();
            }
        }
示例#3
0
        /*public static async Task<bool> SendSMS(string number, string message, string name,
         *          string ConfirmOrBOM, bool AutoCall = false, string TodayOrTomorrow = null)
         * {
         *      //var notifier = new iOSReminderService();
         *      try
         *      {
         *              var vc = UIApplication.SharedApplication.KeyWindow.RootViewController;
         *
         *              while (vc.PresentedViewController != null)
         *              {
         *                      vc = vc.PresentedViewController;
         *              }
         *              if (MFMessageComposeViewController.CanSendText)
         *              {
         *                      Console.WriteLine("SMS available");
         *
         *                      var messageController =
         *                              new MessageComposeController();
         *
         *                      messageController.Finished +=
         *                              //async
         *                              (sender, e) =>
         *                      {
         *                              Console.WriteLine("sms sent: {0}", messageController.Body);
         *                              AutoCaller.Calling = false;
         *                              if (string.Equals(ConfirmOrBOM, Values.BOM))
         *                              {
         *                                      CalendarService.IsConfirmingAppointments = false;
         *
         *                                      //set notification time to confirm appointment
         *                              }
         *                              else {
         *                                      CalendarService.IsConfirmingAppointments = true;
         *
         *                              }
         *                              messageController.DismissViewController(DeviceUtil.CanAnimate, null);
         *                      };
         *
         *                      messageController.Body = message;
         *                      messageController.Recipients = new string[] { number };
         *                      vc.PresentModalViewController(messageController, false);
         *              }
         *              else {
         *                      Console.WriteLine("Can't send text");
         *              }
         *              return true;
         *      }
         *      catch (Exception e)
         *      {
         *              Console.WriteLine("PhoneContacts.SendSMS() error: {0}", e.Message);
         *              if (string.Equals(ConfirmOrBOM, Values.BOM))
         *              {
         *                      //notifier.Remind(DateTime.Now.AddMilliseconds(0), "BOM Confirmation failed sending to " + name, "Text Confirmation Failed");
         *              }
         *              else {
         *
         *                      if (string.Equals(TodayOrTomorrow, Values.TODAY))
         *                      {
         *                              //notifier.Remind(DateTime.Now.AddMilliseconds(0), "SMS failed to send", "Couldn't confirm " + name + " for later's meeting");
         *                      }
         *                      else {
         *                              //notifier.Remind(DateTime.Now.AddMilliseconds(0), "SMS failed to send", "Couldn't confirm " + name + " for tomorrow's meeting");
         *                      }
         *              }
         *      }
         *      return false;
         * }*/

        /*string SaveDefaultImage(ContactData contact){
         *      string filename = System.IO.Path.Combine (Environment.GetFolderPath
         *              (Environment.SpecialFolder.Personal),
         *              "placeholder-contact-male.png");
         *
         *      Console.WriteLine("Assigned default image to {0} {1}. Saving it as {1}",
         *              contact.FirstName, contact.LastName, filename);
         *
         *      return filename;
         * }
         * string SaveImageThenGetPath(ContactData contact, NSData image, ABPersonImageFormat format){
         *      string filename = "";
         *
         *      try{
         *              if(format == ABPersonImageFormat.Thumbnail){
         *                      filename = System.IO.Path.Combine (Environment.GetFolderPath
         *                              (Environment.SpecialFolder.Personal),
         *                              string.Format("{0}.jpg", contact.ID));
         *              }else{
         *                      filename = System.IO.Path.Combine (Environment.GetFolderPath
         *                              (Environment.SpecialFolder.Personal),
         *                              string.Format("{0}-large.jpg", contact.ID));
         *              }
         *
         *              image.Save (filename, true);
         *
         *              Console.WriteLine("Found {0} {1}'s image. Saving it as {2}",
         *                      contact.FirstName, contact.LastName, filename);
         *
         *              return filename;
         *      }catch(Exception e){
         *              Console.WriteLine ("Error in SaveImageThenGetPath(): {0}", e.Message);
         *      }
         *      return string.Empty;
         * }*/
        public bool SaveContactToDevice(string firstName, string lastName, string phone, string aff)
        {
            try {
                ABAddressBook ab = new ABAddressBook();
                ABPerson      p  = new ABPerson();

                p.FirstName    = firstName;
                p.LastName     = lastName;
                p.Organization = aff;
                //p.GetImage(ABPersonImageFormat.Thumbnail).

                ABMutableMultiValue <string> phones = new ABMutableStringMultiValue();
                phones.Add(phone, ABPersonPhoneLabel.Mobile);

                p.SetPhones(phones);

                ab.Add(p);
                ab.Save();

                UserDialogs.Instance.ShowSuccess("Contact saved: " + firstName + " " + lastName, 2000);

                return(true);
            } catch (Exception e) {
                System.Console.WriteLine("[iOS.PhoneContacts] Couldn't save contact: {0} {1}, {2}", firstName, lastName, e.Message);
                UserDialogs.Instance.ShowError("Failed to save contact: " + firstName + " " + lastName + ". Pls try again.", 2000);
            }
            return(false);
        }
示例#4
0
        protected void BtnSaveChangesTouchUpInside(object sender, EventArgs e)
        {
            using (ABAddressBook addressBook = new ABAddressBook()) {
                ABPerson contact = addressBook.GetPerson(contactID);

                if (contact != null)
                {
                    // save contact name information
                    contact.FirstName = txtFirstName.Text;
                    contact.LastName  = txtLastName.Text;

                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                    // remove all phones data
                    // HACK: Cast nint to int
                    for (int i = (int)phones.Count - 1; i >= 0; i--)
                    {
                        phones.RemoveAt(i);
                    }

                    // add the phone number to the phones from the table data source
                    for (int i = 0; i < PhoneNumberTableSource.labels.Count; i++)
                    {
                        phones.Add(new NSString(PhoneNumberTableSource.numbers [i]), new NSString(PhoneNumberTableSource.labels [i]));
                    }

                    // attach the phones back to the contact
                    contact.SetPhones(phones);

                    // save the address book changes
                    addressBook.Save();

                    // show an alert, letting the user know information saved successfully
                    new UIAlertView("Alert", "Contact Information Saved!", null, "OK", null).Show();

                    // update the page
                    PopulatePage(contact);

                    // we have to call reload to refresh the table because the action didn't originate
                    // from the table.
                    tblPhoneNumbers.ReloadData();
                }
                else
                {
                    new UIAlertView("Alert", "Please select a contact using the top right button", null, "OK", null).Show();
                }
            }
        }
示例#5
0
    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.");
    }
示例#6
0
        private void PopulateContacts()
        {
            var addressBook = new ABAddressBook();
            var people      = addressBook.GetPeople();

            // clear address book
            foreach (var person in people)
            {
                addressBook.Remove(person);
            }

            addressBook.Save();

            var random = new Random();

            // create 200 random contacts
            foreach (var name in _names)
            {
                // create an ABRecordRef
                var record = new ABPerson();
                record.FirstName = name[0];
                record.LastName  = name[1];
                var phones = new ABMutableStringMultiValue();
                phones.Add(GeneratePhone(random).ToString(), ABPersonPhoneLabel.Mobile);

                if (random.Next() % 2 == 1)
                {
                    phones.Add(GeneratePhone(random).ToString(), ABPersonPhoneLabel.Main);
                }

                record.SetPhones(phones);

                addressBook.Add(record);
            }
            addressBook.Save();
        }
        protected void BtnAddPhoneNumberTouchUpInside(object sender, EventArgs e)
        {
            // get a reference to the contact
            using (ABAddressBook addressBook = new ABAddressBook()) {
                ABPerson contact = addressBook.GetPerson(contactID);
                if (contact != null)
                {
                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                    // add the phone number to the phones via the multivalue.Add method
                    if (txtPhoneNumber.Text != null || txtPhoneLabel.Text != null)
                    {
                        phones.Add(new NSString(txtPhoneNumber.Text), new NSString(txtPhoneLabel.Text));

                        // attach the phones back to the contact
                        contact.SetPhones(phones);

                        // save the address book changes
                        addressBook.Save();

                        // show an alert, letting the user know the number addition was successful
                        new UIAlertView("Alert", "Phone Number Added", null, "OK", null).Show();

                        // update the page
                        PopulatePage(contact);

                        // we have to call reload to refresh the table because the action didn't originate
                        // from the table.
                        tblPhoneNumbers.ReloadData();
                    }
                    else
                    {
                        // show an alert, letting the user know he has to fill the phone label and phone number
                        new UIAlertView("Alert", "You have to fill a label and a phone number", null, "OK", null).Show();
                    }
                }
                else
                {
                    new UIAlertView("Alert", "Please select a contact using the top right button", null, "OK", null).Show();
                }
            }
        }
        private void PopulateContacts()
        {
            var addressBook = new ABAddressBook();
            var people = addressBook.GetPeople();

            // clear address book
            foreach(var person in people)
            {
                addressBook.Remove(person);
            }

            addressBook.Save();

            var random = new Random();
            // create 200 random contacts
            foreach (var name in _names)
            {
              // create an ABRecordRef
                var record = new ABPerson();
                record.FirstName = name[0];
                record.LastName = name[1];
                var phones = new ABMutableStringMultiValue();
                phones.Add(GeneratePhone(random).ToString(), ABPersonPhoneLabel.Mobile);

                if(random.Next() % 2 == 1) {
                    phones.Add(GeneratePhone(random).ToString(), ABPersonPhoneLabel.Main);
                }

                record.SetPhones(phones);

                addressBook.Add(record);
            }
            addressBook.Save();
        }
示例#9
0
        private static void AddContactInfo(ABPerson selectedPerson, Item item)
        {
            // find the contact in the address book
            var book = new AddressBook();
            Contact contact = null;
            if (book.LoadSupported)
                contact = book.Load(selectedPerson.Id.ToString());
            else
                contact = book.FirstOrDefault(c => c.Id == selectedPerson.Id.ToString());

            if (contact == null)
                return;

            // make a copy of the item
            var itemCopy = new Item(item, true);

            // get more info from the address book
            var mobile = (from p in contact.Phones where
                p.Type == Xamarin.Contacts.PhoneType.Mobile
                select p.Number).FirstOrDefault();
            var home = (from p in contact.Phones where
                p.Type == Xamarin.Contacts.PhoneType.Home
                select p.Number).FirstOrDefault();
            var work = (from p in contact.Phones where
                p.Type == Xamarin.Contacts.PhoneType.Work
                select p.Number).FirstOrDefault();
            var email = (from em in contact.Emails
                select em.Address).FirstOrDefault();
            //var website = (from w in contact.Websites
            //    select w.Address).FirstOrDefault();

            string birthday = null;
            if (selectedPerson.Birthday != null)
                birthday = ((DateTime)selectedPerson.Birthday).ToString("d");

            if (birthday != null)
                item.GetFieldValue(FieldNames.Birthday, true).Value = birthday;
            if (mobile != null)
                item.GetFieldValue(FieldNames.Phone, true).Value = mobile;
            if (home != null)
                item.GetFieldValue(FieldNames.HomePhone, true).Value = home;
            if (work != null)
                item.GetFieldValue(FieldNames.WorkPhone, true).Value = work;
            if (email != null)
                item.GetFieldValue(FieldNames.Email, true).Value = email;
            /*
            if (website != null)
                item.GetFieldValue(FieldNames.Website, true).Value = website
                */

            // save changes to local storage
            Folder folder = App.ViewModel.LoadFolder(item.FolderID);
            StorageHelper.WriteFolder(folder);

            // enqueue the Web Request Record
            RequestQueue.EnqueueRequestRecord(RequestQueue.UserQueue,
                new RequestQueue.RequestRecord()
                {
                    ReqType = RequestQueue.RequestRecord.RequestType.Update,
                    Body = new List<Item>() { itemCopy, item },
                    BodyTypeName = "Item",
                    ID = item.ID
                });

            // add the zaplify contact header to a special Zaplify "related name" field
            // use the native address book because the cross-platform AddressBook class is read-only
            var ab = new ABAddressBook();
            var contactToModify = ab.GetPerson(selectedPerson.Id);

            /* RelatedNames doesn't work on a real device (iPad)
            var relatedNames = contactToModify.GetRelatedNames().ToMutableMultiValue();
            if (relatedNames.Any(name => name.Label == ZaplifyContactHeader))
            {
                // remove the existing one (can't figure out a way to get a mutable ABMultiValueEntry out of zapField)
                var zapField = relatedNames.Single(name => name.Label == ZaplifyContactHeader);
                relatedNames.RemoveAt(relatedNames.GetIndexForIdentifier(zapField.Identifier));
            }
            // add the Zaplify related name field with the itemID value
            relatedNames.Add(item.ID.ToString(), new MonoTouch.Foundation.NSString(ZaplifyContactHeader));
            contactToModify.SetRelatedNames(relatedNames);
            */

            var zapField = contactToModify.Note;
            if (zapField == null)
                contactToModify.Note = ZaplifyContactHeader + item.ID.ToString();
            else
            {
                if (zapField.Contains(ZaplifyContactHeader))
                {
                    var idstring = GetContactID(zapField);
                    if (idstring != null)
                        contactToModify.Note = zapField.Replace(idstring, item.ID.ToString());
                    else
                        contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
                }
                else
                    contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
            }

            // save changes to the address book
            ab.Save();
        }
        // Called when a phone number is swiped for deletion. Illustrates how to delete a multivalue property
        protected void DeletePhoneNumber(int phoneNumberID)
        {
            using (ABAddressBook addressBook = new ABAddressBook ()) {
                ABPerson contact = addressBook.GetPerson (contactID);

                // get the phones and copy them to a mutable set of multivalues (so we can edit)
                ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();

                // loop backwards and delete the phone number
                for (int i = phones.Count - 1; i >= 0; i--) {
                    if (phones [i].Identifier == phoneNumberID)
                        phones.RemoveAt (i);
                }

                // attach the phones back to the contact
                contact.SetPhones (phones);

                // save the changes
                addressBook.Save ();

                // show an alert, letting the user know the number deletion was successful
                new UIAlertView ("Alert", "Phone Number Deleted", null, "OK", null).Show ();

                // repopulate the page
                PopulatePage (contact);
            }
        }
        protected void BtnSaveChangesTouchUpInside(object sender, EventArgs e)
        {
            using (ABAddressBook addressBook = new ABAddressBook ()) {
                ABPerson contact = addressBook.GetPerson (contactID);

                if (contact != null) {
                    // save contact name information
                    contact.FirstName = txtFirstName.Text;
                    contact.LastName = txtLastName.Text;

                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();

                    // remove all phones data
                    for (int i = phones.Count - 1; i >= 0; i--) {
                        phones.RemoveAt (i);
                    }

                    // add the phone number to the phones from the table data source
                    for (int i = 0; i < PhoneNumberTableSource.labels.Count; i++) {
                        phones.Add (new NSString (PhoneNumberTableSource.numbers [i]), new NSString (PhoneNumberTableSource.labels [i]));
                    }

                    // attach the phones back to the contact
                    contact.SetPhones (phones);

                    // save the address book changes
                    addressBook.Save ();

                    // show an alert, letting the user know information saved successfully
                    new UIAlertView ("Alert", "Contact Information Saved!", null, "OK", null).Show ();

                    // update the page
                    PopulatePage (contact);

                    // we have to call reload to refresh the table because the action didn't originate
                    // from the table.
                    tblPhoneNumbers.ReloadData ();
                } else {
                    new UIAlertView ("Alert", "Please select a contact using the top right button", null, "OK", null).Show ();
                }
            }
        }
        protected void BtnAddPhoneNumberTouchUpInside(object sender, EventArgs e)
        {
            // get a reference to the contact
            using (ABAddressBook addressBook = new ABAddressBook ()) {
                ABPerson contact = addressBook.GetPerson (contactID);
                if (contact != null) {
                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();

                    // add the phone number to the phones via the multivalue.Add method
                    if (txtPhoneNumber.Text != null || txtPhoneLabel.Text != null) {
                        phones.Add (new NSString (txtPhoneNumber.Text), new NSString (txtPhoneLabel.Text));

                        // attach the phones back to the contact
                        contact.SetPhones (phones);

                        // save the address book changes
                        addressBook.Save ();

                        // show an alert, letting the user know the number addition was successful
                        new UIAlertView ("Alert", "Phone Number Added", null, "OK", null).Show ();

                        // update the page
                        PopulatePage (contact);

                        // we have to call reload to refresh the table because the action didn't originate
                        // from the table.
                        tblPhoneNumbers.ReloadData ();
                    } else {
                        // show an alert, letting the user know he has to fill the phone label and phone number
                        new UIAlertView ("Alert", "You have to fill a label and a phone number", null, "OK", null).Show ();
                    }
                } else {
                    new UIAlertView ("Alert", "Please select a contact using the top right button", null, "OK", null).Show ();
                }
            }
        }
示例#13
0
        private static void AddContactInfo(ABPerson selectedPerson, Item item)
        {
            // find the contact in the address book
            var     book    = new AddressBook();
            Contact contact = null;

            if (book.LoadSupported)
            {
                contact = book.Load(selectedPerson.Id.ToString());
            }
            else
            {
                contact = book.FirstOrDefault(c => c.Id == selectedPerson.Id.ToString());
            }

            if (contact == null)
            {
                return;
            }

            // make a copy of the item
            var itemCopy = new Item(item, true);

            // get more info from the address book
            var mobile = (from p in contact.Phones where
                          p.Type == Xamarin.Contacts.PhoneType.Mobile
                          select p.Number).FirstOrDefault();
            var home = (from p in contact.Phones where
                        p.Type == Xamarin.Contacts.PhoneType.Home
                        select p.Number).FirstOrDefault();
            var work = (from p in contact.Phones where
                        p.Type == Xamarin.Contacts.PhoneType.Work
                        select p.Number).FirstOrDefault();
            var email = (from em in contact.Emails
                         select em.Address).FirstOrDefault();
            //var website = (from w in contact.Websites
            //    select w.Address).FirstOrDefault();

            string birthday = null;

            if (selectedPerson.Birthday != null)
            {
                birthday = ((DateTime)selectedPerson.Birthday).ToString("d");
            }

            if (birthday != null)
            {
                item.GetFieldValue(FieldNames.Birthday, true).Value = birthday;
            }
            if (mobile != null)
            {
                item.GetFieldValue(FieldNames.Phone, true).Value = mobile;
            }
            if (home != null)
            {
                item.GetFieldValue(FieldNames.HomePhone, true).Value = home;
            }
            if (work != null)
            {
                item.GetFieldValue(FieldNames.WorkPhone, true).Value = work;
            }
            if (email != null)
            {
                item.GetFieldValue(FieldNames.Email, true).Value = email;
            }

            /*
             * if (website != null)
             *  item.GetFieldValue(FieldNames.Website, true).Value = website
             */

            // save changes to local storage
            Folder folder = App.ViewModel.LoadFolder(item.FolderID);

            StorageHelper.WriteFolder(folder);

            // enqueue the Web Request Record
            RequestQueue.EnqueueRequestRecord(RequestQueue.UserQueue,
                                              new RequestQueue.RequestRecord()
            {
                ReqType = RequestQueue.RequestRecord.RequestType.Update,
                Body    = new List <Item>()
                {
                    itemCopy, item
                },
                BodyTypeName = "Item",
                ID           = item.ID
            });

            // add the zaplify contact header to a special Zaplify "related name" field
            // use the native address book because the cross-platform AddressBook class is read-only
            var ab = new ABAddressBook();
            var contactToModify = ab.GetPerson(selectedPerson.Id);

            /* RelatedNames doesn't work on a real device (iPad)
             * var relatedNames = contactToModify.GetRelatedNames().ToMutableMultiValue();
             * if (relatedNames.Any(name => name.Label == ZaplifyContactHeader))
             * {
             *  // remove the existing one (can't figure out a way to get a mutable ABMultiValueEntry out of zapField)
             *  var zapField = relatedNames.Single(name => name.Label == ZaplifyContactHeader);
             *  relatedNames.RemoveAt(relatedNames.GetIndexForIdentifier(zapField.Identifier));
             * }
             * // add the Zaplify related name field with the itemID value
             * relatedNames.Add(item.ID.ToString(), new MonoTouch.Foundation.NSString(ZaplifyContactHeader));
             * contactToModify.SetRelatedNames(relatedNames);
             */

            var zapField = contactToModify.Note;

            if (zapField == null)
            {
                contactToModify.Note = ZaplifyContactHeader + item.ID.ToString();
            }
            else
            {
                if (zapField.Contains(ZaplifyContactHeader))
                {
                    var idstring = GetContactID(zapField);
                    if (idstring != null)
                    {
                        contactToModify.Note = zapField.Replace(idstring, item.ID.ToString());
                    }
                    else
                    {
                        contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
                    }
                }
                else
                {
                    contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
                }
            }

            // save changes to the address book
            ab.Save();
        }