联系人框架 phone 数组

Contact Framework phone array

我有一个 class "Contact" 管理一些人的个人信息。现在我想使用自 iOS 9 以来引入的联系人框架来保存其中的一些信息。

我的联系人 class 有一个

因为有些联系人没有家庭号码,有些联系人没有手机号码,所以我很难创建数组:

// cnContact is an object of the Contact Framework class
// contact is an object of my Contact class

if let huisnummer = contact.huisnummer  {
    if let mobiel = contact.nummer {
        cnContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: huisnummer)),CNLabeledValue(label: CNLabelPhoneNumberMobile, value: CNPhoneNumber(stringValue: mobiel))]
    } else {
        [CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: huisnummer))]
    }
} else {
    if let mobiel = contact.nummer {
        cnContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMobile, value: CNPhoneNumber(stringValue: mobiel))]
    }
}

如果所述数字存在,我必须继续使用 swifts 条件语句进行检查。但我认为,由于这是保存这些联系人的低效方法,因此必须有更有效的方法来保存它。

如何更高效地创建 cnContact 的 phone 号码?

Marlo 使用联系人框架从 iPhone 获取联系人

lazy var contacts: [CNContact] = {
    let contactStore = CNContactStore()
    let keysToFetch = [
        CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
        CNContactEmailAddressesKey,
        CNContactPhoneNumbersKey,
        CNContactImageDataAvailableKey,
        CNContactThumbnailImageDataKey]

    // Get all the containers
    var allContainers: [CNContainer] = []
    do {
        allContainers = try contactStore.containersMatchingPredicate(nil)
    } catch {
        print("Error fetching containers")
    }

    var results: [CNContact] = []

    // Iterate all containers and append their contacts to our results array
    for container in allContainers {
        let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)

        do {
            let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
            results.appendContentsOf(containerResults)
        } catch {
            print("Error fetching results for container")
        }
    }

    return results
}()

功能权限授权

 func askForContactAccess()
 {
    let authorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
    switch authorizationStatus {
    case .Denied, .NotDetermined:
      self.contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (access, accessError) -> Void in
         if !access {
           if authorizationStatus == CNAuthorizationStatus.Denied {
             dispatch_async(dispatch_get_main_queue(), { () -> Void in
               let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
               let alertController = UIAlertController(title: "Contacts", message: message, preferredStyle: UIAlertControllerStyle.Alert)
               let dismissAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
              }
              alertController.addAction(dismissAction)
              self.presentViewController(alertController, animated: true, completion: nil)
           })
         }
       }
     })
   break
      default:
   break
 }
}

Contact - Authorization,Fetch,Add,Update,Search

AppCoda - Contacts

答案与 swift 或联系人框架无关,而是与 1o1 编程有关。我一定是晚了,我没有想出解决方案,而是发布了解决方案:

// Creating empty array of CNLabeledValues
var phoneNumbers : [CNLabeledValue] = []

// Add the mobile number to the array: 
if let mobiel = contact.nummer {
   phoneNumbers.append(CNLabeledValue(label: CNLabelPhoneNumberMobile, value: CNPhoneNumber(stringValue: mobiel)))
}

// Add the Main number to the array:
if let huisnummer = contact.huisnummer {
    phoneNumbers.append(CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: huisnummer)))
}

// Add the array to the Contacts Framework contact
cnContact.phoneNumbers = phoneNumbers

就是这样 :-)