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);
|
}
|
|
}
|
}
|