iOS 11 CoreNFC How To Check if device has NFC Capability?
Asked Answered
T

4

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.

Thud answered 28/8, 2017 at 3:27 Comment(2)
developer.apple.com/documentation/corenfc/nfcndefreadersession/…Intracardiac
@Intracardiac 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.Thud
I
22

You can use the readingAvailable class property:

if NFCNDEFReaderSession.readingAvailable {
    // Set up the NFC session
} else {
    // Provide fallback option
}
Intracardiac answered 28/8, 2017 at 3:35 Comment(7)
Worked and tested on an iPhone 7 Plus and an iPhone 6S. The 7 showed the NFC button and the 6S did not.Thud
if I just use the block above, it will return false for iPhone 7. =/ Anything I need to do more?Cammi
@iori24 Are you testing on a physical device?Intracardiac
@Intracardiac Yes. physical deviceCammi
This also comes back false for my iphone 11Discordancy
also returns false on iPhone 12 which is confirmed to read NFC tags (confirmed via shortcuts app -> automation -> create personal automation -> NFC -> scan)Exception
you also need to enable NFC tag reading capability in order for this to work.Exception
G
11

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.

Givens answered 17/10, 2018 at 13:21 Comment(0)
S
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
}
Soul answered 30/1, 2019 at 1:51 Comment(0)
H
-2

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

Harlamert answered 14/2, 2022 at 15:21 Comment(4)
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.Thud
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.Harlamert
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.Thud
ok, nice informationHarlamert

© 2022 - 2024 — McMap. All rights reserved.