ios - Contact Framework phone array -


i have class "contact" manages personal information of people. want save of information using contact framework introduced since ios 9.

my contact class has

  • home number (huisnummer in dutch)
  • mobile number (mobiel in dutch)

because of contacts have no home number , of contacts have no mobile number hard me create array:

// cncontact object of contact framework class // contact object of 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))]     } } 

i have keep checking using swifts conditionals if said number exists. thought since inefficient way of saving contacts there must more efficient way save this.

how can create cncontact's phone numbers in more efficient way?

marlo fetching contacts iphone using contacts framework

lazy var contacts: [cncontact] = {     let contactstore = cncontactstore()     let keystofetch = [         cncontactformatter.descriptorforrequiredkeysforstyle(.fullname),         cncontactemailaddresseskey,         cncontactphonenumberskey,         cncontactimagedataavailablekey,         cncontactthumbnailimagedatakey]      // containers     var allcontainers: [cncontainer] = []     {         allcontainers = try contactstore.containersmatchingpredicate(nil)     } catch {         print("error fetching containers")     }      var results: [cncontact] = []      // iterate containers , append contacts our results array     container in allcontainers {         let fetchpredicate = cncontact.predicateforcontactsincontainerwithidentifier(container.identifier)          {             let containerresults = try contactstore.unifiedcontactsmatchingpredicate(fetchpredicate, keystofetch: keystofetch)             results.appendcontentsof(containerresults)         } catch {             print("error fetching results container")         }     }      return results }() 

function permission authorization

 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 app access contacts through 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

contacts data

retreive contacts data

appcoda - contacts


Comments