Inder
Inder

Reputation: 29

How to move and copy Contacts to the Gmail Account iOS

This is my code for getting device contacts and I want to move and copy these contacts to my Gmail Account.

 CNContactStore *contactStore = [[CNContactStore alloc] init];
 NSArray *keys = [[NSArray   alloc]initWithObjects:CNContactJobTitleKey,CNContactNoteKey,CNContactBirthdayKey,        CNContactThumbnailImageDataKey, CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNContactTypeKey, CNContactViewController.descriptorForRequiredKeys,CNContainerIdentifierKey, nil];
 CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
 request.predicate = nil;
 [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact* __nonnull contact, BOOL* __nonnull stop){

 }];       

Upvotes: 0

Views: 136

Answers (1)

Artem Stepanenko
Artem Stepanenko

Reputation: 3501

The most fundamental place you may want to check is Google Contacts API.

But for sure it's going to be time consuming and no easy. If that's not an option, you might consider to find something proper on GitHub.

Duraing a quick search I've found this library. Even if it doesn't do exactly what you need, you can get an insight how are you supposed to integrate the Google's API.

This is what you need to do if you decide to not use third-party libraries. (More details you can find here.):

1) you need to authorize;

2) after that you may send a request that creates a new contact:

POST /m8/feeds/contacts/default/full
Content-Type: application/atom+xml
GData-Version: 3.0
...

a request body:

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:gd="http://schemas.google.com/g/2005">
  <atom:category scheme="http://schemas.google.com/g/2005#kind"
    term="http://schemas.google.com/contact/2008#contact"/>
  <gd:name>
     <gd:givenName>Elizabeth</gd:givenName>
     <gd:familyName>Bennet</gd:familyName>
     <gd:fullName>Elizabeth Bennet</gd:fullName>
  </gd:name>
  <atom:content type="text">Notes</atom:content>
  <gd:email rel="http://schemas.google.com/g/2005#work"
    primary="true"
    address="[email protected]" displayName="E. Bennet"/>
  <gd:email rel="http://schemas.google.com/g/2005#home"
    address="[email protected]"/>
  <gd:phoneNumber rel="http://schemas.google.com/g/2005#work"
    primary="true">
    (206)555-1212
  </gd:phoneNumber>
  <gd:phoneNumber rel="http://schemas.google.com/g/2005#home">
    (206)555-1213
  </gd:phoneNumber>
  <gd:im address="[email protected]"
    protocol="http://schemas.google.com/g/2005#GOOGLE_TALK"
    primary="true"
    rel="http://schemas.google.com/g/2005#home"/>
  <gd:structuredPostalAddress
      rel="http://schemas.google.com/g/2005#work"
      primary="true">
    <gd:city>Mountain View</gd:city>
    <gd:street>1600 Amphitheatre Pkwy</gd:street>
    <gd:region>CA</gd:region>
    <gd:postcode>94043</gd:postcode>
    <gd:country>United States</gd:country>
    <gd:formattedAddress>
      1600 Amphitheatre Pkwy Mountain View
    </gd:formattedAddress>
  </gd:structuredPostalAddress>
</atom:entry>

Upvotes: 1

Related Questions