Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Managing the address book


In this recipe, we will discuss how to access and manage the user's stored contacts in the device's address book.

Getting ready

Create a new Single View Application in Xamarin Studio and name it AddressBookApp.

How to do it...

Perform the following steps:

  1. Add a button on the view of the controller.

  2. Enter the following using directive in the AddressBookAppViewController.cs file:

    using MonoTouch.AddressBook;
  3. Override the ViewDidLoad method:

    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();
      this.btnReadContacts.TouchUpInside += (s, e) => {
      ABAuthorizationStatus abStatus = ABAddressBook.GetAuthorizationStatus();
      NSError error;
      ABAddressBook addressBook = ABAddressBook.Create(out error);
      if (abStatus == ABAuthorizationStatus.NotDetermined)
      {
        addressBook.RequestAccess((g, err) => {
          if (!g)
          {
            Console.WriteLine("User denied address book access!");
          } else
          {
            this.InvokeOnMainThread(() =>this.ReadContacts(addressBook...