2016-10-24 26 views
1

我正在尝试访问联系人框架,因此我可以将新联系人保存到用户地址簿中。我有以下代码...尝试访问iOS 10中的联系人框架时发生崩溃

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.plistdict

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

我也包括在“链接的框架Contacts.frameworkContactsUI.framework和库'在目标属性(一般)

+1

您的设备是否运行iOS 10?清理项目,从设备删除应用程序或重新启动有时帮助 –

+1

请检查此链接http://stackoverflow.com/questions/14704310/error-couldnt-materialize-struct-couldnt-read-eax –

+0

@josip我试过卸载从手机,重新启动手机,重新启动XCode和清理项目,但没有运气:( – Brendan

回答

3

我已经添加plist条目的联系人描述和修改你的代码有点用我的swi英尺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) 
      } 
     } 
    } 
3

好了,所以我想它了 - 这是没有什么明显的,我害怕。

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

相关问题