联系人访问权限显示在模拟器中,但有时无法在真实设备上显示

时间:2018-10-19 21:06:33

标签: ios swift permissions access cncontactstore

我有一个简单的代码,要求访问联系人

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

此代码在模拟器上可以正常工作。当我在模拟器上删除应用程序并清理然后再次构建并运行该应用程序时,它可以正常工作,并显示带有权限请求的弹出视图,但是在实际设备上,当我第一次从手机中删除该应用程序并进行清理时,权限弹出构建并运行我不再收到弹出许可请求

1 个答案:

答案 0 :(得分:2)

当您删除某个应用程序时,如果您想在同一天将其删除,则iOS会保留捆绑包标识符一天的权限

  1. 通过每天增加iPhone OS(iOS)日期来更改iPhone OS(iOS)数据
  2. 等待一天
  3. 重置设备设置

enter image description here

Click here是苹果文档的参考资料,我将其作为屏幕截图,也可以对其进行检查。