How to initialize your app or uncheck the CNContactPickerViewController

Asked 2 years ago, Updated 2 years ago, 34 views

I am creating an app with Swift4.

V Place the Get button on the ViewController and press this button to open the iPhone contact list.
②Select any person (multiple people) from the list, check, and press Done to open an alert and display the selected person.
③Tap more people from there to see their name, mobile number, and email address.
④Press OK on the alert in の to return to ViewController.
⑤If you press the Get Me button on ViewController again to open the iPhone contact list, the check will not be missed.

I would like to Uncheck the CNContactPickerViewController checked with 」 in the 」 stage.Or if I initialize the app here, the check will disappear, but I don't know.

//ViewController.swift
//  ToAddressBook
//  Created by admin on November 13, 2017.
//  Copyright © 2017 css. All rights reserved.

import UIKit
import Contacts
import ContactsUI

classViewController:UIViewController, CNContactPickerDelegate, CNContactViewControllerDelegate{

    target: [(name:String, email:String, phNo:String)] = [ ]
    varnameList: String = [ ]
    varcnPicker=CNContactPickerViewController()

    func contactPickerDidCancel(_picker:CNContactPickerViewController){
        print("ContactPicker Cancelled"")
    }
    // Initialize the app???
    func applicationWillEnterForeground (application:UIApplication) {
        let storyboard = UIStoryboard (name: "Main", bundle:nil)
        storyboard.instantiateViewController(withIdentifier: "MainViewController")
    }

    // Show selected person as alert
    func displayNameList(mycount:Int){
        self.cnPicker.dismiss(animated:false){
            print("It's closed?")
        }
        let alert = UIAlertController(
            title: "Selected person",
            message: ",
            preferredStyle:.alert)
        // Add action to alert (action name is selected)
        for i in 0..<mycount {
            alert.addAction(
                UIAlertAction(
                    title: "\(nameList[i])",
                    style:.default,
                    handler: {(action)->Void in
                        // Each action (each person's information is taken from a tuple array and displayed as an alert)
                        letkekkaAlert=UIAlertController(
                            title:self.target[i].name,
                            message: "\(self.target[i].email)"+"\n"+"\(self.target[i].phNo)",
                            preferredStyle:.alert)
                        kekkaAlert.addAction(UIAlertAction(
                            title: "OK",
                            style:.default,
                            handler: {(action)->Void in
                                // I want to cancel the check or initialize the application
                  // Process here?
                                // Initialize target
                                self.target=[]                        
                        }))
                        self.present(kekkaAlert, animated:true, completion:nil)
                        print("target name=\(self.target[i].name)")
                        print("target email=\(self.target[i].email)")
                        print("targetphoneNumber=\(self.target[i].phNo)")
                })
            )
        }
        // Add "Cancel" to alert
        alert.addAction(
            UIAlertAction(
                title: "Cancel",
                style:.cancel,
                handler:nil)
        )
        // Alert View
        self.present(
            alert,
            animated —true,
            completion: {
                // print("Alert displayed")
            }
        )
    }
    // Press the "Done" button to select
    func contactPicker(_picker:CNContactPickerViewController, didSelect contacts:[CNContact]){
        contacts.forEach {contact in
            let names = contact.giveName +" + contact.familyName
            let emails=(contact.emailAddresses[0].value as!String)
            letphoneNo=(contact.phoneNumber[0].value as!CNPhoneNumber).value(forKey: "digits") as! String
            print("email is=\(emails)")
            print("phoneNois=\(phoneNo)")
            nameList.append(names)
            // Put it in the tuple array "target"
            target.append ((name:names, email:emails, phNo:phoneNo))
            print("\"(target)"")
        }
        let count = nameList.count
        print("nameList=\(nameList)")
        displayNameList (mycount:count)

    }
    // Press the "List" button.Get Contacts
    @ IBAction funcgetMyContact(_sender:Any){
        nameList = [ ]
        // Display CNContactPickerViewController to select a contact
        // When you do this, the first time you see a dialog that says, "Application is asking for access to your contact."
        // Requires permission to access the contact.If you allow it, you will see a list of contacts registered on your iPhone.
        //let cnPicker=CNContactPickerViewController()
        cnPicker.delegate=self
        self.present(cnPicker, animated:true, completion:nil)


    }

    override func viewDidLoad(){
        super.viewDidLoad()

    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func commitButtonTapped(){
        self.view.endEditing(true)
    }

}

Screen 1
Screen 1
Screen 2
Screen 2
Screen 3
Screen 3
Screen 4
Screen 4
Screen 5
Screen 5

swift ios

2022-09-30 10:27

1 Answers

@IBAction funcgetMyContact(_sender:Any){} was solved by letting cnPicker=CNContactPickerViewController() every time.Thank you.


2022-09-30 10:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.