Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Return email addresses on iOS #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

l3ender
Copy link

@l3ender l3ender commented Mar 5, 2020

Platforms affected

  • iOS

What does this PR do?

This PR returns email addresses for iOS contacts.

What testing has been done on this change?

We use the find method like so (transposed slightly from Ionic/Typescript)

let fields: ContactFieldType[] = ["phoneNumbers", "emails"];
let options: IContactFindOptions = {
	filter: "",
	multiple: true,
};
let results = contacts.find(fields, options);

Resolves #3.

(cherry picked from commit 9200220)
@l3ender
Copy link
Author

l3ender commented Mar 19, 2020

Hello, any feedback on this? :)

@asolntsev

@asolntsev
Copy link
Member

@l3ender Thank you for the contribution. Currently we don't actively maintain this plugin.
Probably we will review pull requests when we get back from quarantine.

@katamshut
Copy link

Sorry I can't commit but here the fix for retreiving emails on iOS:

change the static variable allContactKeys in class CDVContacts.swift (line 45) to:

static let allContactKeys: [CNKeyDescriptor] = [CNContactIdentifierKey as CNKeyDescriptor,
                                                    CNContactNicknameKey as CNKeyDescriptor,
                                                    CNContactGivenNameKey as CNKeyDescriptor,
                                                    CNContactFamilyNameKey as CNKeyDescriptor,
                                                    CNContactMiddleNameKey as CNKeyDescriptor,
                                                    CNContactNamePrefixKey as CNKeyDescriptor,
                                                    CNContactNameSuffixKey as CNKeyDescriptor,
                                                    CNContactPhoneNumbersKey as CNKeyDescriptor,
                                                    CNContactTypeKey as CNKeyDescriptor,
                                                    CNContactEmailAddressesKey as CNKeyDescriptor]

change the function toDictionary in class CDVContact.swift (line 957) to:

func toDictionary(_ withFields: [AnyHashable: Any]) -> [AnyHashable: Any] {
        // if not a person type record bail out for now
        if nonMutableRecord?.contactType != CNContactType.person {
            return [NSNull() : NSNull()]
        }
        var value: Any? = nil
        returnFields = withFields
        var nc = [AnyHashable: Any](minimumCapacity: 1) // new contact dictionary to fill in from ABRecordRef
        // id
        nc[kW3ContactId] = nonMutableRecord?.identifier
        if let fields = returnFields {
            if fields[kW3ContactDisplayName] != nil {
                // displayname requested -  iOS doesn't have so return null
                nc[kW3ContactDisplayName] = NSNull()
                // may overwrite below if requested ContactName and there are no values
            }

            // nickname
            if fields[kW3ContactNickname] != nil {
                value = nonMutableRecord?.nickname
                nc[kW3ContactNickname] = (value != nil) ? value : NSNull()
            }

            // name dictionary
            // NSLog(@"getting name info");
            let data: NSObject? = extractName()
            if data != nil {
                nc[kW3ContactName] = data
            }
            if fields[kW3ContactDisplayName] != nil {
                if ((data == nil) || ((data as? [AnyHashable: Any])?[kW3ContactFormattedName] == nil)) {
                    // user asked for displayName which iOS doesn't support but there is no other name data being returned
                    // try and use Nickname so some name is returned
                    value = nonMutableRecord?.nickname
                    nc[kW3ContactDisplayName] = (value != nil) ? value : ""
                }
            }

            // phoneNumbers array
            // NSLog(@"getting phoneNumbers");
            value = extractMultiValue(kW3ContactPhoneNumbers)
            if value != nil {
                nc[kW3ContactPhoneNumbers] = value
            }

            // emails array
            // NSLog(@"getting emails");
            value = extractMultiValue(kW3ContactEmails)
            if value != nil {
                nc[kW3ContactEmails] = value
            }
            return nc
        } else {
            // if no returnFields specified, W3C says to return empty contact (but Cordova will at least return id)
            return nc
        }
    }

That way the find function will return emails as in the original plugin. Did only test this for find function. You may add this to this repo after more testing on other functions.

Thanks for createing this repo!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Email addresses not returned on iOS
3 participants