wxr
2020-01-10 1a4b95a7ebef71838bd3eda2c22056bbf0db65ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using AddressBook;
using AddressBookUI;
 
namespace Shared
{
    public static class Contacts
    {
        public static Action<string, string> ContactAction;
        public static void Open()
        {
            switch (ABAddressBook.GetAuthorizationStatus())
            {
                case ABAuthorizationStatus.NotDetermined:
                    Foundation.NSError s=null;
                    ABAddressBook.Create(out s).RequestAccess((b, ss) => {
                        Shared.Application.RunOnMainThread(() =>
                        {
                            if (b)
                            {
                                var picker = new ABPeoplePickerNavigationController();
                                picker.Cancelled += Picker_Cancelled;
                                picker.SelectPerson2 += Picker_SelectPerson2;
                                BaseViewController.Instance.PresentViewController(picker, true, null);
                            }
                            else {
                                ContactAction?.Invoke(null, null);
                            }
                        });
                    });
                    break;
                case ABAuthorizationStatus.Authorized:
                    {
                        var picker = new ABPeoplePickerNavigationController();
                        picker.Cancelled += Picker_Cancelled;
                        picker.SelectPerson2 += Picker_SelectPerson2;
                        BaseViewController.Instance.PresentViewController(picker, true, null);
                    }
                    break;
                default:
                    ContactAction?.Invoke(null, null);
                    break;
            }
        }
 
        private static void Picker_SelectPerson2(object sender, ABPeoplePickerSelectPerson2EventArgs e)
        {
            var person=  e.Person;
            string phone = "";
            foreach(var v in person.GetPhones()) {
                if (!string.IsNullOrEmpty(v.Value))
                {
                    phone = v.Value.Replace("-", " ").Replace(" ", "");
                    break;
                } 
            }
            ContactAction?.Invoke((person.LastName + " " + person.FirstName).Trim(' '), phone);
        }
 
        private static void Picker_Cancelled(object sender, EventArgs e)
        {
            ContactAction?.Invoke(null, null);
        }
 
    }
}