To run the example project, clone the repo, and run pod install
from the Example directory first.
import AddressBookManager
...
var abManager: AddressBookManager? = AddressBookManager()
switch (AddressBookManager.getAuthorizationStatus()) {
case .Authorized:
// Authorized To Use AddressBook
case .Denied:
// Denied Access To AddressBook
case .Restricted:
// Restricted Access To AddressBook
case .Unknown:
// Access To AddressBook Unknown, Most Likely AddressBook Authorization Has Not Been Requested Yet
}
// Option 1
abManager?.requestAuthorizationWithCompletion({ (granted: Bool, error: CFError?) -> Void in
if (error) {
// Handle Error
} else if (granted) {
var people = abManager?.allContacts
// Do Something With Contacts
}
})
// Option 2 - If you want to use a specific queue for retrieval
abManager?.retrieveAllContactsInQueue(dispatch_get_main_queue(),
completion: { (contacts: [AddressBookPerson]?, error: CFError?) -> Void in
if (error) {
// Handle Error
} else {
// Do Something With Contacts
}
})
// Sort Contacts In Ascending Order By First Name
abManager?.retrieveAllContactsInQueue(dispatch_get_main_queue(),
sort: { $0.firstName < $1.firstName },
completion: { (contacts: [AddressBookPerson]?, error: CFError?) -> Void in
if (error) {
// Handle Error
} else {
// Do Something With Contacts
}
})
// Retrieve Contacts That Have Atleast One Email
abManager?.retrieveAllContactsInQueue(dispatch_get_main_queue(),
filter: { count($0.emails!) > 0 },
completion: { (contacts: [AddressBookPerson]?, error: CFError?) -> Void in
if (error) {
// Handle Error
} else {
// Do Something With Contacts
}
})
// Retrieve Only Contacts That Have Atleast One Email And Sort Those Contacts In Ascending Order By First Name
abManager?.retrieveAllContactsInQueue(dispatch_get_main_queue(),
sort: { $0.firstName < $1.firstName },
filter: { count($0.emails!) > 0 },
completion: { (contacts: [AddressBookPerson]?, error: CFError?) -> Void in
if (error) {
// Handle Error
} else {
// Do Something With Contacts
}
})
abm.requestAuthorizationWithCompletion { (granted: Bool, error: CFError?) -> Void in
var person = AddressBookPerson()
person.firstName = "Bob"
person.lastName = "Smith"
var personalEmail = MultiValue(key: "personal", value: "bob@mail.com")
person.emails = [personalEmail]
var homePhoneNumber = MultiValue(key: "home", value: "5555555555")
var mobilePhoneNumber = MultiValue(key: "mobile", value: "1234567890")
person.phoneNumbers = [homePhoneNumber, mobilePhoneNumber]
person.profilePicture = UIImage(named: "bob.png")
var homeAddress = Dictionary<AddressProperty, AnyObject>()
homeAddress[AddressProperty.Street] = "123 Maple Street"
homeAddress[AddressProperty.City] = "Miami"
homeAddress[AddressProperty.State] = "FL"
homeAddress[AddressProperty.ZipCode] = "00000"
homeAddress[AddressProperty.Country] = "USA"
var homeAddressValue = MultiValue(key: "home", value: homeAddress)
person.addresses = [homeAddressValue]
var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let birthday: NSDate? = dateFormatter.dateFromString("01/01/1970")
person.birthday = birthday
person.organization = "Some Huge Organization"
person.jobTitle = "Developer"
person.department = "Software"
person.note = "Some Note Here"
var personalURL = MultiValue(key: "personal", value: "https://somewebsite")
person.urls = [personalURL]
person.prefix = "Mr"
person.suffix = "Jr"
person.middleName = "Roger"
var anniversaryDate = MultiValue(key: "anniversary", value: dateFormatter.dateFromString("02/02/2000"))
person.dates = [anniversaryDate]
abm?.addRecord(person)
abm?.save()
}
- personCount - returns number of contacts in the address book
- groupCount - returns number of groups in the address book
- removeRecord - removes the record (AddressBookRecord) from the address book
- hasUnsavedChanges - if there were any changes to the address book (additions, removal, etc...) that have not yet been saved
AddressBookManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "AddressBookManager"
OR
You can simply clone this repository and drag the files into your project.
Aadesh Patel, aadeshp95@gmail.com
AddressBookManager is available under the MIT license. See the LICENSE file for more info.