-5
連絡先の電話番号をすべてアレイに登録するにはどうすればよいですか?データベースに1つ以上の数値が存在するかどうかをチェックするために、配列を私のServer/DBに送るには、これが必要です。連絡先の電話番号をすべてアレイに登録するにはどうすればよいですか?
私はまだSWIFT 2で動作し、後にも迅速3.
でこのコードは動作しますが、私は、それははるかに優れたバージョンを存在すると思います。手伝って頂けますか?
ご協力いただきありがとうございます。
// With help: http://stackoverflow.com/questions/37039103/how-to-fetch-only-mobile-numbers-in-swift-using-cncontacts
// With help: http://stackoverflow.com/questions/32669612/how-to-fetch-all-contacts-record-in-ios-9-using-contacts-framework/34095632
let store = CNContactStore()
store.requestAccessForEntityType(.Contacts, completionHandler: {
granted, error in
guard granted else {
let alert = UIAlertController(title: "Can't access contact", message: "Please go to Settings -> MyApp to enable contact permission", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactPhoneNumbersKey]
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
var cnContacts = [CNContact]()
do {
try store.enumerateContactsWithFetchRequest(request){
(contact, cursor) -> Void in
cnContacts.append(contact)
}
} catch let error {
NSLog("Fetch contact error: \(error)")
}
var mobilenumbers: [String] = []
NSLog(">>>> Contact list:")
for contact in cnContacts {
let fullName = CNContactFormatter.stringFromContact(contact, style: .FullName) ?? "No Name"
NSLog("\(fullName): \(contact.phoneNumbers.description)")
// If phoneNo a Mobilenumber, then put into Array:
for phoneNo in contact.phoneNumbers {
if phoneNo.label == CNLabelPhoneNumberMobile {
let istEineMobileNummer = (phoneNo.value as! CNPhoneNumber).stringValue
mobilenumbers.append(istEineMobileNummer)
}
}
}
print("Print all Mobilenumbers:")
print(mobilenumbers)
})
ありがとうございます。上記の私は、動作するサンプルコードを追加しました。しかし、私は、このコードは十分ではないと思う... – Andi