Russ
Russ

Reputation: 83

Phone app prompt works half the time swift

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

Upvotes: 0

Views: 129

Answers (0)

Related Questions