Latest 3.0.9
Homepage https://github.com/satishbabariya/SwiftyContacts
License MIT
Platforms ios 9.0, osx 10.12, watchos 3.0, requires ARC
Authors

Language: Swift 4
Version
License
Platform
Swift Package Manager
Carthage compatible
CocoaPods compatible
RxSwift: Supported
Read the Docs

A Swift library for Contacts framework.

Requirements

  • iOS 9.0+ / Mac OS X 10.12+ / watchOS 3.0+
  • Xcode 9.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate SwiftyContacts into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'SwiftyContacts'

#or

pod 'SwiftyContacts/RxSwift'

For swift 3.x use


// Swift 3.x
pod 'SwiftyContacts' , '~> 2.0.7'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SwiftyContacts into your Xcode project using Carthage, specify it in your Cartfile:

github "SwiftyContacts/SwiftyContacts" ~> 3.0.8

Swift Package Manager

To use SwiftyContacts as a Swift Package Manager package just add the following in your Package.swift file.

import PackageDescription

let package = Package(
    name: "HelloSwiftyContacts",
    dependencies: [
        .Package(url: "https://github.com/satishbabariya/SwiftyContacts", "3.0.8")
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate SwiftyContacts into your project manually.

Embeded Binaries

  • Download the latest release from https://github.com/satishbabariya/SwiftyContacts/releases
  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
  • In the tab bar at the top of that window, open the "General" panel.
  • Click on the + button under the "Embedded Binaries" section.
  • Add the downloaded SwiftyContacts.framework.
  • And that’s it!

Get started

Import SwiftyContacts into your porject

    import SwiftyContacts

For requesting an access for getting contacts.
The user will only be prompted the first time access is requested.

    requestAccess { (responce) in
        if responce{
            print("Contacts Acess Granted")
        } else {
            print("Contacts Acess Denied")
        }
    }

Determine Status of Acess Permission

    authorizationStatus { (status) in
        switch status {
            case .authorized:
                print("authorized")
                break
            case .denied:
                print("denied")
                break
            default:
                break
        }
    }

Fetch Contacts
— Result will be Array of CNContacts

    fetchContacts(completionHandler: { (result) in
        switch result{
            case .Success(response: let contacts):
                // Do your thing here with [CNContacts] array    
                break
            case .Error(error: let error):
                print(error)
            break
        }
    })

Fetch Contacts by Order
— Sorted by CNContactSortOrder
— Result will be Array of CNContacts

    fetchContacts(ContactsSortorder: .givenName) { (result) in
        switch result{
            case .Success(response: let contacts):
                // Do your thing here with [CNContacts] array
                break
            case .Error(error: let error):
                print(error)
                break
        }
    })

Fetch Contacts on Background Thread

    fetchContactsOnBackgroundThread(completionHandler: { (result) in
        switch result{
            case .Success(response: let contacts):
                // Do your thing here with [CNContacts] array    
                break
            case .Error(error: let error):
                print(error)
                break
        }
    })

Search Contact


    searchContact(SearchString: "john") { (result) in
        switch result{
            case .Success(response: let contacts):
                // Contacts Array includes Search Result Contacts
                break
            case .Error(error: let error):
                print(error)
                break
        }
    }

Get CNContact From Identifire


    getContactFromID(Identifire: "XXXXXXXXX", completionHandler: { (result) in  
        switch result{
            case .Success(response: let contact):
                // CNContact
                break
            case .Error(error: let error):
                print(error)
                break
        }
    })

Add Contact


    let contact : CNMutableContact = CNMutableContact()
    contact.givenName = "Satish"
    // OR Use contact.mutableCopy() For Any CNContact

    addContact(Contact: contact) { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Contact Sucessfully Added")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Add Contact in Container


    addContactInContainer(Contact: CNMutableContact, Container_Identifier: String) { (result) in
        //Same As Add Contact
    }

Update Contact


    updateContact(Contact: contact) { (result) in
        switch result{
        case .Success(response: let bool):
            if bool{
                print("Contact Sucessfully Updated")
            }
            break
        case .Error(error: let error):
            print(error.localizedDescription)
            break
        }
    }

Delete Contact

    // Use contact.mutableCopy() To convert CNContact to CNMutableContact
    deleteContact(Contact: contact) { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Contact Sucessfully Deleted")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Fetch List Of Groups

    fetchGroups { (result) in
        switch result{
            case .Success(response: let groups):
                // List Of Groups in groups array
                break
            case .Error(error: let error):
                print(error.localizedDescription)
            break
        }
    }

Create Group

    createGroup(Group_Name: "Satish") { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Group Sucessfully Created")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Create Group in Container

    createGroup(Group_Name: "Satish" , ContainerIdentifire: "ID") { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Group Sucessfully Created")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Update Group

    updateGroup(Group: group, New_Group_Name: "New Name") { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Group Sucessfully Updated")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Remove Group

    removeGroup(Group: group) { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Group Sucessfully Removed")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Fetch Contacts In Group


    fetchContactsInGorup(Group: group) { (result) in
        switch result{
            case .Success(response: let contacts):
                // Do your thing here with [CNContacts] array    
                break
            case .Error(error: let error):
                print(error)
                break
        }
    }

// OR Use

    fetchContactsInGorup2(Group: group) { (result) in
        switch result{
            case .Success(response: let contacts):
                // Do your thing here with [CNContacts] array    
                break
            case .Error(error: let error):
                print(error)
                break
        }
    }

Add Contact To Group

    addContactToGroup(Group: group, Contact: contact) { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Contact Sucessfully Added To Group")         
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Remove Contact From Group

    removeContactFromGroup(Group: group, Contact: contact) { (result) in
        switch result{
            case .Success(response: let bool):
                if bool{
                    print("Contact Sucessfully Added To Group")
                }
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Convert [CNContacts] TO CSV


    contactsToVCardConverter(contacts: ContactsArray) { (result) in
        switch result {
            case .Success(response: data):
                // Use file extension will be .vcf
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break

        }
    }

Convert CSV TO [CNContact]


    VCardToContactConverter(data: data) { (result) in
        switch result{
            case .Success(response: let contacts):
                // Use Contacts array as you like   
                break
            case .Error(error: let error):
                print(error.localizedDescription)
                break
        }
    }

Find Duplicates Contacts


    findDuplicateContacts(Contacts: contactsArray) { (duplicatesContacts) in
        //Duplicates Contacts Array 
        //Array type [Array<CNContact>]
    }

RxSwift

Contacts Authorization


        rx_requestAccess().subscribe { (event) in
            switch event{
            case .next(let element):
                print(element)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Fetch Contacts


        rx_fetchContacts().subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Fetch Contacts By CNContactSortOrder


        rx_fetchContacts(ContactsSortorder: .givenName).subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Search Contacts


        rx_searchContact(SearchString: "Satish").subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Search Contacts with Array of Identifiers


        rx_ContactsFromIDs(SIdentifires: array).subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Contact Operations

Add new Contact.

        rx_addContact(Contact: mutableContact).subscribe { (event) in
            switch event{
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed,.next:
                //Do your thing
                break
            }
        }

Adds the specified contact to the contact store.

        rx_addContactInContainer(Contact: mutableContact, Container_Identifier: idString).subscribe { (event) in
            switch event{
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed,.next:
                //Do your thing
                break
            }
        }

Updates an existing contact in the contact store.

        rx_updateContact(Contact: mutableContact).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Deletes a contact from the contact store.

        rx_deleteContact(Contact: mutableContact).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Groups Methods

fetch list of Groups from the contact store.

        rx_fetchGroups().subscribe { (event) in
            switch event{
            case .next(let groups):
                //Array of groups
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                break
            }
        }

Adds a group to the contact store.

        rx_createGroup(Group_Name: "Work").subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Adds a group to the contact store.

        rx_createGroupInContainer(Group_Name: "Work", ContainerIdentifire: containerID).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Remove an existing group in the contact store.

        rx_removeGroup(Group: group).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Update an existing group in the contact store.

        rx_updateGroup(Group: group).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Adds a contact as a member of a group.

        rx_addContactToGroup(Group: group, Contact: contact).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Remove a contact as a member of a group.

        rx_removeContactFromGroup(Group: group, Contact: contact).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Fetch all contacts in a group.


        rx_fetchContactsInGorup(Group: group).subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Converter Methods

Convert [CNContacts] TO CSV

        rx_contactsToVCardConverter(contacts: arrContacts).subscribe { (event) in
            switch event{
            case .next(let data):
                //Do anything.
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                break
            }
        }

Convert CSV TO [CNContact]


        x_VCardToContactConverter(data: data).subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

Convert Array of CNContacts to Data using NSKeyedArchiver

        rx_archiveContacts(contacts: arrContacts).subscribe { (event) in
            switch event{
            case .next(let data):
                //Do anything.
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                break
            }
        }

Convert Data to Array of CNContacts using NSKeyedArchiver


        rx_unarchiveConverter(data: data).subscribe { (event) in
            switch event{
            case .next(let contacts):
                print(contacts.count)
                break
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed:
                print("completed")
                break
            }
        }

CoreTelephonyCheck

Convert CNPhoneNumber To digits

        rx_CNPhoneNumberToString(CNPhoneNumber: contact).subscribe { event in
            switch event {
            case .error(let error):
                print(error.localizedDescription)
                break
            case .completed, .next:
                // Do your thing
                break
            }
        }

Making a call.

    makeCall(CNPhoneNumber: number)

Author

Satish Babariya, [email protected]

License

SwiftyContacts is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftyContacts",
    "version": "3.0.9",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "summary": "A Swift library for Contacts framework.",
    "homepage": "https://github.com/satishbabariya/SwiftyContacts",
    "authors": {
        "Satish Babariya": "[email protected]"
    },
    "source": {
        "git": "https://github.com/satishbabariya/SwiftyContacts.git",
        "tag": "3.0.9"
    },
    "platforms": {
        "ios": "9.0",
        "osx": "10.12",
        "watchos": "3.0"
    },
    "requires_arc": true,
    "default_subspecs": "Core",
    "pushed_with_swift_version": "4.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Sources/Core/*.swift",
            "frameworks": [
                "Foundation",
                "Contacts"
            ]
        },
        {
            "name": "RxSwift",
            "source_files": "Sources/RxSwift/*.swift",
            "frameworks": [
                "Foundation",
                "Contacts"
            ],
            "dependencies": {
                "RxSwift": [
                    "~> 4.0"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This