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
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;
}
}