2016-10-24 2 views
1

Ich versuche, auf das Kontakte-Framework zuzugreifen, um einen neuen Kontakt im Adressbuch des Benutzers speichern zu können. Ich habe den folgenden Code ...Absturz beim Zugriff auf das Kontakte-Framework in iOS 10

import Contacts 
import ContactsUI 

an der Spitze der Datei und schließen ein Verfahren ...

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

Auf der Linie markiert, gibt es eine SIABRT Absturz. Wickeln des Stapels nach unten und po $arg1 läuft wirft die folgende up ...

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

ich die notwendige Linie in Info.plist im Stamm dict

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

Ich habe auch enthalten Contacts.framework und ContactsUI.framework in ‚Linked Frameworks haben und Bibliotheken in den Zieleigenschaften (Allgemein)

+1

Wird auf Ihrem Gerät iOS 10 ausgeführt? Reinigen des Projekts, Löschen der App vom Gerät oder Neustart manchmal hilft –

+1

bitte überprüfen Sie diesen Link http://StackOverflow.com/Questions/14704310/error-couldnt-materialize-struct-Couldnt-Read-Steuer –

+0

@Josip Ich habe versucht, zu deinstallieren vom Telefon, Neustart des Telefons, Neustart von XCode und Reinigung des Projekts, aber kein Glück :( – Brendan

Antwort

3

Ich habe plist Eintrag für Kontaktbeschreibung hinzugefügt und Ihren Code ein wenig geändert, um in meinem swi zu verwenden ft 3.0-Projekt, folgender Code funktioniert wie ein Zauber auf meinem 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

Ok, damit ich es herausgefunden - es war nichts offensichtlich, dass ich Angst habe.

Aus irgendeinem Grund hatte XCode meine Info.plist repliziert und Parsing und Laden einer Kopie anstelle der Info.plist in meinem Stammverzeichnis. Obwohl ich das NSContactsUsageDescription zu einer Datei hinzufügte, las es das andere, folglich der Unfall.

Verwandte Themen