最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]尝试用CNContactPickerViewController去绕开权限bug而直接访问通讯录

Swift crifan 3926浏览 0评论

折腾:

[规避解决]swift中通过CNContactStore获取联系人列表出错:error=Error Domain=CNErrorDomain Code=100 "访问被拒绝”

好像是系统bug,绕不开。

所以打算,直接用

ios – CNContactPicker grant permission not prompting – Stack Overflow

提到的:

CNContactPickerViewController

去看看是否可以绕开权限,直接访问通讯录

参考:

iOS9系列专题五——全新的联系人与联系人UI框架 – 珲少的个人空间 – 开源中国社区

CNContactViewController Class Reference

CNContactPickerViewController Class Reference

看了半天,基本有点概念了:

CNContactPickerViewController:负责,读取联系人列表

CNContactViewController:负责点击单个联系人后,显示单个联系人的信息

这里还有个例子:

ContactsUISample/ViewController.swift at master · koogawa/ContactsUISample · GitHub

之前就试了试:

swift   CNContactPickerViewController

swift CNContactViewController

contacts – CNContactPickerViewController() in xcode7 swift2 – Stack Overflow

然后试用了代码:

        if #available(iOS 9.0, *) {
           
            let contactPicker = CNContactPickerViewController()
            contactPicker.displayedPropertyKeys =
                [CNContactEmailAddressesKey, CNContactPhoneNumbersKey]
            self.presentViewController(contactPicker, animated: true, completion: nil)

可以显示出demo的效果:

然后此处,再去看看 设置-》隐私 中已经自动把此app添加了访问通讯录的权限,结果还是没有:

算了,去直接用:

class PhoneContactViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CNContactPickerDelegate {
    /***************************************************************************
     * CNContactPickerDelegate functions
    ***************************************************************************/
     // Called when a property of the contact has been selected by the user.
    @available(iOS 9.0, *)
    func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
        print("didSelectContactProperty")
        let contact = contactProperty.contact
        let contactName = CNContactFormatter.stringFromContact(contact, style: .FullName) ?? ""
        let propertyName = CNContact.localizedStringForKey(contactProperty.key)
        let title = "\(contactName)’s \(propertyName)"
       
        dispatch_async(dispatch_get_main_queue()) {
            let alert = UIAlertController(title: title,
                message: contactProperty.value?.description,
                preferredStyle: .Alert)
            alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
   
    // Called when the user taps Cancel.
    @available(iOS 9.0, *)
    func contactPickerDidCancel(picker: CNContactPickerViewController) {
        print("contactPickerDidCancel")
        print("picker=\(picker)")
    }
    func readPhoneContactToList(){
        print("readPhoneContactToList")
       
        if #available(iOS 9.0, *) {
            let contactPickerVC = CNContactPickerViewController()
            contactPickerVC.delegate = self
            let displayedItems = [CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactOrganizationNameKey, CNContactJobTitleKey]
            contactPickerVC.displayedPropertyKeys = displayedItems
//            self.presentViewController(contactPickerVC, animated: true, completion: nil)
            self.showViewController(contactPickerVC, sender: self)
        } else {
            // Fallback on earlier versions
            print("current OS version is <= 9.0")
        }
    }

结果:

好像是,可以正常工作了?

没有报错,没有异常?没有出现权限问题?

此处直接黑色没东西,难道是因为,此处使用的是iOS模拟器,里面没有一个通讯录联系人而导致的?

那就去iOS模拟器中,想办法创建几个联系人:

先去Home:

再去选择到对应的联系人:

创建几个:

然后再去允许代码试试,看看能否出来联系人。

结果后来证实是,调用显示的代码不对,改为:

            self.presentViewController(contactPickerVC, animated: true, completion: nil)
//            self.showViewController(contactPickerVC, sender: self)

即可正常显示联系人:

然后,再去想办法:

[未解决]修改或扩展CNContactPickerViewController中的显示出来的单行的联系人显示形式

转载请注明:在路上 » [已解决]尝试用CNContactPickerViewController去绕开权限bug而直接访问通讯录

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.202 seconds, using 22.10MB memory