Git Product home page Git Product logo

kpkcontacts's Introduction

KPKContacts (v1.2.0)

This package was originally created to solve a friend's problem. He found it difficult to access the iOS' contacts telephone numbers.

Here's what you can do with this package:

  • You can retrieve all contacts from an iOS device which contain valid telephone numbers - a default regex function is used if one is not provided
  • you can specify your own phone number validator block.
  • A custom KPKContact object is returned for every contact found which contain both the original CNContact information should you require other details, as well as properties that are exposed for convenience.

Demo

A demo is available which shows a table view contacts viewer displaying all your iOS device's contacts with their phone numbers with writing a few lines of code.

Installation

There isn't a cocoapod for this package as of today, didn't have the time to do so just yet. For now, simply dragging the two source files into your project is all that's required.

How to use

This is the store where you call your methods on

    var kpkContactStore = KPKContactStore()

Define and instantiate an empty array to store found contacts in

    var contacts = [KPKContact]()

If you want to detect if the user has disabled your app access to their Contacts you will have to make your class the delegate

    kpkContactStore.delegate = self

You can then adopt the protocol method that will get called when you initiate a contact search

extension YourClass: KPKContactStoreDelegate {
    func kpkContactStore(contactStore: KPKContactStore, contactsAccessAuthorizationStatus status: KPKContactAuthorizationStatus) {
        switch status {
        case .Denied, .NotDetermined:
            // You can display an alert to the user which can deep link on your app in the device's settings app.
        //case other options are available:
        default:
            return
        }
    }
}

Here's how you make a call for contacts retrieval. This will use a default regex phone number vaidator block

    self.kpkContactStore.findContactsWithValidNumbersOnly(){
        kpkContacts in
        // set local array to kpkContacts
}

You can then process a the contact search. This is done in the background with the completion handler invoked on the main queue. Here's an example of a tableView being updated with the contacts retrieved

    self.kpkContactStore.findContactsWithValidNumbersOnly(){
        kpkContacts in
        if let contacts = kpkContacts {
            self.contacts = contacts
            self.tableView.reloadData()
        }
    }

Here's how you can set a custom regex phone number validator block, you do so by using the helper function:

private var regexPhoneNumberValidatorBlock: String -> Bool = { value in
    let PHONE_REGEX = "^\\s*(?:\\+?(\\d{1,3}))?[-. (]*(\\d{3})[-. )]*(\\d{3})[-. ]*(\\d{4})(?: *x(\\d+))?\\s*$"
        let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
        let result =  phoneTest.evaluateWithObject(value)
        return result
    }
    // And then you'd make the call to the findContactsWithValidNumbersOnly method
    contacts = self.kpkContactStore.findContactsWithValidNumbersOnly()

Here's how you access the 4 properties (so far, more to come when there's a demand for them) for the contact objects retrieved:

    // You can access your contacts array to grab a contact object - KPKContact - by specifying some sort of index.
    let contact: KPKContact = contacts[indexPath.row]

The KPKContact object contains 4 properties:

    // The original contact information should you need something extra
    let originalContact: CNContact = contact.originalCNContact
    
    // First name
    let firstName: String = contact.firstName
    
    // Last name
    let lastName: String = contact.lastName
    
    // And a numbers array which stores all the valid numbers associated to the contact
    let numbers: [KPKContactNumberInformation] = contact.numbers

Within the numbers array you'll have KPKContactNumberInformation objects. They contain 3 properties

    // An identifier which uniquely identifies the number even after install
    let identifier: String
    
    // The display name used for the number: Mobile, Home, etc
    let displayType: String
    
    // The telephone number in string format
    let number: String

There is a helper method which allows you to access the first number for a contact:

    contact.firstNumberAvailable()
    // Rather than doing 
    // contact.numbers.first?.number 

To-dos

  • Simplify internal methods by using .map, and .filter methods
  • Add more helper functions to the KPKContacts class
  • Make the search look at the keys available.
  • Write Tests

Development

Want to contribute? Great!

KPKContacts uses git for fast developing.

  • Fork this repo
  • Make a change
  • And then make a pull request.

License

MIT

Free Software, Hell Yeah!

kpkcontacts's People

Contributors

pavankataria avatar

Stargazers

Tiago Braga avatar Jazbo avatar MBO avatar  avatar

Watchers

James Cloos avatar  avatar MBO avatar

Forkers

zenangst

kpkcontacts's Issues

If user deny's access to contacts

Hey, thanks for the resource, really made life easier dealing with the iOS contacts framework :)

My issue is that the user is prompted to grant access to their contact's only once.
IF user later revokes access
THEN this resource has no method to check and alert them to grant access to their device's contacts
IF user does not grant access (I haven't tested this scenario) i'm assuming it won't alert them again next time they load the view attempting to access their contacts.

What I end up with is an empty tableview like below.
img_3121

Find contacts method crashes

This crashes with libc++abi.dylib: terminating with uncaught exception of type NSException

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            if let contacts = self.kpkContactStore.findContactsWithValidNumbersOnly() {
                self.contacts = contacts
                dispatch_async(dispatch_get_main_queue()) {
                    self.tableView.reloadData()
                }
            }
        }

It crashes when I run it in view did Load. FYI the view controller I load it in is for an xib.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.