13

How does one detect if an iPhone has the ability to use the NFC chip provided by the core NFC framework?

I know right now it only works on iPhone 7 and 7 plus but I don't want to hardcode hardware string identifiers as I don't know what devices will come out in the future.

SolidSnake4444
  • 3,372
  • 6
  • 40
  • 63
  • https://developer.apple.com/documentation/corenfc/nfcndefreadersession/2915854-readingavailable – Paulw11 Aug 28 '17 at 03:28
  • @Paulw11 Wow, can't believe I missed that. I'm guessing that was added in a later beta version? I don't remember that being there when it was announced. Put that in an answer. – SolidSnake4444 Aug 28 '17 at 03:32

4 Answers4

22

You can use the readingAvailable class property:

if NFCNDEFReaderSession.readingAvailable {
    // Set up the NFC session
} else {
    // Provide fallback option
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
10

Update for iOS 12:

1) If you want to run your app only for iPhone 7 and newer models you can add NFC requirement in Info.plist:

<key>UIRequiredDeviceCapabilities</key>
    <array>
        // ... your restrictions
        <string>nfc</string>
    </array>

With this requirement only the devices with NFC will be able to download our app from App Store.

2) For iPhones older than iPhone 7 and for iPads support you have to also check if Core NFC is available because it is not included for these devices. That is why you should link Core NFC framework using Weak Linking:

Weak Linking of Core NFC framework

and then check for Core NFC availability in code:

var isNFCAvailable: Bool {
    if NSClassFromString("NFCNDEFReaderSession") == nil { return false }
    return NFCNDEFReaderSession.readingAvailable
}

If isNFCAvailablereturns true then you can use all the APIs provided by Core NFC without worrying about your app crash.

Legonaftik
  • 1,350
  • 3
  • 17
  • 33
4

Also check devices with iOS < 11.0 ie. iPhone 5

import CoreNFC
.
.
// Check if NFC supported
if #available(iOS 11.0, *) {
   if NFCNDEFReaderSession.readingAvailable {
      // available  
   }
   else {
     // not
   }
} else {
  //iOS don't support
}
Matt
  • 399
  • 3
  • 6
-2

Can check the device list from Apple https://developer.apple.com/support/required-device-capabilities/ (it's long, use: cmd-f + "nfc")

tontonCD
  • 320
  • 2
  • 6
  • The question was for a code based solution that allows one to check in their code if the device that the code is running on supports NFC. This makes it future proof and you don't have to change the code later to support or remove devices. – SolidSnake4444 Feb 15 '22 at 03:53
  • The original question has no sense for me, because an app with NFC capabilities refuses to install if the device has not. So you see the notification. No sense to install and execute another code to see another notification. – tontonCD Feb 16 '22 at 15:47
  • 1
    The question was asked almost 5 years ago when there were devices that could be running iOS 11 and could be using an app that could turn on and off NFC access. This also is still applicable today with iPad, Mac, and iOS hybrid apps. Also you are able to install an NFC app on a non NFC phone unless the developer states that the app should be restricted to only NFC phones. If the developer does not restrict it, it can be installed on non NFC phones. – SolidSnake4444 Feb 17 '22 at 17:24
  • ok, nice information – tontonCD Mar 13 '22 at 16:43