尝试访问iOS 10中的Contacts Framework时崩溃

时间:2016-10-24 13:14:51

标签: ios contacts ios10

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

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

我在Info.plist

dict中有必要的行
<key>NSContactsUsageDescription</key>
<string>We need to access Contacts to import entries direct from the app.</string>

我还在目标媒体资源(常规)中的“关联框架和图书馆”中加入了Contacts.frameworkContactsUI.framework

2 个答案:

答案 0 :(得分:3)

我已经为联系人描述添加了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)
            }
        }
    }

答案 1 :(得分:3)

好的,所以我明白了 - 恐怕没什么明显的。

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