// The one source that names the Contacts API. Left out of an app whose plist says nothing about contacts// (`COTTON_NO_CONTACTS`, set by ios-spec-gate for Words, Days and Story): App Store processing refuses a binary that// references Contacts without NSContactsUsageDescription (ITMS-90683, Words build 21, 22 Nov 2026).#if !COTTON_NO_CONTACTSimportContactsimportCottonKitimportFoundation/// The person's own address book — the ONE place a `CNContactStore` is built,/// in each app target and nowhere in the package, so a test bundle (which/// links the package, never an app) cannot reach it. Installed at launch into/// `LiveStores`; `LiveStoresSweepTests` holds the rule.structSystemAddressBook: AddressBook {/// **`limited` is a YES for contacts.** The person chose which cards Cotton may see, and the honest thing is to/// show exactly those rather than to argue with the choice (`PeopleImportSheet`'s own rule).func access() -> CapabilityAccess {switch CNContactStore.authorizationStatus(for: .contacts) {case .authorized, .limited: return .authorizedcase .notDetermined: return .notDeterminedcase .denied: return .deniedcase .restricted: return .restricted @unknown default: return .denied } }func askAccess() async -> Bool { (try? await CNContactStore().requestAccess(for: .contacts)) ?? false }func enumerate(_ depth: AddressCardDepth, _ each: (AddressCard) -> Void) throws {var keys: [any CNKeyDescriptor] = [CNContactIdentifierKey, CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] as [any CNKeyDescriptor]if depth == .whole { keys += [ CNContactOrganizationNameKey, CNContactJobTitleKey, CNContactEmailAddressesKey, CNContactPostalAddressesKey, CNContactBirthdayKey, ] as [any CNKeyDescriptor]// The platform's own composed name needs whatever the formatter decides it needs — prefix, middle name,// suffix, nickname, and the ordering rules for the device's locale. keys.append(CNContactFormatter.descriptorForRequiredKeys(for: .fullName)) }let request = CNContactFetchRequest(keysToFetch: keys) request.sortOrder = .givenNametry CNContactStore().enumerateContacts(with: request) { contact, _ inguard depth == .whole else { each(AddressCard(id: contact.identifier, givenName: contact.givenName, familyName: contact.familyName, phones: contact.phoneNumbers.map(\.value.stringValue)))return } each(AddressCard( id: contact.identifier, composedName: CNContactFormatter.string(from: contact, style: .fullName), givenName: contact.givenName, familyName: contact.familyName, organization: contact.organizationName, jobTitle: contact.jobTitle, phones: contact.phoneNumbers.map(\.value.stringValue), emails: contact.emailAddresses.map { $0.value asString }, cities: contact.postalAddresses.map(\.value.city), birthday: contact.birthday)) } }/// The FULL image, falling back to the thumbnail: a portrait is stored at 1024 points and the card's thumbnail/// is often smaller than that.func picture(_ identifier: String) throws -> Data? {let keys = [CNContactImageDataKey, CNContactThumbnailImageDataKey] as [any CNKeyDescriptor]let card = try CNContactStore().unifiedContact(withIdentifier: identifier, keysToFetch: keys)return card.imageData ?? card.thumbnailImageData }}#endif