Cotton code
70 lines · 3.7 KB · SwiftRaw
// 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_CONTACTSimport Contactsimport CottonKitimport Foundation/// 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.struct SystemAddressBook: 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 .authorized        case .notDetermined: return .notDetermined        case .denied: return .denied        case .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 = .givenName        try CNContactStore().enumerateContacts(with: request) { contact, _ in            guard 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 as String },                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