Skip to content
86 changes: 12 additions & 74 deletions src/AddressBook/ABAddressBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,77 +102,21 @@ public ExternalChangeEventArgs (ABAddressBook addressBook, NSDictionary? info)
public NSDictionary? Info { get; private set; }
}

// Quoth the docs:
// http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABPersonGetSortOrdering
//
// "The value of these constants is undefined until one of the following has
// been called: ABAddressBookCreate, ABPersonCreate, ABGroupCreate."
//
// Meaning we can't rely on static constructors, as they could be invoked
// before those functions have been invoked. :-/
//
// Note that the above comment was removed from iOS 6.0+ documentation (and were not part of OSX docs AFAIK).
// It make sense since it's not possible to call those functions, from 6.0+ they will return NULL on devices,
// unless the application has been authorized to access the address book.
[SupportedOSPlatform ("ios")]
/// <summary>Provides access to the legacy Address Book database.</summary>
[ObsoletedOSPlatform ("ios", "Use the 'Contacts' API instead.")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("maccatalyst", "Use the 'Contacts' API instead.")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
static class InitConstants {
public static void Init () { }

static InitConstants ()
{
#if __MACCATALYST__
// avoid TypeLoadException if used before macOS 11.x
if (!SystemVersion.CheckiOS (14, 0))
return;
public partial class ABAddressBook : NativeObject, IEnumerable<ABRecord> {

#if !XAMCORE_5_0
/// <summary>Identifies the error domain under which Address Book errors are grouped.</summary>
/// <remarks>
/// <para>
/// When an <see cref="CoreFoundation.CFException" /> is thrown from an <see cref="ABAddressBook" /> method,
/// its <see cref="CoreFoundation.CFException.Domain" /> is equal to this value.
/// </para>
/// </remarks>
public static readonly NSString ErrorDomain = _ErrorDomain;
#endif
// ensure we can init. This is needed before iOS6 (as per doc).
IntPtr p = ABAddressBook.ABAddressBookCreate ();

ABGroupProperty.Init ();
ABLabel.Init ();
ABPersonAddressKey.Init ();
ABPersonDateLabel.Init ();
ABPersonInstantMessageKey.Init ();
ABPersonInstantMessageService.Init ();
ABPersonKindId.Init ();
ABPersonPhoneLabel.Init ();
ABPersonPropertyId.Init ();
ABPersonRelatedNamesLabel.Init ();
ABPersonUrlLabel.Init ();
ABSourcePropertyId.Init ();

// From iOS 6.0+ this might return NULL, e.g. if the application is not authorized to access the
// address book, and we would crash if we tried to release a null pointer
if (p != IntPtr.Zero)
CFObject.CFRelease (p);
}
}

/// <include file="../../docs/api/AddressBook/ABAddressBook.xml" path="/Documentation/Docs[@DocId='T:AddressBook.ABAddressBook']/*" />
[SupportedOSPlatform ("ios")]
[ObsoletedOSPlatform ("ios", "Use the 'Contacts' API instead.")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("maccatalyst", "Use the 'Contacts' API instead.")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
public class ABAddressBook : NativeObject, IEnumerable<ABRecord> {

/// <summary>
/// Identifies the error domain under which address book errors are grouped.
/// </summary>
/// <remarks>
/// When an <see cref="CoreFoundation.CFException" /> is
/// thrown from a <see cref="AddressBook.ABAddressBook" />
/// method, the
/// <see cref="CoreFoundation.CFException.Domain" /> property
/// will be equal to <c>ErrorDomain</c>.
/// </remarks>
public static readonly NSString ErrorDomain;

GCHandle sender;

Expand Down Expand Up @@ -225,12 +169,6 @@ public ABAddressBook ()
internal ABAddressBook (NativeHandle handle, bool owns)
: base (handle, owns)
{
InitConstants.Init ();
}

static ABAddressBook ()
{
ErrorDomain = Dlfcn.GetStringConstant (Libraries.AddressBook.Handle, "ABAddressBookErrorDomain")!;
}

/// <inheritdoc />
Expand Down
22 changes: 0 additions & 22 deletions src/AddressBook/ABGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,6 @@
using CoreFoundation;

namespace AddressBook {
[SupportedOSPlatform ("ios")]
[ObsoletedOSPlatform ("ios", "Use the 'Contacts' API instead.")]
[SupportedOSPlatform ("maccatalyst")]
[ObsoletedOSPlatform ("maccatalyst", "Use the 'Contacts' API instead.")]
[UnsupportedOSPlatform ("macos")]
[UnsupportedOSPlatform ("tvos")]
static class ABGroupProperty {

public static int Name { get; private set; }

static ABGroupProperty ()
{
InitConstants.Init ();
}

internal static void Init ()
{
Name = Dlfcn.GetInt32 (Libraries.AddressBook.Handle, "kABGroupNameProperty");
}
}

/// <summary>
/// A grouping of <see cref="AddressBook.ABPerson" /> and
/// other <see cref="AddressBook.ABGroup" /> records.
Expand Down Expand Up @@ -104,7 +83,6 @@ public class ABGroup : ABRecord, IEnumerable<ABRecord> {
public ABGroup ()
: base (ABGroupCreate (), true)
{
InitConstants.Init ();
}

[DllImport (Constants.AddressBookLibrary)]
Expand Down
Loading