7

在Xcode 9.0 beta 2CoreNFC sample中运行代码时出现此错误并且应用程序崩溃

dyld: Library not loaded: @rpath/CoreNFC.framework/CoreNFC
  Referenced from: /var/containers/Bundle/Application/2837709C-C852-4811-B696-38F2725554D4/iOS-11-by-Examples.app/iOS-11-by-Examples
  Reason: image not found

有谁知道如何解决这个问题?

4

3 回答 3

13

感谢@Chinchan Zu 的评论,我将一些答案结合在一起来解决这个问题

这是如何将核心 NFC 标记为可选的stackoverflow 问题

首先,您在“Linked Frameworks and Libraries”中将 NFCCore 导入为可选,如此屏幕截图所示 将 nfcCore 链接为可选

然后在你的代码中,你用这个#if 检查来包装你的代码。这是我使用的课程

#if canImport(CoreNFC)

import Foundation
import CoreNFC

#endif

class NFCManagar: NSObject {
  #if canImport(CoreNFC)
  var session: NFCNDEFReaderSession?
  #endif

  var items = [Item]()
  var completion: ((_ success: Bool, _ error: Error?)-> Void)?

  func beginScanning(items: [Item], completion: @escaping (_ success: Bool, _ error: Error?)-> Void) {
      self.completion = completion
      self.items.removeAll()
      self.items.append(contentsOf: items)

      #if canImport(CoreNFC)
      session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
      session?.alertMessage = "Hold your iPhone near check in/out device."
      session?.begin()
      #endif
  }

}

#if canImport(CoreNFC)
extension NFCManagar: NFCNDEFReaderSessionDelegate {

  // MARK: - NFCNDEFReaderSessionDelegate

  /// - Tag: processingTagData
  func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
          debugPrint("Nfc is detected")
  }

  /// - Tag: endScanning
  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) {

              debugPrint("Nfc didInvalidateWithError \(error)")
          }
      }

      // A new session instance is required to read new tags.
      self.session = nil
  }
}
#endif
于 2018-10-18T07:05:32.303 回答
1

CoreNFC 仅适用于 iPhone 7 和 iPhone 7 Plus 设备。确保您在其中之一上运行您的代码。

有关更多信息,请参阅 WWDC 会议和相关文档。

https://developer.apple.com/videos/play/wwdc2017/718/

https://developer.apple.com/documentation/corenfc

于 2017-07-06T10:31:43.467 回答
0

对于 Xcode 11,将 CoreNFC.framework 设置为“请勿嵌入”。并且还用#if 检查包装你的代码。

于 2019-12-11T18:27:41.130 回答