如何解决电话应用程序提示可以快速工作一半
我正在制作一个应用程序,将存储在设备上的所有联系人导入应用程序 UITableView And when cell is selected the dialler should pop up with a selected phone number. 但是它有一半的时间
import UIKit
import Contacts
import ContactsUI
struct ContactsArray {
var name: String
var familyName: String
var phoneNumber: String
}
class ViewController: UIViewController {
var contactsArray = [ContactsArray]()
override func viewDidLoad() {
super.viewDidLoad()
fetchContacts()
// Some UI setup
}
func fetchContacts() {
let store = CNContactStore()
store.requestAccess(for: .contacts) { (granted,err) in
if let err = err {
print("Failed to get access",err)
return
}
if granted {
print("granted")
let key = [CNContactGivennameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey]
let request = CNContactFetchRequest(keysToFetch: key as [CNKeyDescriptor])
do {
var contactsArray = self.contactsArray
try store.enumerateContacts(with: request,usingBlock: { (contact,stopPointer) in
contactsArray.append(ContactsArray(name: contact.givenname,familyName: contact.familyName,phoneNumber: contact.phoneNumbers.first?.value.stringValue ?? ""))
})
self.contactsArray = contactsArray
} catch let err {
print("Failed to enumerate contacts")
}
} else {
print("grant denied...")
}
}
}
}
extension ViewController: UITableViewDelegate,UITableViewDataSource {
// Some code here,cell count,cellForItemAt and etc.
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath,animated: true)
let phoneNumber = contactsArray[indexPath.row].phoneNumber
print("number: \(contactsArray[indexPath.row].phoneNumber)") // This prints the correct number
print("number phone: \(phoneNumber)") // This prints the correct number too
// However here,the same phoneNumber prints error
guard let number = URL(string: "telprompt://\(phoneNumber)") else {
print("error")
return
}
UIApplication.shared.open(number)
}
}
问题是,当我选择特定联系人(例如我的老师或教练)时,它总是会正常打开。但是,当我单击朋友 1、朋友 2 等其他联系人时,它会进入返回块。
我在 didSelectRowAt 方法中添加了一些注释。这就是行动发生的地方。请帮帮我,谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。