dyld: Library not loaded: @rpath/CoreNFC.framework

2019-06-27 09:04发布

I get this error and app get crash while run CoreNFC sample code in Xcode 9.0 beta 2

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

Does anyone knows how to fix this?

2条回答
Ridiculous、
2楼-- · 2019-06-27 09:48

CoreNFC is only available on iPhone 7 and iPhone 7 Plus devices. Make sure you're running your code on one of those.

See the WWDC session and the relevant documentation for more information.

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

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

查看更多
Fickle 薄情
3楼-- · 2019-06-27 09:51

I combined some answers together to fix this thanks to @Chinchan Zu's comment

here is how to mark Core NFC as Optional stackoverflow question

First you make import to NFCCore in "Linked Frameworks and Libraries" as optional as in this screenshot linking nfcCore as optional

Then inside your code you wrap your code with this #if check. Here is the class I used

#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
查看更多
登录 后发表回答