Displaying Contact info on another ViewController with a segue

305 Views Asked by At

Im using contact framework to fetch user contact's name, number, email, and contact image, the data is being fetched. All I am able to display right now is only one of the keys in the table view, like either the name number or email, I wish to have the name be clicked and then segue to the other view controller where the rest of the properties show up. but I am having trouble displaying it on the other viewController. I have tried using the get contacts method in the prepareforsegue function, did not seem to work The code I am using to fetch contacts is

 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)
    }
    tableView.reloadData()
}


func retrieveContactsWithStore(store: CNContactStore) {
    do {
        let groups = try store.groupsMatchingPredicate(nil)
        // let predicate = CNContact.predicateForContactsInGroupWithIdentifier(groups[0].identifier)
        let containerId = CNContactStore().defaultContainerIdentifier()
        let predicate: NSPredicate = CNContact.predicateForContactsInContainerWithIdentifier(containerId)
        let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey,CNContactImageDataAvailableKey,CNContactThumbnailImageDataKey]
        print(CNContactEmailAddressesKey.capitalizedString)
        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)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier  = segue.identifier  {
        if identifier == "DetailContact" {

        }
    }






}
2

There are 2 best solutions below

0
On

As i said in my comments, create a new contact object in your other controller. For example: yourContactObject and then set some value to it in the prepare for segue as follows:

var selectedContact;// Your contact which you will set in the did select of the other controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier  = segue.identifier  {
        if identifier == "DetailContact" {
            let controller = segue.destinationViewController
            controller.yourContactObject = selectedContact.
        }
    }
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedContact = objects[indexPath.row]
}
0
On

Instead of trying to re-do the query in prepareForSegue, make a property on your other view controller, say var contact: CNContactStore! and then observe which row gets selected in the table view. So in didSelectRowAtIndexPath you can do the following...

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let contactSelected = yourDataModel[indexPath.row] //now you know which contact was selected
    self.performSegueWithIdentifier("DetailContact", sender: contactSelected) 
    /**you can pass the selected contact as the sender argument, 
      or set it as a class-level variable, 
      or use a delegate or notification, all would work */
}

And then in your prepareForSegue method, could do the following...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier  = segue.identifier  {
        if identifier == "DetailContact" {
            let controller = segue.destinationViewController
            controller.contact = sender as! CNContactStore
        }
    }
}