Sunday, December 15, 2024
HomeiOS Developmentios - Displaying restricted contacts record in UIKit

ios – Displaying restricted contacts record in UIKit


I’ve an app that was written in UIKit. It is too giant, and it will be a lot too time consuming at this level to transform it to SwiftUI.

I need to incorporate the brand new restricted contacts into this app. The best way it is at the moment written every part works high-quality aside from exhibiting the restricted contacts within the contact picker.

I’ve downloaded and gone although the Apple tutorial app however I am having bother pondering it by into UIKit. After a few hours I made a decision I need assistance.

I perceive I want to tug the contact IDs of the contacts which might be within the restricted contacts record. Undecided how to try this or tips on how to get it to show within the picker. Any assist can be drastically appreciated.

func requestAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void)
    {
        change CNContactStore.authorizationStatus(for: .contacts)
        {
        case .licensed:
            completionHandler(true)
            
        case .denied:
            showSettingsAlert(completionHandler)
            
        case .restricted, .notDetermined:
            CNContactStore().requestAccess(for: .contacts) { granted, error in
                if granted
                {
                    completionHandler(true)
                } else {
                    DispatchQueue.essential.async { [weak self] in
                        self?.showSettingsAlert(completionHandler)
                    }
                }
            }
            // iOS 18 solely
        case .restricted:
            completionHandler(true)
            
        @unknown default: break
        }
    }

// A textual content area that shows the title of the chosen contact
 @IBAction func contact_Fld_Tapped(_ sender: TextField_Designable)
    {
        sender.resignFirstResponder()
        
        // The contact ID that's saved to the Db
        getTheCurrentContactID()
        
        let theAlert = UIAlertController(title: Okay.Titles.chooseAContact, message: nil, preferredStyle: .actionSheet)
        
        // Create a brand new contact
        let addContact = UIAlertAction(title: Okay.Titles.newContact, model: .default) { [weak self] _ in
            self?.requestAccess { _ in
                let openContact = CNContact()
                let vc = CNContactViewController(forNewContact: openContact)
                vc.delegate = self // this delegate CNContactViewControllerDelegate
                DispatchQueue.essential.async {
                    self?.current(UINavigationController(rootViewController: vc), animated: true)
                }
            }
        }
        
        // Choose contact from contact record
        let getContact = UIAlertAction(title: Okay.Titles.fromContacts, model: .default) { [weak self] _ in
            self?.requestAccess { _ in
                self?.contactPicker.delegate = self
                DispatchQueue.essential.async {
                    self?.current(self!.contactPicker, animated: true)
                }
            }
        }
        
        // Edit the present contact
        let editBtn = UIAlertAction(title: Okay.Titles.editContact, model: .default) { [weak self] _ in
            self?.requestAccess { _ in
                let retailer = CNContactStore()
                var vc = CNContactViewController()
                do {
                    let descriptor = CNContactViewController.descriptorForRequiredKeys()
                    let editContact = strive retailer.unifiedContact(withIdentifier: self!.oldContactID, keysToFetch: [descriptor])
                    vc = CNContactViewController(for: editContact)
                } catch {
                    print("Getting contact to edit failed: (self!.VC_String) (error)")
                }
                
                vc.delegate = self // delegate for CNContactViewControllerDelegate
                
                self?.navigationController?.isNavigationBarHidden = false
                self?.navigationController?.navigationItem.hidesBackButton = false
                self?.navigationController?.pushViewController(vc, animated: true)
            }
        }
        
        let cancel = UIAlertAction(title: Okay.Titles.cancel, model: .cancel) { _ in }
        
        if oldContactID.isEmpty
        {
            editBtn.isEnabled = false
        }
        
        theAlert.addAction(getContact) // Choose from contacts
        theAlert.addAction(addContact) // Create new contact
        theAlert.addAction(editBtn) // Edit this contact
        theAlert.addAction(cancel)
        
        let popOver = theAlert.popoverPresentationController
        popOver?.sourceView = sender
        popOver?.sourceRect = sender.bounds
        popOver?.permittedArrowDirections = .any
        current(theAlert,animated: true)
    }

func requestAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void)
    {
        change CNContactStore.authorizationStatus(for: .contacts)
        {
        case .licensed:
            completionHandler(true)
            
        case .denied:
            showSettingsAlert(completionHandler)
            
        case .restricted, .notDetermined:
            CNContactStore().requestAccess(for: .contacts) { granted, error in
                if granted
                {
                    completionHandler(true)
                } else {
                    DispatchQueue.essential.async { [weak self] in
                        self?.showSettingsAlert(completionHandler)
                    }
                }
            }
            // iOS 18 solely
        case .restricted:
            completionHandler(true)
            
        @unknown default: break
        }
    }

// MARK: - Contact Picker Delegate
extension AddEdit_Quote_VC: CNContactPickerDelegate
{
    func contactPickerDidCancel(_ picker: CNContactPickerViewController)
    {
        
    }
    
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
    {
        
    }
    
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact)
    {
        selectedContactID = contact.identifier
        
        let firm: String = contact.organizationName
        let companyText = firm == "" ? Okay.Titles.noCompanyName : contact.organizationName
        
        contactNameFld_Outlet.textual content = CNContactFormatter.string(from: contact, model: .fullName)!
        companyFld_Outlet.textual content = companyText
        
        save_Array[0] = Okay.AppFacing.true_App
        setSaveBtn_AEQuote()
    }
}

// MARK: - Create Contact Delegate
// Enhancing a contact
extension AddEdit_Quote_VC: CNContactViewControllerDelegate
{
    func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool
    {
        return false
    }
    
    func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?)
    {
        selectedContactID = contact?.identifier ?? ""
        
        if selectedContactID != ""
        {
            let firm: String = contact?.organizationName ?? ""
            let companyText = firm == "" ? Okay.Titles.noCompanyName : contact!.organizationName
            
            contactNameFld_Outlet.textual content = CNContactFormatter.string(from: contact!, model: .fullName)
            companyFld_Outlet.textual content = companyText
            
            getTheCurrentContactID()
            if selectedContactID != oldContactID
            {
                save_Array[0] = Okay.AppFacing.true_App
                setSaveBtn_AEQuote()
            }
        }
        
        dismiss(animated: true, completion: nil)
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments