dyld: Library not loaded: @rpath/CoreNFC.framework/CoreNFC, iOS11 and Xcode 9 beta

女生的网名这么多〃 提交于 2019-12-07 03:42:06

问题


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?


回答1:


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

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



回答2:


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



来源:https://stackoverflow.com/questions/44946057/dyld-library-not-loaded-rpath-corenfc-framework-corenfc-ios11-and-xcode-9-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!