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 🙂

  • Matthew Raby

    Can you access an image assigned to a contact? The above solution is similar to most that I have found for Swift and works great. So far the only values I have been able to retrieve from the address book is the name and phone number 🙁

    • With the use of function :

      func imageForPerson(person: ABRecordRef) -> UIImage?{
          let data = ABPersonCopyImageData(person).takeRetainedValue() as NSData
          let image = UIImage(data: data)
          return image
      }
      
  • Matthew Raby

    Thank you for the response, that was exactly what I was looking for!

  • Purpa

    Is there a way to reset the permissions using code? Let’s say a user makes a mistake and denies access to the AB. How do you allow the user to reset it from inside the app? I tried using ABAddressBookRequestAccessWithCompletion again but it does not seem to work. Thanks.

    • The application will ask for permission at one time If user denies then they has to give permission from the settings page.

      • Purpa

        That’s what I was afraid of. Thanks for such a quick reply.

  • Nicolas Zein

    Hello Ashish thanks for this great tutorial.

    Please i need help. I want to be able to see all contacts listed and retrieve one of the several numbers listed to pull the name and phone number. Do you have that tutorial in swift?! Thanks.

    • I will let you know. If I got any tutorial.

      Thanks for the complement.

  • Gaurav singh tomar

    first of all thanks for such a simple tutorial. It is really very helpful. i want to have list of all contacts and will be able to pick a contact. It will be great if you help me out.

    • You have to learn your self about it. Like as I am doing self learning.

      Thanks for complement.