Пример #1
0
        private static void CallSomeContactMethods()
        {
            //Create a Contact
            QContact newContact = new QContact
            {
                ContactName = "John Doe",
                FirstName   = "John",
                LastName    = "Doe",
                Email       = "*****@*****.**",
                Kind        = QContactKind.person
            };

            try
            {
                newContact.Save();

                Console.WriteLine("The new contact Id is: " + newContact.Id);

                List <QContact> contacts = QContact.Find();
                Console.WriteLine("Retrieved contacts: " + contacts.Count());

                Console.WriteLine("Deleting newContact");
                newContact.Delete();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error executing some operation with QContacts: " + ex.Message);
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
        public bool AddContacts(QContact qc)
        {
            Console.WriteLine("export contacts android");

            List <ContentProviderOperation> ops = new List <ContentProviderOperation>();
            int rawContactInsertIndex           = ops.Count;

            ContentProviderOperation.Builder builder =
                ContentProviderOperation.NewInsert(ContactsContract.RawContacts.ContentUri);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountType, null);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountName, null);
            ops.Add(builder.Build());

            //Name
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.StructuredName.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, qc.LastName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, qc.FirstName);
            ops.Add(builder.Build());

            //MOBILE
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, qc.Mobile);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Primary Phone");
            ops.Add(builder.Build());

            //Email
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Email.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data, qc.Email);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Email.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Label, "Email");
            ops.Add(builder.Build());

            //Address
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.StructuredPostal.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.Street, qc.Addr);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.City, "");
            ops.Add(builder.Build());

            //others-> birthday fb
            StringBuilder sb = new StringBuilder();

            sb.Append("Birthday:").Append(qc.Birthday.ToString("MM/dd/yyyy")).Append(", Facebook:").Append(qc.Facebook)
            .Append(", Instagram:").Append(qc.Instagram).Append(", Twitter:").Append(qc.Twitter)
            .Append(", Linkedin:").Append(qc.LinkedIn).Append(", Skype:").Append(qc.Skype);
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Note.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Note.InterfaceConsts.Data1, sb.ToString());
            ops.Add(builder.Build());

            //company
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Organization.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Organization.InterfaceConsts.Data, qc.Company);
            ops.Add(builder.Build());

            //url
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Website.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Website.InterfaceConsts.Data, qc.URL);
            ops.Add(builder.Build());

            //homePhone
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, qc.HomePhone);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Home");
            ops.Add(builder.Build());

            //workPhone
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, qc.WorkPhone);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Work");
            ops.Add(builder.Build());

            //homeFax
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, qc.HomeFax);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Home Fax");
            ops.Add(builder.Build());

            //workFax
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, qc.WorkFax);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.StructuredPostal.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Work Fax");
            ops.Add(builder.Build());

            try
            {
                var res = Forms.Context.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
                return(true);
                //Toast.MakeText(Forms.Context, "Contact Saved", ToastLength.Short).Show();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
                //Toast.MakeText(Forms.Context, "Contact Not Saved", ToastLength.Long).Show();
                return(false);
            }
        }