Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting addresses from iCloud contacts via CloudKit JS

I want my web application allow to import user contacts (particularly addresses) from iCloud contacts.

Something similar to what Google People API provides for Google Contacts.

The scenario is that, a user comes to my site using a desktop browser and imports all their contacts.
So the user should not waste time on typing all their contacts to be able to use them on the site.

I'm trying to use CloudKit JS for the issue.

It looks like .discoverAllUserIdentities is what I need according to this:

GET users/discover: Fetches all user identities in the current user’s address book, described in Discovering All User Identities (GET users/discover)

However, I'm getting the empty set: {"users":[]}

It seems like the web application doesn't have the permissions to get the contacts. If it's so, how to request the permissions?

Or may be I'm on completely wrong way, then please point me on the right direction if the issue is solvable.

like image 400
user3697159 Avatar asked Aug 25 '16 11:08

user3697159


People also ask

How do I export contacts from iCloud?

Click the gear icon, then choose Select All to mark all of your contacts for export. Select the gear icon again, and choose ExportvCard to copy your contacts to a . vcf file.

What is API apple CloudKit com?

CloudKit is an integrated macOS and iOS API that functions as a backend as a service (BaaS). CloudKit is the framework that powers iCloud on iOS, macOS and on the web.

How do I import iCloud contacts?

to go to the People page. On the toolbar, select Manage > Import contacts. Select Browse, choose your CSV file, and then select Open. Select Import.


1 Answers

Updated Answer

Since you're not building a native web app, you can't request access to a users iCloud contacts. This would be a security nightmare if websites were allowed access to users data. There are tools like ShuttleCloud that offer an API for migrating users contacts. This is what Google uses for their own Gmail service.

So no, you can't request direct access to contacts through the browser, but there are tools out there to make importing things easier.

Old Answer

You need to ask iOS for permission. You can do so by viewing Apple's documentation.

Example

#import <AddressBookUI/AddressBookUI.h>

  // Request authorization to Address Book
  ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

  if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
      if (granted) {
          // First time access has been granted, add the contact
          [self _addContactToAddressBook];
      } else {
          // User denied access
          // Display an alert telling user the contact could not be added
      }
    });
  }
  else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
    [self _addContactToAddressBook];
  }
  else {
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
  }
like image 177
Matthew Beckman Avatar answered Sep 29 '22 20:09

Matthew Beckman