Ejemplo n.º 1
0
        static Task <IEnumerable <string> > GetAddressesForPositionAsync(Position position)
        {
            var location = new CLLocation(position.Latitude, position.Longitude);
            var geocoder = new CCLGeocoder();
            var source   = new TaskCompletionSource <IEnumerable <string> >();

            geocoder.ReverseGeocodeLocation(location, (placemarks, error) =>
            {
                if (placemarks == null)
                {
                    placemarks = new CLPlacemark[0];
                }
                List <string> addresses = new List <string>();
#if __MOBILE__
                addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)).ToList();
#else
                foreach (var item in placemarks)
                {
                    var address        = new CNMutablePostalAddress();
                    address.Street     = item.AddressDictionary["Street"] == null ? "" : item.AddressDictionary["Street"].ToString();
                    address.State      = item.AddressDictionary["State"] == null ? "" : item.AddressDictionary["State"].ToString();
                    address.City       = item.AddressDictionary["City"] == null ? "" : item.AddressDictionary["City"].ToString();
                    address.Country    = item.AddressDictionary["Country"] == null ? "" : item.AddressDictionary["Country"].ToString();
                    address.PostalCode = item.AddressDictionary["ZIP"] == null ? "" : item.AddressDictionary["ZIP"].ToString();
                    addresses.Add(CNPostalAddressFormatter.GetStringFrom(address, CNPostalAddressFormatterStyle.MailingAddress));
                }
#endif
                source.SetResult(addresses);
            });
            return(source.Task);
        }
Ejemplo n.º 2
0
        static Task <IEnumerable <string> > GetAddressesForPositionAsync(Position position)
        {
            var location = new CLLocation(position.Latitude, position.Longitude);
            var geocoder = new CCLGeocoder();
            var source   = new TaskCompletionSource <IEnumerable <string> >();

            geocoder.ReverseGeocodeLocation(location, (placemarks, error) =>
            {
                if (placemarks == null)
                {
                    placemarks = new CLPlacemark[0];
                }
                List <string> addresses = new List <string>();
#if __MOBILE__ && !(MACCATALYST || MACOS || __MACCATALYST__)
#pragma warning disable BI1234, CA1416 // Type or member is obsolete, ABAddressFormatting.ToString(...) has [UnsupportedOSPlatform("ios9.0")]
                addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)).ToList();
#pragma warning restore BI1234, CA1416 // Type or member is obsolete
#else
                foreach (var item in placemarks)
                {
                    var address = new CNMutablePostalAddress();
#pragma warning disable CA1416 // TODO: 'CLPlacemark.AddressDictionary' is unsupported on: 'maccatalyst' 11.0 and later
                    address.Street     = item.AddressDictionary["Street"] == null ? "" : item.AddressDictionary["Street"].ToString();
                    address.State      = item.AddressDictionary["State"] == null ? "" : item.AddressDictionary["State"].ToString();
                    address.City       = item.AddressDictionary["City"] == null ? "" : item.AddressDictionary["City"].ToString();
                    address.Country    = item.AddressDictionary["Country"] == null ? "" : item.AddressDictionary["Country"].ToString();
                    address.PostalCode = item.AddressDictionary["ZIP"] == null ? "" : item.AddressDictionary["ZIP"].ToString();
#pragma warning restore CA1416
                    addresses.Add(CNPostalAddressFormatter.GetStringFrom(address, CNPostalAddressFormatterStyle.MailingAddress));
                }
#endif
                source.SetResult(addresses);
            });
            return(source.Task);
        }
        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            viewIsDisplayed = true;
            var region = LocationTriggerCreator.TargetRegion;

            if (region != null)
            {
                var centerLocation = new CLLocation(region.Center.Latitude, region.Center.Longitude);
                geocoder.ReverseGeocodeLocation(centerLocation, (placemarks, error) => {
                    // The geocoder took too long, we're not on this view any more.
                    if (!viewIsDisplayed)
                    {
                        return;
                    }

                    if (error != null)
                    {
                        DisplayError(error);
                        return;
                    }

                    if (placemarks != null)
                    {
                        var mostLikelyPlacemark = placemarks.FirstOrDefault();
                        if (mostLikelyPlacemark != null)
                        {
                            CNMutablePostalAddress address = CreatePostalAddress(mostLikelyPlacemark);
                            var addressFormatter           = new CNPostalAddressFormatter();
                            string addressString           = addressFormatter.GetStringFromPostalAddress(address);
                            localizedAddress = addressString.Replace("\n", ", ");
                            var section      = NSIndexSet.FromIndex(2);
                            TableView.ReloadSections(section, UITableViewRowAnimation.Automatic);
                        }
                    }
                });
            }
            TableView.ReloadData();
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
        private void PerformContactAction(UITableViewCell selectedCell)
        {
            if (selectedCell == createNewContactCell)
            {
                var contactViewController = CNContactViewController.FromNewContact(null);
                contactViewController.Delegate = this;
                base.NavigationController.PushViewController(contactViewController, true);
            }

            else if (selectedCell == createNewContactExistingDataCell)
            {
                var contact = new CNMutableContact
                {
                    FamilyName = Name.Family,
                    GivenName  = Name.Given,
                };

                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[]
                {
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.IPhone, new CNPhoneNumber(PhoneNumber.IPhone)),
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.Mobile, new CNPhoneNumber(PhoneNumber.Mobile))
                };

                var homeAddress = new CNMutablePostalAddress
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode
                };
                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                //Erstelle einen Kontakt-View-Controller mit unserem Kontakt
                var contactViewController = CNContactViewController.FromNewContact(contact);
                contactViewController.Delegate = this;
                base.NavigationController.PushViewController(contactViewController, true);
            }

            else if (selectedCell == editContactCell)
            {
                var contact = new CNMutableContact();

                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[] { new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber(PhoneNumber.Mobile)) };
                var homeAddress = new CNMutablePostalAddress()
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode
                };
                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                var contactViewController = CNContactViewController.FromUnknownContact(contact);
                contactViewController.AllowsEditing = true;
                contactViewController.ContactStore  = new CNContactStore();
                contactViewController.Delegate      = this;

                base.NavigationController.PushViewController(contactViewController, true);
            }

            else if (selectedCell == displayEditCell)
            {
                var name = $"{Name.Given} {Name.Family}";
                FetchContact(name, (contacts) =>
                {
                    if (contacts.Any())
                    {
                        var contactViewController           = CNContactViewController.FromContact(contacts[0]);
                        contactViewController.AllowsEditing = true;
                        contactViewController.AllowsActions = true;
                        contactViewController.Delegate      = this;

                        var highlightedPropertyIdentifiers = contacts[0].PhoneNumbers.FirstOrDefault()?.Identifier;
                        if (!string.IsNullOrEmpty(highlightedPropertyIdentifiers))
                        {
                            contactViewController.HighlightProperty(new NSString("phoneNumbers"), highlightedPropertyIdentifiers);
                        }
                        else
                        {
                            this.ShowAlert($"Could not find {name} in Contacts.");
                        }
                    }
                });
            }
        }
Ejemplo n.º 6
0
        private void PerformContactAction(UITableViewCell selectedCell)
        {
            if (selectedCell == this.createNewContactCell)
            {
                // Create an empty contact view controller.
                var contactViewController = CNContactViewController.FromNewContact(null);
                // Set its delegate.
                contactViewController.Delegate = this;
                // Push it using the navigation controller.
                base.NavigationController.PushViewController(contactViewController, true);
            }
            // Called when users tap "Create New Contact With Existing Data" in the UI.
            // Create and launch a contacts view controller with pre - filled fields.
            else if (selectedCell == this.createNewContactExistingData)
            {
                var contact = new CNMutableContact
                {
                    // Given and family names.
                    FamilyName = Name.Family,
                    GivenName  = Name.Given,
                };

                // Phone numbers.
                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[]
                {
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.IPhone,
                                                       new CNPhoneNumber(PhoneNumber.IPhone)),
                    new CNLabeledValue <CNPhoneNumber>(PhoneNumber.Mobile,
                                                       new CNPhoneNumber(PhoneNumber.Mobile))
                };

                // Postal address.
                var homeAddress = new CNMutablePostalAddress
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode,
                };

                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                // Create a contact view controller with the above contact.
                var contactViewController = CNContactViewController.FromNewContact(contact);
                // Set its delegate.
                contactViewController.Delegate = this;
                // Push it using the navigation controller.
                base.NavigationController.PushViewController(contactViewController, true);
            }
            // Called when users tap "Edit Unknown Contact" in the UI.
            // The view controller displays some contact information that you can either add to an existing contact or use them to create a new contact.
            else if (selectedCell == this.editContactCell)
            {
                var contact = new CNMutableContact();

                // Phone number.
                contact.PhoneNumbers = new CNLabeledValue <CNPhoneNumber>[] { new CNLabeledValue <CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber(PhoneNumber.Mobile)) };

                // Postal address.
                var homeAddress = new CNMutablePostalAddress()
                {
                    Street     = Address.Street,
                    City       = Address.City,
                    State      = Address.State,
                    PostalCode = Address.PostalCode,
                };

                contact.PostalAddresses = new CNLabeledValue <CNPostalAddress>[] { new CNLabeledValue <CNPostalAddress>(CNLabelKey.Home, homeAddress) };

                // Create a view controller that allows editing.
                var contactViewController = CNContactViewController.FromUnknownContact(contact);
                contactViewController.AllowsEditing = true;
                contactViewController.ContactStore  = new CNContactStore();
                contactViewController.Delegate      = this;

                // Push the unknown contact in the view controler.
                base.NavigationController.PushViewController(contactViewController, true);
            }
            // Called when users tap "Display and Edit Contact" in the UI.
            // Searches for the contact specified whose last name and first name are respectively specified by contact.family and contact.given
            else if (selectedCell == this.displayEditCell)
            {
                var name = $"{Name.Given} {Name.Family}";
                this.FetchContact(name, (contacts) =>
                {
                    if (contacts.Any())
                    {
                        var contactViewController           = CNContactViewController.FromContact(contacts[0]);
                        contactViewController.AllowsEditing = true;
                        contactViewController.AllowsActions = true;
                        contactViewController.Delegate      = this;

                        /*
                         *  Set the view controller's highlightProperty if
                         *  highlightedPropertyIdentifier exists. Thus, ensuring
                         *  that the contact's phone number specified by
                         *  highlightedPropertyIdentifier will be highlighted in the
                         *  UI.
                         */

                        var highlightedPropertyIdentifiers = contacts[0].PhoneNumbers.FirstOrDefault()?.Identifier;
                        if (!string.IsNullOrEmpty(highlightedPropertyIdentifiers))
                        {
                            contactViewController.HighlightProperty(new NSString("phoneNumbers"), highlightedPropertyIdentifiers);
                        }

                        // Show the view controller.
                        base.NavigationController.PushViewController(contactViewController, true);
                    }
                    else
                    {
                        this.ShowAlert($"Could not find {name} in Contacts.");
                    }
                });
            }
        }