ios - What i should change in my code to access all the contacts instead of just 1 contact with swift? -


i have specific problem. want access , present table view user's contact list. problem when run code access 1 contact contact list. i'm trying think should change nothing far. given code below need change achieve result want? here code:

import uikit import contacts import addressbook   class masterviewcontroller: uitableviewcontroller {  var detailviewcontroller: detailviewcontroller? = nil var objects = [cncontact]()  override func viewdidload() {     super.viewdidload()     // additional setup after loading view, typically nib.     let addexisting = uibarbuttonitem(title: "add existing", style: .plain, target: self, action: #selector(masterviewcontroller.addexistingcontact))     self.navigationitem.leftbarbuttonitem = addexisting      if let split = self.splitviewcontroller {         let controllers = split.viewcontrollers         self.detailviewcontroller = (controllers[controllers.count-1] as! uinavigationcontroller).topviewcontroller as? detailviewcontroller     }      nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(masterviewcontroller.insertnewobject(_:)), name: "addnewcontact", object: nil)     self.getcontacts() }  func getcontacts() {   let store = cncontactstore()    if cncontactstore.authorizationstatusforentitytype(.contacts) == .notdetermined {     store.requestaccessforentitytype(.contacts, completionhandler: { (authorized: bool, error: nserror?) -> void in       if authorized {         self.retrievecontactswithstore(store)       }     })   } else if cncontactstore.authorizationstatusforentitytype(.contacts) == .authorized {     self.retrievecontactswithstore(store)   }  }   func retrievecontactswithstore(store: cncontactstore) {    {   let groups = try store.groupsmatchingpredicate(nil)     let predicate =  cncontact.predicateforcontactsingroupwithidentifier(groups[0].identifier)   //let predicate = cncontact.predicateforcontactsmatchingname("john")   let keystofetch = [cncontactformatter.descriptorforrequiredkeysforstyle(.fullname), cncontactemailaddresseskey]     let contacts = try store.unifiedcontactsmatchingpredicate(predicate, keystofetch: keystofetch)   self.objects = contacts   dispatch_async(dispatch_get_main_queue(), { () -> void in     self.tableview.reloaddata()   }) } catch {   print(error)   } }  func addexistingcontact() {  }  override func viewwillappear(animated: bool) {     self.clearsselectiononviewwillappear = self.splitviewcontroller!.collapsed     super.viewwillappear(animated) }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  func insertnewobject(sender: nsnotification) {     if let contact = sender.userinfo?["contacttoadd"] as? cncontact {         objects.insert(contact, atindex: 0)         let indexpath = nsindexpath(forrow: 0, insection: 0)         self.tableview.insertrowsatindexpaths([indexpath], withrowanimation: .automatic)     } }  // mark: - segues  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if segue.identifier == "showdetail" {         if let indexpath = self.tableview.indexpathforselectedrow {             let object = objects[indexpath.row]             let controller = (segue.destinationviewcontroller as! uinavigationcontroller).topviewcontroller as! detailviewcontroller             controller.contactitem = object             controller.navigationitem.leftbarbuttonitem = self.splitviewcontroller?.displaymodebuttonitem()             controller.navigationitem.leftitemssupplementbackbutton = true         }     } }  // mark: - table view  override func numberofsectionsintableview(tableview: uitableview) -> int {     return 1 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     return objects.count }      override func tableview(tableview: uitableview, cellforrowatindexpath   indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath)  let contact = self.objects[indexpath.row] let formatter = cncontactformatter()  cell.textlabel?.text = formatter.stringfromcontact(contact) cell.detailtextlabel?.text = contact.emailaddresses.first?.value as? string  return cell   }        override func tableview(tableview: uitableview, caneditrowatindexpath    indexpath: nsindexpath) -> bool {     // return false if not want specified item editable.     return false   } } 

this how list of user's contacts , store them in cncontact array.

let request = cncontactfetchrequest(keystofetch: [cncontactemailaddresseskey, cncontactformatter.descriptorforrequiredkeysforstyle(.fullname)])      var contacts = [cncontact]()     {         try store.enumeratecontactswithfetchrequest(request) { contact, stop in             contacts.append(contact)         }          self.objects = contacts         nsoperationqueue.mainqueue().addoperationwithblock({              self.tableview.reloaddata()         })     } catch {         print(error)     } 

i'm assuming problem line groupsmatchingpredicate(nil), don't pass in predicate can't find matches. thought.


Comments

Popular posts from this blog

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

wordpress - (T_ENDFOREACH) php error -

Using django-mptt to get only the categories that have items -