iosswiftcncontactstore

contact access permission is shown in simulator but not on real device sometimes swift


I have a simple code that request access to contacts

override func viewDidLoad() {
    super.viewDidLoad()
   fetchContacts()
}
func fetchContacts()
{
    let allowedCharset = CharacterSet
        .decimalDigits
    let store = CNContactStore()
    store.requestAccess(for: .contacts) { (granted, err) in
        if let error = err
            {
                print("failed to access",error)
                return
            }
        if (granted)
        {
            ///// after we get access to fetch contacts //// we reload table view data ///
            print("access granted")
            let keys = [CNContactGivenNameKey,CNContactPhoneNumbersKey,CNContactFamilyNameKey,CNContactMiddleNameKey]
            let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
            do {
                try store.enumerateContacts(with: request, usingBlock: { (contact, stopPointerIfYouWantToStopEnumerating) in
                    let array = contact.phoneNumbers
                    for number in array
                    {
                        let fullName = contact.givenName + contact.middleName
                        let lastName = contact.familyName
                        let value = number.value.stringValue
                        let number = String(value.unicodeScalars.filter(allowedCharset.contains))
                        print (number)
                        /////////// 4 cases we just need the phone not to be zero ///////

                        if (fullName != "SPAM")
                        {
                            self.firstName.append(fullName)
                            self.lastName.append(lastName)
                            self.numberArray.append(number)
                        }
                    }

                })
                //self.table()
            }
            catch let err2 {
                print ("failer to enurmerate",err2)
            }
          }
        }
}

This code works fine on simulator. When I delete app on the simulator and clean then build and run the app again it works fine a popup view appears with permissions request, however on real device it works the permissions pops the first time when I delete the app from the phone and clean then build and run I dont receive the pop permission request again


Solution

  • When you delete an app the iOS keeps the permissions for a day for bundle identifier if you want to remove it in the same date you have a three options

    1. change the iPhone OS (iOS) data by increasing iPhone OS (iOS) date with a day
    2. Wait for a day
    3. Reset the Device settings

    enter image description here

    Click here the apple docs reference that i take the screenshot form it also you can check it.