This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
forked from ningsuhen/SwiftAddressBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Tassilo Karge edited this page Jan 25, 2015
·
2 revisions
Here´s how to use SwiftAddressBook. Most things apply to ABAddressBook as well, if you really want to use the unwrapped C-Version that iOS provides.
First, some frequently used actions
- Checking for Authorization:
let status : ABAuthorizationStatus = SwiftAddressBook.authrizationStatus()
- Getting Access:
swiftAddressBook?.requestAccessWithCompletion({ (success, error) -> Void in
if success {
//do something with swiftAddressBook
}
else {
//no success, access denied. Optionally evaluate error
}
})
- Getting all People:
let people : Array<SwiftAddressBookPerson>? = swiftAddressBook?.allPeople
- Getting the underlying ABRecord of any SwiftAddressBookRecord (e.g. a person):
person.internalRecord
- Create new person in standard source:
SwiftAddressBookPerson.create()
- Save modified address book:
swiftAddressBook?.save()
Now, some theorie about the iOS address book...
ABAddressBook is a database with address book data from diverse sources and with diverse types with a c API. All data classes inherit from ABRecord
, and so do the data objects in SwiftAddressBookWrapper: They inherit from SwiftAddressBookRecord
.
SwiftAddressBookRecord` has the subclasses
-
SwiftAddressBookSource
which is about the data sources of ABAddressBook (or SwiftAddressBook, however you like). It could represent a CalDAV Account, Facebook Contacts or the standard local address book source. -
SwiftAddressBookGroup
which represents a collection of people (like "work", "family" etc.) -
SwiftAddressBookPerson
(obviously representing a single person), which is the main class to deal with usually.
SwiftAddressBookPerson has properties, which are mapped from and to the corresponding kABPerson...Property
. They can be of two kinds:
- Standard properties of type String, Int, NSDate etc.
- So-called multivalue properties, which are an array of
MultivalueEntry
s of some specified type for theirvalue
entry. The best example for multivalue properties is the "phone number": For example, one can have multiple mobile phonenumbers. Those are all stored in the array, each with its unique id, value and label. The label for all of those would most probably be "mobile", but can really be anything you want it to be if you set it (including "iPhone" or "bush drum"). There are constants though, and it is a better experience if you stick with them as long as possible. The id stays always the same for the same entry. Thus, you can identify a multivalue entry by the id, even if value and/or label change. Now that´s the most complicated part about address book: setting such a property requires you not just to edit it, but to set it to the person, since Arrays and Structs are value types in swift, thus copied and not only referenced. Setting a multivalue property changes its ids in this implementation, though.