0

Im making an app which imports all contacts stored on a device to the app UITableView And when cell is selected the dialler should pop up with a selected phone number. However it works half the time

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)
        
        
        
    }
}

The thing is, when I select specific contacts like my teacher or coach, it always opens up properly. However when I click other contacts like friend1, friend2 or whatever, it gets into return block.

I've added some comments to the didSelectRowAt method. Thats where the action is happening. Please help me out, thanks

Russ
  • 53
  • 1
  • 9
  • Use `tel:` for your URL eg `tel:12125551234` – Paulw11 Jul 24 '21 at 06:08
  • @Paulw11 like so? `guard let number = URL(string: "tel:\(phoneNumber)") ` – Russ Jul 24 '21 at 06:12
  • 1
    Yes. You don't need the // Have you looked at the characters is the numbers that don't work? There must be something preventing them from being turned into a valid URL. – Paulw11 Jul 24 '21 at 06:13
  • @Paulw11 thank you for pointing that out. I've noticed it works only if the phone number is a consecutive string like `+120522222` and if it has spaces, brackets or dash like `+1(205)22222` , `+1 205 222 22` , `+1-205-222-22` it gets into the error block. How can I fix this? – Russ Jul 24 '21 at 06:22
  • Strip those characters from the string. You might want to use PhoneNumberKit to normalise the numbers. – Paulw11 Jul 24 '21 at 06:22
  • @Paulw11 thank you for your help sir. I've added some string extension and now it works great. Might help you in the future too https://stackoverflow.com/questions/32364055/formatting-phone-number-in-swift/51427133#51427133 – Russ Jul 24 '21 at 06:44

0 Answers0