In this example, we loop over every message we received from the tag. The reader session calls this method once a tag with an NDEF message is found. 2-The important method for reading data from an NFC tag is readerSession(_:didDetectNDEFs:). This method is optional and can be omitted. As the name suggests, it is called when the session started. 1 - The first method is readerSessionDidBecomeActive(_:).
Once the session has started, you can see this alert: 3-The last step is to call the method begin on the session and the device will look for NFC tags nearby. 2-Additionally, we set the property alertMessage, this is the text that is displayed to the user whenever we start to scan. If you want to scan only one single tag you set it to true and if you want to keep scanning set it to false. If you don’t want to explicitly use an existing queue, you can use nil and a new dispatch queue is created for the session.įinally, invalidAfterFirstRead is a boolean flag to - you guessed it - invalid the session after the first tag is read. The parameter queue is a dispatch queue that’s used when the delegate gets called. We will take a closer look at it in some seconds. This protocol defines methods to handle events around the reading session, e.g. The first parameter is an object implementing the NFCNDEFReaderSessionDelegate protocol. 1 - When creating a new session object, it expects three arguments: delegate, queue, and invalidAfterFirstRead. Initializing a NFCNDEFReaderSession to communicate with NFC tags.