addressbook

 

「連絡先」のデータを取得して、viewに表示しました。

 

personAddress

 

 

 

 

 

 

 

 

 

1.フレームワークの追加とimport
・Contacts.framework
・ContactsUI.framework
を追加して、importする

import Contacts
import ContactsUI

2. delegate設定

class PersonRecodeViewController: UIViewController 
                                 ,CNContactPickerDelegate{

    @IBOutlet weak var tfName: UITextView! 
    @IBOutlet weak var tfTel: UITextView!
    @IBOutlet weak var tfAdr: UITextView!

 
    @IBAction func clickNameInBtn(sender: AnyObject) {
        clickType = .Click_NAME;
        
        let picker = CNContactPickerViewController()
        
        // Name,電話番号を表示する
        let displayedItems = [CNContactPhoneNumbersKey]
        picker.displayedPropertyKeys = displayedItems
        picker.delegate = self
        
        // Show the picker
        self.presentViewController(picker, animated: true, completion: nil)
    }
    
    @IBAction func clickTelInBtn(sender: AnyObject) {
        clickType = .Click_TEL;
        
        let picker = CNContactPickerViewController()
        
        // Name,電話番号を表示する
        let displayedItems = [CNContactPhoneNumbersKey]
        picker.displayedPropertyKeys = displayedItems
        picker.delegate = self
        
        // Show the picker
        self.presentViewController(picker, animated: true, completion: nil)
        
    }
    
    @IBAction func clickAddrInBtn(sender: AnyObject) {
        clickType = .Click_ADDRESS;
        let picker = CNContactPickerViewController()
        
        // Name,住所を表示する
        let displayedItems = [CNContactPostalAddressesKey]
        picker.displayedPropertyKeys = displayedItems
        picker.delegate = self
        
        // Show the picker
        self.presentViewController(picker, animated: true, completion: nil)
        
    }

   // MARK: - 「連絡先」のデータ選択時の
    func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
        let contact = contactProperty.contact
        let select = contactProperty.value?.description;
        print(contactProperty.value?.description)
        print(contactProperty.key)
        
        
        switch clickType {
        case .Click_NAME,.Click_TEL :
            if ( clickType ==  .Click_NAME ){
                let contactName = CNContactFormatter.stringFromContact(contact, style: .FullName) ?? ""
                tfName.text = contactName;
            }
  
            if( select?.rangeOfString("CNPhoneNumber") != nil)
            {
                var telnum: String!
                let value = contactProperty.value
                telnum = ( value as! CNPhoneNumber).valueForKey("digits") as! String
                
                if telnum != nil {
                    tfTel.text  = telnum

                    if tfTel.text.isEmpty == false {
                        btnTel.hidden = false
                    }

                }
                else{
                    tfTel.text  = "Not available tel number"
                }
            }
            
        case .Click_ADDRESS:
            if( select?.rangeOfString("CNPostalAddress") != nil)
            {
                var fullAdr: String!
                var post: String!
                var state: String!
                var city: String!
                var street: String!
                
                let value = contactProperty.value
                post = (value as! CNPostalAddress).valueForKey("postalCode") as! String 
                state = (value as! CNPostalAddress).valueForKey("state") as! String 
                city = (value as! CNPostalAddress).valueForKey("city") as! String 
                street = (value as! CNPostalAddress).valueForKey("street") as! String 
                fullAdr = "\(post)\n\(state)\n\(city)\n\(street)"
                print("fullAdr:\(fullAdr)");
                
                if fullAdr != nil {
                    tfAdr.text  = fullAdr
                }
                else {
                    tfAdr.text  = "Not available home address"
                }
            }
        }
 
        btnSave.enabled = true;
    }

}

こんな感じになります。

address_1

 

 

 

 

 

 

address_2

 

 

 

 

 

 

 

 

person2

 

 

 

 

 

 

 

 

 

ここから、住所データから地点データに変更して、地図への表示へと続くのです。。。ではでは。