Requesting Access to the Address Book – Swift Language – iOS 8

Request to Access Address Book
Request to Access Address Book

Here is simple steps to requesting access to the Address Book in Swift Language iOS 8
Import the framework of Address Book:
[code language=”objc”]
import AddressBook
[/code]
Create an object of Address Book:
[code language=”objc”]
var addressBook: ABAddressBookRef?
[/code]
Create a method for assigning the value to addressBook:
[code language=”objc”]
func createAddressBook(){
var error: Unmanaged<CFError>?
addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
}
[/code]
Post following code to requesting access:
[code language=”objc”]
switch ABAddressBookGetAuthorizationStatus(){
case .Authorized:
println("Already authorized")
createAddressBook()
/* Access the address book */
case .Denied:
println("Denied access to address book")
case .NotDetermined:
createAddressBook()
if let theBook: ABAddressBookRef = addressBook{
ABAddressBookRequestAccessWithCompletion(theBook,
{(granted: Bool, error: CFError!) in
if granted{
println("Access granted")
} else {
println("Access not granted")
}
})
}
case .Restricted:
println("Access restricted")
default:
println("Other Problem")
}
[/code]
Happy Coding 🙂