मैं एक ही मुद्दा था से मेल खाती है पता पुस्तिका के पहले रिकॉर्ड का उपयोग करने के लिए चुना उपयोग करने के लिए होने के साथ खुश नहीं था, लेकिन स्विफ्ट में, और निक बर्न्स सुझाव इस्तेमाल किया और इसके बारे में एक ब्लॉग पोस्ट ने लिखा है:
import Contacts
...
@IBOutlet weak var nameFirst: UILabel!
@IBOutlet weak var nameLast: UILabel!
@IBOutlet weak var email: UILabel!
@IBOutlet weak var phoneNo: UILabel!
@IBOutlet weak var address: UILabel!
/**
Search the addressbook for the contact
*/
private func getMe() {
let ownerName = getOwnerName()
let store = CNContactStore()
do {
// Ask permission from the addressbook and search for the user using the owners name
let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(ownerName), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey])
//assuming contain at least one contact
if let contact = contacts.first {
// Checking if phone number is available for the given contact.
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
// Populate the fields
populateUsingContact(contact)
} else {
//Refetch the keys
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey]
let refetchedContact = try store.unifiedContactWithIdentifier(contact.identifier, keysToFetch: keysToFetch)
// Populate the fields
populateUsingContact(refetchedContact)
}
}
} catch let e as NSError {
print(e.localizedDescription)
}
}
/**
Get the device owners name
- returns: The owners name
*/
private func getOwnerName() -> String {
// Get the device owners name
var ownerName = UIDevice.currentDevice().name.trimmedString.stringByReplacingOccurrencesOfString("'", withString: "")
// Get the model of the device
let model = UIDevice.currentDevice().model
// Remove the device name from the owners name
if let t = ownerName.rangeOfString("s \(model)") {
ownerName = ownerName.substringToIndex(t.startIndex)
}
return ownerName.trimmedString
}
/**
Populate the fields using a contact
- parameter contact: The contact to use
*/
private func populateUsingContact(contact: CNContact) {
// Set the name fields
nameFirst.text = contact.givenName
nameLast.text = contact.familyName
// Check if there is an address available, it might be empty
if (contact.isKeyAvailable(CNContactPostalAddressesKey)) {
if let
addrv = contact.postalAddresses.first,
addr = addrv.value as? CNPostalAddress where addrv.value is CNPostalAddress
{
let address = "\(addr.street)\n\(addr.postalCode) \(addr.city)\n\(addr.country)"
self.address.text = address
}
}
// Check if there is a phonenumber available, it might be empty
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
if let
phonenumberValue = contact.phoneNumbers.first,
pn = phonenumberValue.value as? CNPhoneNumber where phonenumberValue.value is CNPhoneNumber
{
phoneNo.text = pn.stringValue
}
}
}
और विस्तार:: यह मूल रूप से स्विफ्ट में अंतिम परिणाम है http://paulpeelen.com/2016/01/20/prefill-an-signup-form-in-ios-using-swift-and-cncontact/
extension String {
/// Trim a string
var trimmedString: String {
return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
}
स्रोत
2016-01-20 15:12:01
मैं पुष्टि कर सकता हूं कि ऐसा लगता है कि वे इसे कैसे करते हैं। यदि आप अपना डिवाइस नाम उस नाम पर बदलते हैं जो पता पुस्तिका में मौजूद नहीं है, तो कार्ड केस डिवाइस नाम से प्राप्त नया नाम दिखाता है। – TomSwift
बहुत बढ़िया। अच्छी चाल जो नियमों द्वारा निभाती है! – Genericrich
क्या यह अभी भी काम करता है? फोन पता पुस्तिका अनुमतियों के लिए नहीं पूछेगा? – Kamchatka