サーチ…


備考

役に立つリンク

連絡先アクセスの承認

フレームワークのインポート

迅速

import Contacts

目標-C

#import <Contacts/Contacts.h>

アクセシビリティのチェック

迅速

switch CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts){
case .Authorized: //access contacts
case .Denied, .NotDetermined: //request permission
default: break
}

目標-C

switch ([CNContactStore authorizationStatusForEntityType:CNEntityType.Contacts]){
case CNAuthorizationStatus.Authorized:
    //access contacts
    break;
case CNAuthorizationStatus.Denied:
    //request permission
    break;
case CNAuthorizationStatus.NotDetermined:
    //request permission
    break;
}

許可の要求

迅速

var contactStore = CKContactStore()
contactStore.requestAccessForEntityType(CKEntityType.Contacts, completionHandler: { (ok, _) -> Void in
    if access{
        //access contacts
    }
}

連絡先へのアクセス

フィルタの適用

連絡先にアクセスするために、我々は、種類のフィルタ適用すべきであるNSPredicate我々はオーソライズお問い合わせアクセスの例では、それを定義し、当社のContactStoreの変数に。たとえば、ここでは自分の名前と一致する連絡先を並べ替えるとします:

迅速

let predicate = CNContact.predicateForContactsMatchingName("Some Name")

目標-C

NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"Some Name"];

フェッチするキーの指定

ここでは、連絡先の名、姓、プロフィール画像を取得します。

迅速

let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey]

連絡先を取得しています

迅速

do {
    let contacts = try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
} catch let error as NSError {
    //...
}

連絡先の詳細へのアクセス

迅速

print(contacts[0].givenName)
print(contacts[1].familyName)
let image = contacts[2].imageData

連絡先の追加

迅速

import Contacts
 
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
 
contact.imageData = NSData() // The profile picture as a NSData object
 
contact.givenName = "John"
contact.familyName = "Appleseed"
 
let homeEmail = CNLabeledValue(label:CNLabelHome, value:"[email protected]")
let workEmail = CNLabeledValue(label:CNLabelWork, value:"[email protected]")
contact.emailAddresses = [homeEmail, workEmail]
 
contact.phoneNumbers = [CNLabeledValue(
    label:CNLabelPhoneNumberiPhone,
    value:CNPhoneNumber(stringValue:"(408) 555-0126"))]
 
let homeAddress = CNMutablePostalAddress()
homeAddress.street = "1 Infinite Loop"
homeAddress.city = "Cupertino"
homeAddress.state = "CA"
homeAddress.postalCode = "95014"
contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]
 
let birthday = NSDateComponents()
birthday.day = 1
birthday.month = 4
birthday.year = 1988  // You can omit the year value for a yearless birthday
contact.birthday = birthday
 
// Saving the newly created contact
let store = CNContactStore()
let saveRequest = CNSaveRequest()
saveRequest.addContact(contact, toContainerWithIdentifier:nil)
try! store.executeSaveRequest(saveRequest)


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow