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:

      import AddressBook

Create an object of Address Book:

      var addressBook: ABAddressBookRef?

Create a method for assigning the value to addressBook:

      func createAddressBook(){
          var error: Unmanaged<CFError>?
          addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
      }

Post following code to requesting access:

      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")
      }

Happy Coding 🙂