With the “three customers”, no future, no one to want, 49 years into the army and other jokes, iOS developers are less and less new. However, there are still some, so it would be nice to have a little help for the new learning of the application actually developed by the recording point (I also just switched from OC to SWIFT development, so the following are all SWIFT codes, if newcomers are also advised to learn SWIFT directly). Please pull it directly to the bottom, CTRL + W to see the problem.

First, address book related

Requirements – Obtain the address book content, do not need to modify, only need to obtain contact information, easy to contact.

Only access to address book content, does not involve modification, do not apply for permission, do not apply for permission, do not apply for permission!

1. First of all, adjust the system address book. Currently, the lowest supported system of App is above iOS 9, so the following methods of iOS 9 will not be introduced. Import import ContactsUI, pop-up system directories VCCNContactPickerViewController directly, follow CNContactPickerDelegate agent method, it’s as simple as that is done.

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let contactPick = CNContactPickerViewController()
        contactPick.delegate = self
        self.present(contactPick, animated: true, completion: nil)
    }
Copy the code

2, light pop out useless, but also can choose. Select a contact and implement the proxy method.

Single contact: There are two proxy methods and only one should be implemented. Method one takes precedence over method two.

See the code comments for the differences between the two methods, and that’s it. The second method is mainly used when a contact has more than one phone number, you can go to the details page and select the specific one you want.

/ / in the address book list select func contactPicker (_ picker: CNContactPickerViewController, didSelect contact: CNContact) {} // Go to the personal details page and select func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) { let familyName = contactProperty.contact.familyName let givenName = ContactProperty. Contact. GivenName var phoneNum = "" / / even if there are multiple phone calls, which one do you chose, it returns is which. If let phone = contactProperty.value as CNPhoneNumber { phoneNum = phone.stringValue } }Copy the code

Ps: In the personal details page, we usually do not need to display all the information. What exactly is displayed can be configured using this displayedPropertyKeys property. DisplayedPropertyKeys = [CNContactPhoneNumbersKey] so that only the phone number is displayed on the details page. Address, email, etc are displayed.

3. To achieve multiple contact selection, implement one of the following proxy methods. The difference is the same as above, one in the list, one in the details (I think it should be so, but it’s not!!). .

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    }

    func contactPicker(_ picker: CNContactPickerViewController, didSelectContactProperties contactProperties: [CNContactProperty]) {
    }
Copy the code

In my tests, multiple options won’t enter the details. Is my posture wrong?? Therefore, it is best to use method 1 directly when you want multiple choices.

And method 2 to be effective, you must set the following properties to be effective, but also cannot enter the details inside!! Is the posture wrong?

contactVC.predicateForSelectionOfProperty = NSPredicate(format: "key == 'emailAddresses'")
NSPredicate(format: "key == 'phoneNumbers'", argumentArray: nil)
Copy the code

Finally: the system address book also has many attributes to provide, are generally limited operation, their own point to go in to see it, here is not introduced.

TableView swipes to delete cells

Simple implementation, only use the system agent method to complete iOS 11 system before several agent method cooperation to complete, iOS 11 after a method to complete, much more convenient

IOS 11 after the proxy method

11 after a method to delete / / iOS func tableView (_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let deleteAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: "Delete") {_, _, _ in / / delete} return UISwipeActionsConfiguration (actions: a [deleteAction])}Copy the code

You can also limit which cells can be swiped and deleted

Func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {if "invalid" {return false} return true}Copy the code

Before iOS 11, there were several methods, deleting Settings and implementing separate proxy methods.

/ / delete the name func tableView (_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { return "delete" } func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) - > UITableViewCell. EditingStyle {return. Delete} / / perform operations func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCell. EditingStyle forRowAt indexPath: indexPath) {/ / delete}Copy the code

Now there’s a problem:

This is what the UI design looks like:

This is what happens when I use the system proxy approach:

The design draft requires that the icon be deleted inside the cell, and the system method is triggered at the edge of the screen to delete the icon and push the whole cell. This is not easy for me to handle, so I have to implement the sideslip delete operation? Any suggestions?