0

our apps have a feature to get device contact, i have just used CNcontacpickerview it works when i run it on emulator, but when it run on device, the phonenumber return 'not fetched'. is it because my permission or what? the weird is it works on emulator

enter image description here

import UIKit
import ContactsUI
import MessageUI

class SharedAboutViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CNContactPickerDelegate,MFMessageComposeViewControllerDelegate {

    @IBOutlet weak var tableview: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        // Do any additional setup after loading the view.
    }

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        //... handle sms screen actions
        self.dismiss(animated: true, completion: nil)
    }

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

        let when = DispatchTime.now() + 0.2 // change 2 to desired number of seconds
        DispatchQueue.main.asyncAfter(deadline: when) {
            // Your code with delay
            if (MFMessageComposeViewController.canSendText()) {
                let controller = MFMessageComposeViewController()
                controller.body = "Message Body"
                controller.recipients = ["hello"]
                controller.messageComposeDelegate = self
                self.present(controller, animated: true, completion: nil)
            }
        }

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let contactPicker = CNContactPickerViewController()
        contactPicker.delegate = self
        contactPicker.displayedPropertyKeys =
            [CNContactNicknameKey
                ,CNContactEmailAddressesKey]

        self.present(contactPicker, animated: true, completion: nil)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cells");
        cell?.textLabel?.text = "hello";
        return cell!;
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.view.frame.height*0.08;
    }
}
Benny Wijaya
  • 207
  • 3
  • 16

1 Answers1

0

solved

i just added this code in my page to show permission popout

let addressBookStore = CNContactStore()

addressBookStore.requestAccess(for: CNEntityType.contacts) { (isGranted, error) in
    print(isGranted)
    print(error)
}
Benny Wijaya
  • 207
  • 3
  • 16
  • My experience: if you use `contactPicker.displayedPropertyKeys`, the parts of the text template of printing `CNContact` will read "not fetched. For example, if you added the key for phone to that array, re-running the code would replace the "phoneNumbers=(not fetched) display with some `CNPhoneNumber` values. – benc Aug 05 '20 at 06:23
  • I have done less learning about the store, but I think it makes sense (you *do* want to display limited info, but after the contact is selected, you do want to pull the entire set, even data that wasn't necessarily displayed) – benc Aug 05 '20 at 06:25