Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin ios: ABAddressbook.Create always null, can't request access

I have a problem with requesting access to the Addressbook, because ABAddressbook.Create is always null.

So how can I request the Access?

NSError err = new NSError ();
ABAddressBook ab = ABAddressBook.Create(out err)
ab.RequestAccess (delegate {}); //ab always null

Thanks for help.

like image 819
anguish Avatar asked Sep 27 '13 08:09

anguish


1 Answers

If it's null then something was wrong and your NSError should tell you what it is (btw there's no need to initialize an out parameter).

In general (iOS6+) your code should look like:

NSError err; 
var ab = ABAddressBook.Create (out err);
if (err != null) {
    // process error
    return;
}
// if the app was not authorized then we need to ask permission
if (ABAddressBook.GetAuthorizationStatus () != ABAuthorizationStatus.Authorized) { 
    ab.RequestAccess (delegate (bool granted, NSError error) { 
        if (error != null) {
            // process error
        } else if (granted) {
            // permission now granted -> use the address book
        } 
    }); 
} else { 
    // permission already granted -> use the address book
} 
like image 111
poupou Avatar answered Oct 11 '22 01:10

poupou