12

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.

user3697159
  • 121
  • 6

2 Answers2

3

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
  }
Matthew Beckman
  • 1,702
  • 2
  • 16
  • 28
  • 1
    The question is about Web Application. Matthew, please explain, how a web site can ask an iOS for permissions? – user3697159 Feb 18 '17 at 15:06
  • If it's a native web app for iOS, you can request access to the contacts using the documentation linked to above. If it's just a webpage you are loading in Safari or other browser, then no, you can't access contacts on iOS or Android. – Matthew Beckman Feb 19 '17 at 20:08
  • Yep, the question is about a normal web site, no matter, which device a visitor uses, it might be attended with help of a Desktop browser. – user3697159 Feb 21 '17 at 07:31
  • There is no legit way for just the browser to get iCloud / address book access. That would be a security nightmare, so it's locked down on many levels. It would only work if you were building a native web app. – Matthew Beckman Feb 21 '17 at 07:36
  • To make the question clear, I've added the following: > 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. – user3697159 Feb 21 '17 at 07:44
  • Ah ok, that is a very different scenario than what I though you were asking. I *think* if you want to go that route, you're going to have to ask users to export their address book. Then you'd ask the user to upload that file. Would something like that work for you? If so, let me know, and I can do some research and update my answer. – Matthew Beckman Feb 21 '17 at 07:51
  • I'm confused. How do things like the below-linked GitHub repos work if there's no public HTTP API? https://github.com/adammck/ruby-icloud https://github.com/picklepete/pyicloud – bubbleking Sep 01 '17 at 00:40
1

Document from apple:

Discussion

Use this method to retrieve information about other users of the app. This method returns information about those users who meet the following criteria:

  • The user must be in the current user’s address book.
  • The user must have run the app.
  • The user must have granted the permission to be discovered for this container.

Guide of discoverAllUserIdentities

Codus
  • 1,433
  • 1
  • 14
  • 18