简体   繁体   English

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

[英]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当我在模拟器上删除应用程序并清理然后再次构建并运行该应用程序时,它可以正常工作,会出现一个带有权限请求的弹出视图,但是在真实设备上它可以正常工作,当我从手机中删除应用程序并清理时,权限会在第一次弹出构建并运行我没有再次收到弹出权限请求

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当您删除应用程序时,如果您想在同一天删除它,iOS 会将捆绑标识符的权限保留一天,您有三个选项

  1. change the iPhone OS (iOS) data by increasing iPhone OS (iOS) date with a day通过增加一天的 iPhone OS (iOS) 日期来更改 iPhone OS (iOS) 数据
  2. Wait for a day等一天
  3. Reset the Device settings重置设备设置

在此处输入图片说明

Click here the apple docs reference that i take the screenshot form it also you can check it. 单击此处我截取屏幕截图的苹果文档参考,您也可以查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 应用程序在真实设备上崩溃,但在模拟器上运行 - Swift app crashes on real device but works on simulator 文件仅在模拟器上加载,而不在真实设备上加载(Swift 3) - File only loads on Simulator not on real device (Swift 3) 科尔多瓦:警报未在桌面环境中显示(它们在模拟器和真实设备上均能正常工作) - Cordova: Alerts not shown on desktop environment (They work fine on simulator and real device) Flutter 中的 HealthKit Permission 弹出窗口未出现在真实设备和模拟器中 - Popup of HealthKit Permission is not appearing in real device and simulator in Flutter 当检查nil时,Swift Optional有时会在设备上崩溃,但不会在模拟器上崩溃 - Swift Optional sometimes crashes on device but not on simulator when checking nil 有一种方法可以访问 iphone/ipad 中的文档文件夹(真实设备,没有模拟器)? - There's a way to access the document folder in iphone/ipad (real device, no simulator)? AVAudioPlayer在模拟器上工作但不在Real Device上工作 - AVAudioPlayer working on Simulator but not on Real Device UIImage在模拟器上显示,而不是在真实设备上显示 - UIImage is showing on simulator, not on a real device 使用真实设备作为模拟器和存储 - Using real device as simulator & storage AVPlayer在模拟器上运行,但不能在真实设备上运行 - AVPlayer runs on simulator but not real device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM