Sök…


Anmärkningar

Användbara länkar

Auktorisera kontaktåtkomst

Importera ramverket

Snabb

import Contacts

Objective-C

#import <Contacts/Contacts.h>

Kontrollera tillgängligheten

Snabb

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

Objective-C

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

Begär tillstånd

Snabb

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

Åtkomst till kontakter

Applicera ett filter

För att få åtkomst till kontakter, bör vi använda ett filter av typ NSPredicate på vår contactStore-variabel som vi definierade det i Autorisera Contact Access-exempel. Här vill vi till exempel sortera ut kontakter med namnmatchning med våra egna:

Snabb

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

Objective-C

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

Ange nycklar för att hämta

Här vill vi hämta kontaktens förnamn, efternamn och profilbild:

Snabb

let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey]

Hämtar kontakter

Snabb

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

Få kontaktinformation

Snabb

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

Lägga till en kontakt

Snabb

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
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow