电话应用程序提示可以快速工作一半

问题描述

我正在制作一个应用程序,将存储在设备上的所有联系人导入应用程序 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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...