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

给你一囗甜甜゛ 提交于 2019-12-05 06:04:09

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

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

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