$npx -y skills add dpearson2699/swift-ios-skills --skill core-nfcRead and write NFC tags using CoreNFC. Use when scanning NDEF tags, reading ISO7816/ISO15693/FeliCa/MIFARE tags, writing NDEF messages, handling NFC session lifecycle, configuring NFC entitlements, or implementing background tag reading in iOS apps.
| 1 | # CoreNFC |
| 2 | |
| 3 | Read and write NFC tags on iPhone using the CoreNFC framework. Covers NDEF |
| 4 | reader sessions, tag reader sessions, NDEF message construction, entitlements, |
| 5 | and background tag reading. |
| 6 | |
| 7 | ## Contents |
| 8 | |
| 9 | - [Setup](#setup) |
| 10 | - [NDEF Reader Session](#ndef-reader-session) |
| 11 | - [Tag Reader Session](#tag-reader-session) |
| 12 | - [Writing NDEF Messages](#writing-ndef-messages) |
| 13 | - [NDEF Payload Types](#ndef-payload-types) |
| 14 | - [Background Tag Reading](#background-tag-reading) |
| 15 | - [Common Mistakes](#common-mistakes) |
| 16 | - [Review Checklist](#review-checklist) |
| 17 | - [References](#references) |
| 18 | |
| 19 | ## Setup |
| 20 | |
| 21 | ### Project Configuration |
| 22 | |
| 23 | 1. Add the **Near Field Communication Tag Reading** capability in Xcode |
| 24 | 2. Add `NFCReaderUsageDescription` to Info.plist with a user-facing reason string |
| 25 | 3. Add the `com.apple.developer.nfc.readersession.formats` entitlement with the current `TAG` value; do not add legacy `NDEF` |
| 26 | 4. For ISO 7816 tags, add supported application identifiers to `com.apple.developer.nfc.readersession.iso7816.select-identifiers` in Info.plist |
| 27 | 5. For FeliCa tags, add supported system codes to `com.apple.developer.nfc.readersession.felica.systemcodes`; do not use wildcard system codes |
| 28 | |
| 29 | ### Device Requirements |
| 30 | |
| 31 | NFC reading requires iPhone 7 or later. Always check for reader session |
| 32 | availability before creating NFC UI or sessions. Use the concrete reader |
| 33 | session type you are about to create. |
| 34 | |
| 35 | ```swift |
| 36 | import CoreNFC |
| 37 | |
| 38 | guard NFCNDEFReaderSession.readingAvailable else { |
| 39 | // Device does not support NFC or feature is restricted |
| 40 | showUnsupportedMessage() |
| 41 | return |
| 42 | } |
| 43 | ``` |
| 44 | |
| 45 | ### Key Types |
| 46 | |
| 47 | | Type | Role | |
| 48 | |---|---| |
| 49 | | `NFCNDEFReaderSession` | Scans for NDEF-formatted tags | |
| 50 | | `NFCTagReaderSession` | Scans for ISO7816, ISO15693, FeliCa, MIFARE tags | |
| 51 | | `NFCNDEFMessage` | Collection of NDEF payload records | |
| 52 | | `NFCNDEFPayload` | Single record within an NDEF message | |
| 53 | | `NFCNDEFTag` | Protocol for interacting with an NDEF-capable tag | |
| 54 | |
| 55 | ## NDEF Reader Session |
| 56 | |
| 57 | Use `NFCNDEFReaderSession` to read NDEF-formatted data from tags. This is the |
| 58 | simplest path for reading standard tag content like URLs, text, and MIME data. |
| 59 | |
| 60 | ```swift |
| 61 | import CoreNFC |
| 62 | |
| 63 | final class NDEFReader: NSObject, NFCNDEFReaderSessionDelegate { |
| 64 | private var session: NFCNDEFReaderSession? |
| 65 | |
| 66 | func beginScanning() { |
| 67 | guard NFCNDEFReaderSession.readingAvailable else { return } |
| 68 | |
| 69 | session = NFCNDEFReaderSession( |
| 70 | delegate: self, |
| 71 | queue: nil, |
| 72 | invalidateAfterFirstRead: false |
| 73 | ) |
| 74 | session?.alertMessage = "Hold your iPhone near an NFC tag." |
| 75 | session?.begin() |
| 76 | } |
| 77 | |
| 78 | // MARK: - NFCNDEFReaderSessionDelegate |
| 79 | |
| 80 | func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) { |
| 81 | // Session is scanning |
| 82 | } |
| 83 | |
| 84 | func readerSession( |
| 85 | _ session: NFCNDEFReaderSession, |
| 86 | didDetectNDEFs messages: [NFCNDEFMessage] |
| 87 | ) { |
| 88 | for message in messages { |
| 89 | for record in message.records { |
| 90 | processRecord(record) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func readerSession( |
| 96 | _ session: NFCNDEFReaderSession, |
| 97 | didInvalidateWithError error: Error |
| 98 | ) { |
| 99 | let nfcError = error as? NFCReaderError |
| 100 | if nfcError?.code != .readerSessionInvalidationErrorFirstNDEFTagRead, |
| 101 | nfcError?.code != .readerSessionInvalidationErrorUserCanceled { |
| 102 | print("Session invalidated: \(error.localizedDescription)") |
| 103 | } |
| 104 | self.session = nil |
| 105 | } |
| 106 | } |
| 107 | ``` |
| 108 | |
| 109 | ### Reading with Tag Connection |
| 110 | |
| 111 | For read-write operations, use the tag-detection delegate method to connect |
| 112 | to individual tags: |
| 113 | |
| 114 | ```swift |
| 115 | func readerSession( |
| 116 | _ session: NFCNDEFReaderSession, |
| 117 | didDetect tags: [any NFCNDEFTag] |
| 118 | ) { |
| 119 | guard let tag = tags.first else { |
| 120 | session.restartPolling() |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | session.connect(to: tag) { error in |
| 125 | if let error { |
| 126 | session.invalidate(errorMessage: "Connection failed: \(error)") |
| 127 | return |
| 128 | } |
| 129 | |
| 130 | tag.queryNDEFStatus { status, capacity, error in |
| 131 | guard error == nil else { |
| 132 | session.invalidate(errorMessage: "Query failed.") |
| 133 | return |
| 134 | } |
| 135 | |
| 136 | switch status { |
| 137 | case .notSupported: |
| 138 | session.invalidate(errorMessage: "Tag is not NDEF compliant.") |
| 139 | case .readOnly: |
| 140 | tag.readNDEF { message, error in |
| 141 | if let message { |
| 142 | self.processMessage(message) |
| 143 | } |
| 144 | session.invalidate() |
| 145 | } |
| 146 | case .readWrite: |
| 147 | tag.readNDEF { message, error in |
| 148 | if let message { |
| 149 | self.processMessage(message) |