2017-10-25 5 views
0

우리의 애플 리케이션은 장치 접촉을 얻을 수있는 기능이 있습니다, 난 그냥 에뮬레이터에서 그것을 실행할 때 작동 CNcontacpickerview 사용하지만, 장치에서 실행되면 phonenumber 반환 ' 가져 오지 않음 '. 내 허락이나 뭐 때문에? 이상한은스위프트 3 : CNcontactpicker의 phonenumber 반환 장치에서

난 그냥 권한 다른 창에서 보여 내 페이지에이 코드를 추가 해결

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

답변

0

에뮬레이터에서 작동합니다

let addressBookStore = CNContactStore() 

addressBookStore.requestAccess(for: CNEntityType.contacts) { (isGranted, error) in 
    print(isGranted) 
    print(error) 
}