Getting addresses from iCloud contacts via CloudKit JS
Asked Answered
D

2

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.

Decillion answered 25/8, 2016 at 11:43 Comment(0)
P
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
  }
Presidency answered 17/2, 2017 at 7:22 Comment(7)
The question is about Web Application. Matthew, please explain, how a web site can ask an iOS for permissions?Decillion
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.Presidency
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.Decillion
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.Presidency
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.Decillion
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.Presidency
I'm confused. How do things like the below-linked GitHub repos work if there's no public HTTP API? github.com/adammck/ruby-icloud github.com/picklepete/pyicloudViperine
G
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

Guayule answered 29/8, 2017 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.