ios - NFC 标签读取应用程序。线程 1:信号 SIGABRT
问题描述
当我点击我的应用程序上的扫描按钮时出现此错误,我不知道如何解决它:
NFC 标签读取应用程序。线程 1:信号 SIGABRT
Xcode 根本没有显示任何错误,当我点击扫描按钮时它崩溃了,其他一切正常。
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
}
}
解决方案
推荐阅读
- angular - 角度图像缩放悬停效果
- powershell - 从 Json 对象中检索值,其字段在 powershell 中具有点和连字符
- node.js - 快速服务器的 axios 响应中未填充“数据”字段
- javascript - 如何在geoJSON中添加标签和标题到点(标记),一旦加载就会显示在谷歌地图上
- typescript - 带有类型或接口的类型断言
- angular8 - 如何创建按钮作为重置按钮以重置 angular8 中的表单?
- mysql - 使用 EF 和 MySql 在生成的迁移上手动设置注释
- javascript - Jquery .css('背景', '值'); 不工作
- java - TreeSet 如何为添加维护 O(logN)?
- java - 创建无参数方法和字符串参数的区别?