0
Xcode 8で迅速に3つのアプリを作成しています。 、国と郵便番号を入力し、ビューコントローラーのラベルに入力します。私はinfo.plistファイルを変更して承認を許可し、以下のコードを書きました。情報を保持するラベルと連絡先にアクセスするためのボタンを作成しました。住所、市区町村、州、郵便番号、国の連絡先をiOSで取得し、ラベルに記入する
ラベルに入力された連絡先の氏名と電子メールアドレスを取得できましたが、住所、市区町村、州、郵便番号、国などの住所の詳細を取得する方法がわかりません郵便住所。私はスタックオーバーフローとインターネットで無駄に検索を使い果たしました。あなたの援助を丁重にお願いします。
洞察と提案は大歓迎です。
import UIKit
import Contacts
import ContactsUI
class ThirdViewController: UIViewController, CNContactPickerDelegate {
@IBOutlet weak var lblDetails: UILabel!
@IBOutlet weak var backTextField: UITextView!
@IBAction func btnSelectContact(_ sender: Any) {
let entityType = CNEntityType.contacts
let authStatus = CNContactStore.authorizationStatus(for:entityType)
if authStatus == CNAuthorizationStatus.notDetermined {
let contactStore = CNContactStore.init()
contactStore.requestAccess(for: entityType, completionHandler: {(success, nil) in
if success {
self.openContacts()
}
else {
print("not authorized")
}
})
}
else if authStatus == CNAuthorizationStatus.authorized {
self.openContacts()
}
}
func openContacts() {
let contactPicker = CNContactPickerViewController.init()
contactPicker.delegate = self as CNContactPickerDelegate
self.present(contactPicker, animated: true, completion: nil)
}
func contactPickerDidCancel(_picker: CNContactPickerViewController) {
_picker.dismiss(animated: true) {
}
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
// when a contact is selected get the full name
let fullName = "\(contact.givenName) \(contact.familyName)"
// the following gets the email address from contact
var email = "No Email Address Found"
if !contact.emailAddresses.isEmpty {
let emailString = (((contact.emailAddresses[0] as AnyObject).value(forKey: "labelValuePair")
as AnyObject).value(forKey: "value"))
email = emailString! as! String
}
// start of adding postaladdresses
var streetAddress = "No Postal Address Found"
if !contact.postalAddresses.isEmpty {
let streetAddressString = (((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair")
as AnyObject).value(forKey: "value"))
streetAddress = streetAddressString! as! String
}
// end of postal addresses
// the following takes the information and populates a label on the view controller
self.lblDetails.text = "\(fullName)\n\(email)\n\(streetAddress)" // \n\(streetName)
}
}
感謝をあなたは大変です。トラブルシューティングをしてからコードを提供するのはとても素晴らしいことです。私はあなたが私を助ける意志によって謙虚になります。私はあなたが時間とトラブルを抱えてそのように私を助けることを期待したことはありませんでした!私はあなたに最も感謝しています。私が助けてくれたやり方で他の人を助けることができるより良いプログラマになれば幸いです。 –
私はあなたを同僚として迎え入れてくれてありがとうございます。Stack Overflowはあなたをメンバーの一人として迎え入れてくれて幸いです! –