如何将联系人移动并复制到Gmail帐户iOS

时间:2016-11-16 11:57:19

标签: ios objective-c cncontactstore

这是我获取设备联系人的代码,我想将这些联系人移动并复制到我的Gmail帐户。

 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){

 }];       

1 个答案:

答案 0 :(得分:1)

您可能想要检查的最基本的地方是Google Contacts API

但可以肯定的是,这将耗费时间并且不容易。如果这不是一个选项,您可以考虑在 GitHub 上找到合适的内容。

快速搜索我找到了this库。即使它不能完全满足您的需求,您也可以了解如何整合Google的API。

如果您决定不使用第三方库,则需要执行此操作。 (您可以找到更多详细信息here。):

1)您需要authorize;

2)之后,您可以发送创建新联系人的请求:

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

请求正文:

<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="liz@gmail.com" displayName="E. Bennet"/>
  <gd:email rel="http://schemas.google.com/g/2005#home"
    address="liz@example.org"/>
  <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="liz@gmail.com"
    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>