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

[已解决]swift实现选择通讯录的联系人

iOS crifan 2258浏览 0评论

之前去找过类似的库:

[整理]iOS 获取通讯录 联系人

参考:

iOS通讯录开发 – 简书

Swift – 访问通讯录联系人(使用系统提供的通讯录交互界面)

swift – How to extract name from a contact in iOS? – Stack Overflow

去试试:

swift peoplepickernavigationcontroller didselectperson

PeoplePickerSwiftDemo/PickEmailViewController.swift at master · robertmryan/PeoplePickerSwiftDemo

最后用代码:

//

//  SelectContactViewController.swift

//  QorosSales

//

//  Created by licrifan on 16/5/17.

//  Copyright © 2016年 licrifan. All rights reserved.

//

import UIKit

//import AddressBook

import AddressBookUI

import Cartography

class SelectContactViewController: UIViewController, ABPeoplePickerNavigationControllerDelegate {

    let contactPickerNav:ABPeoplePickerNavigationController

   

    var phoneNumberLabel:UILabel

    var selectButton:UIButton

    init() {

        self.contactPickerNav = ABPeoplePickerNavigationController()

        self.phoneNumberLabel = UILabel()

        self.selectButton = UIButton()

        super.init(nibName: nil, bundle: nil)

    }

   

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

   

    override func viewDidLoad() {

        super.viewDidLoad()

       

        if let tabBarController = self.tabBarController {

            TabBarHeight = tabBarController.tabBar.frame.height //49

        }

       

        self.view.backgroundColor = UIColor.whiteColor()

       

        self.selectButton = UIButton(type: .System)

        self.selectButton.backgroundColor = UIColor.lightGrayColor()

        self.selectButton.setTitle("点击选择联系人", forState: UIControlState.Normal)

        self.selectButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

        self.selectButton.addTarget(self, action: #selector(SelectContactViewController.contactSelected), forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(self.selectButton)

        constrain(selectButton) {selectButton in

            selectButton.top == selectButton.superview!.top + 60

            selectButton.height == 50

            selectButton.width == 120

            selectButton.centerX == selectButton.superview!.centerX

//            selectButton.left == selectButton.superview!.left

//            selectButton.right == selectButton.superview!.right

        }

       

        self.phoneNumberLabel.text = "未选择联系人"

        self.phoneNumberLabel.numberOfLines = 0

        self.phoneNumberLabel.textAlignment = .Center

        self.phoneNumberLabel.font = UIFont.boldSystemFontOfSize(15)

        self.view.addSubview(self.phoneNumberLabel)

        constrain(phoneNumberLabel, selectButton) {phoneNumberLabel, selectButton in

            phoneNumberLabel.top == selectButton.bottom + 40

            phoneNumberLabel.height == 40

            phoneNumberLabel.left == phoneNumberLabel.superview!.left

            phoneNumberLabel.right == phoneNumberLabel.superview!.right

        }

       

        contactPickerNav.peoplePickerDelegate = self

//        contactPickerNav.displayedProperties = [NSNumber(int: kABPersonPhoneProperty), NSNumber(int: kABPersonFirstNameProperty), NSNumber(int: kABPersonMiddleNameProperty), NSNumber(int: kABPersonLastNameProperty)]

        contactPickerNav.displayedProperties = [NSNumber(int: kABPersonPhoneProperty)]

//        contactPickerNav.predicateForSelectionOfPerson = NSPredicate(value: false)

        contactPickerNav.predicateForEnablingPerson = NSPredicate(format: "phoneNumbers.@count > 0")

    }

   

    func contactSelected(sender:UIButton) {

        print("sender=\(sender)")

       

//        print("\(contactPickerNav.viewControllers)")

//        contactPickerNav.popToRootViewControllerAnimated(false)

//        contactPickerNav.popViewControllerAnimated(false)

        contactPickerNav.viewControllers = [] // clear previous saved view, jump to root view

//        print("\(contactPickerNav.viewControllers)")

        self.presentViewController(contactPickerNav, animated: true, completion: nil)

    }

   

    func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController) {

        print("DidCancel peoplePicker=\(peoplePicker)")

        contactPickerNav.dismissViewControllerAnimated(true, completion: nil)

    }

   

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier) {

        print("didSelectPerson peoplePicker=\(peoplePicker), person=\(person), property=\(property), identifier=\(identifier)")

       

//        print("namePrefix=\(person.namePrefix), displayName=\(person.displayName)")

//        print("property0=\(ABRecordCopyValue(person, Int32(0)).takeRetainedValue()), property1=\(ABRecordCopyValue(person, Int32(1)).takeRetainedValue())")

        let nameCFString : CFString = ABRecordCopyCompositeName(person).takeRetainedValue()

        let name : NSString = nameCFString as NSString

        print("name=\(name)")

       

        let multiPhoneValue: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()

        let index = ABMultiValueGetIndexForIdentifier(multiPhoneValue, identifier)

//        let phone = ABMultiValueCopyValueAtIndex(multiValue, index).takeRetainedValue() as! String

        var phone:String = ""

        let gotPhone = ABMultiValueCopyValueAtIndex(multiPhoneValue, index).takeRetainedValue()

        //(__NSCFString) gotPhone = 0xbffe12e4

        if gotPhone is String {

            phone = gotPhone as! String

        }

       

        print("multiPhoneValue=\(multiPhoneValue), index=\(index), phone=\(phone)")

        /*

         multiPhoneValue=ABMultiValueRef 0x78738030 with 2 value(s)

         0: _$!<Mobile>!$_ (0xf20848) – (555) 564-8583 (0x78980ba0)

         1: _$!<Main>!$_ (0xf20868) – (415) 555-3695 (0x78983380)

         , index=0, phone=(555) 564-8583

         */

       

       

        let phoneValueCount = ABMultiValueGetCount(multiPhoneValue)

        for index in 0..<phoneValueCount {

            let curPhoneLabel = ABMultiValueCopyLabelAtIndex(multiPhoneValue, index).takeRetainedValue()

            let curLocalizedPhoneLabel = ABAddressBookCopyLocalizedLabel(curPhoneLabel)

                .takeRetainedValue() as String

            let phoneValue = ABMultiValueCopyValueAtIndex(multiPhoneValue, index).takeRetainedValue()

            let phoneStr = phoneValue as! String

            print("[\(index)] curLocalizedPhoneLabel=\(curLocalizedPhoneLabel), phoneStr=\(phoneStr)")

            //[0] curLocalizedPhoneLabel=mobile, phoneStr=(555) 564-8583

            //[1] curLocalizedPhoneLabel=main, phoneStr=(415) 555-3695

        }

        self.phoneNumberLabel.text = "联系人:\(name)\r\n号码:\(phone)"

    }

   

//    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord){

//        print("didSelectPerson peoplePicker=\(peoplePicker), person=\(person)")

//    }

   

//    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, shouldContinueAfterSelectingPerson person: ABRecordRef, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {

//        print("peoplePicker=\(peoplePicker), person=\(person), property=\(property), identifier=\(identifier)")

//

//        peoplePickerNavigationController(peoplePicker, didSelectPerson: person, property: property, identifier: identifier)

//       

//        contactPickerNav.dismissViewControllerAnimated(true, completion: nil)

//       

//        return false;

//    }

}

效果:

转载请注明:在路上 » [已解决]swift实现选择通讯录的联系人

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 1.294 seconds, using 22.08MB memory