【问题标题】:Crash when trying to access Contacts Framework in iOS 10尝试在 iOS 10 中访问联系人框架时崩溃
【发布时间】:2016-10-24 13:14:50
【问题描述】:

我正在尝试访问联系人框架,以便将新联系人保存到用户通讯录中。我有以下代码...

import Contacts
import ContactsUI

在文件的开头并包含一个方法...

func requestForAccess(completion: (granted: Bool) -> ()) {

    dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.rawValue), 0), {
        let authStatus = CNContactStore.authorizationStatusForEntityType(.Contacts)

        switch authStatus {
        case .Authorized:
            completion(granted: true)
        case .Denied, .NotDetermined:

            // CRASH HERE before the completion block is called, after calling 
            // requestAccessForEntityType

            self.contactStore.requestAccessForEntityType(.Contacts, completionHandler: { (access, accessError) -> () in
                if access {
                    completion(granted: access)
                } else {
                    if authStatus == .Denied {
                        dispatch_async(dispatch_get_main_queue(), { () -> () in
                            let msg = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
                            //self.alertService.showAlert(msg)
                        })
                    }
                }
            })
        default:
            completion(granted: false)
        }
    })
}

在标记的行,有一个SIABRT 崩溃。结束堆栈并运行po $arg1 会抛出以下...

error: Couldn't materialize: couldn't read the value of register x0
Errored out in Execute, couldn't PrepareToExecuteJITExpression

我在根dictInfo.plist 中有必要的行

<key>NSContactsUsageDescription</key>
<string>We need to access Contacts to import entries direct from the app.</string>

我还在目标属性的“链接框架和库”中包含Contacts.frameworkContactsUI.framework(常规)

【问题讨论】:

  • 您的设备是否运行 iOS 10?清理项目、从设备中删除应用程序或重新启动有时会有所帮助
  • CNContactStore 框架是否在构建阶段根据需要添加为可选?你为什么要在后台线程中访问请求?
  • 无关但请注意,当当前授权被“拒绝”时,没有理由致电requestAccessForEntityType。不会再次提示用户。对requestAccessForEntityType 的调用将简单地再次返回“拒绝”而不询问用户。相反,您应该显示一条警报,告知用户访问当前被拒绝,并提供启动应用程序设置页面的机会,用户可以在其中启用访问权限。
  • 抱歉,我在考虑 Contacts.framework。这是发生在设备上还是模拟器上?

标签: ios contacts ios10


【解决方案1】:

我为联系人描述添加了 plist 条目,并稍微修改了您的代码以在我的 swift 3.0 项目中使用,以下代码在我的 iOS 10.0.2 上运行起来就像一个魅力

   override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        self.requestForAccess { (gotAccess) in
            print(gotAccess)
        }
    }

    func requestForAccess(_ completion: @escaping (_ granted: Bool) -> ()) {
        DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { 
            let authStatus = CNContactStore.authorizationStatus(for: .contacts)

            switch authStatus {
            case .authorized:
                completion(true)
            case .denied, .notDetermined:

                self.contactStore.requestAccess(for: .contacts, completionHandler: { (access, accessError) -> () in
                    if access {
                        completion(access)
                    } else {
                        if authStatus == .denied {
                            DispatchQueue.main.async {
                                let msg = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
                                print(msg)
                            }
                        }
                    }
                })
            default:
                completion(false)
            }
        }
    }

【讨论】:

    【解决方案2】:

    好的,所以我想通了 - 恐怕这不是什么明显的事情。

    由于某种原因,XCode 复制了我的 Info.plist 并且正在解析和加载副本,而不是我根目录中的 Info.plist。虽然我将NSContactsUsageDescription 添加到一个文件中,但它正在读取另一个文件,因此崩溃了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多