Я получаю эту ошибку, когда нажимаю кнопку сканирования в моем приложении, и я не знаю, как ее решить:
Приложение чтения меток NFC.Поток 1: сигнал SIGABRT
Xcode вообще не показывает ошибок и вылетает, когда я нажимаю кнопку Scan, все остальное работает нормально.
import UIKit
import CoreNFC
class HomeViewController: UIViewController, NFCNDEFReaderSessionDelegate {
// MARK: - Properties
let reuseIdentifier = "reuseIdentifier"
var detectedMessages = [NFCNDEFMessage]()
var session: NFCNDEFReaderSession?
// MARK: - IB-Outlets
@IBOutlet weak var tableView: UITableView!
// MARK: - ViewDidLoad Method
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
}
// MARK: - ViewDidAppear Method
override func viewDidAppear(_ animated: Bool) {
self.navigationItem.hidesBackButton = true
}
// MARK: - Scan Button Pressed
@IBAction func beginScanning(_ sender: UIButton) {
session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
session?.alertMessage = "Hold your iPhone near the item to learn more about it."
session?.begin()
}
// MARK: - User Information Button Pressed
@IBAction func userInformationButtonPressed(_ sender: UIBarButtonItem) {
}
// MARK: - Log Out Button Pressed
@IBAction func logOutButtonPressed(_ sender: UIBarButtonItem) {
}
// MARK: - NFC ReaderSession Delegates
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
// Check the invalidation reason from the returned error.
if let readerError = error as? NFCReaderError {
// Show an alert when the invalidation reason is not because of a success read
// during a single tag read mode, or user canceled a multi-tag read mode session
// from the UI or programmatically using the invalidate method call.
if (readerError.code != .readerSessionInvalidationErrorFirstNDEFTagRead)
&& (readerError.code != .readerSessionInvalidationErrorUserCanceled) {
let alertController = UIAlertController(
title: "Session Invalidated",
message: error.localizedDescription,
preferredStyle: .alert
)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
DispatchQueue.main.async {
self.present(alertController, animated: true, completion: nil)
}
}
}
// A new session instance is required to read new tags.
self.session = nil
}
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
DispatchQueue.main.async {
// Process detected NFCNDEFMessage objects.
self.detectedMessages.append(contentsOf: messages)
}
}
// MARK: - addMessage(fromUserActivity:)
func addMessage(fromUserActivity message: NFCNDEFMessage) {
DispatchQueue.main.async {
self.detectedMessages.append(message)
self.tableView.reloadData()
}
}
}
extension HomeViewController {
// MARK: - Table View Functions
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return detectedMessages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
let message = detectedMessages[indexPath.row]
let unit = message.records.count == 1 ? " Payload" : " Payloads"
cell.textLabel?.text = message.records.count.description + unit
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let indexPath = tableView.indexPathForSelectedRow,
let payloadsTableViewController = segue.destination as? PayloadsTableViewController else {
return
}
payloadsTableViewController.payloads = detectedMessages[indexPath.row].records
}
}