- Introduction
 
NFC Library for iOS uses Core NFC API. This Library is not available for other platforms.
2. Components in the Demo and what they do
The Demo contains a tab control with three tab items, named Read NDEF, Write NDEF, Read TAG. Each tab item contains Read or Write button, which in some cases calls the same onClick event and executes the same code. For example the Read NDEF and Write NDEF tab items both execute the same code in their buttons’ onClick events. You see the code bellow.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  | 
						procedure TFormMain.ButtonNdefClick(Sender: TObject); begin   Memo.Lines.Clear;   try     if not TNDefSession.ReadingAvailable then       AddLine('NFC is not available on this device')     else     begin       NDefSession := TNDefSession.Create;       NDefSession.AlertMessage := 'Hold your iPhone near the item.';       NDefSession.OnActive := NfcActive;       NDefSession.OnDetect := NDefDetect;       NDefSession.OnError := NfcError;       NDefSession.Scan;     end;   except     on E: Exception do       AddLine('Exception: ' + E.Message);   end; end;  | 
					
The TNDefSession and TNfcTagSession objects actually are part of the NFC Library that reads and writes when NFC items for scanning are near the phone. When the OnDetect event is called the NDefSession, the code in the NDefDetect method is executed. A cycle starts, going through all the available tags. You can see this in the code below.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37  | 
						procedure TFormMain.NDefDetect(Sender: TObject; Tags: TNDefTags); var   I: Integer;   Tag: TNDefTag; begin   try     AddLine('NDEF detected: ' + IntToStr(Length(Tags)) + ' tags');     for I := 0 to Length(Tags) - 1 do     begin       Tag := Tags[I];       <em>// connect to NDEF tag</em>       NDefSession.Connect(Tag,         procedure (Error: TNfcError)         begin           if Error.IsError then           begin             ShowError(Error);             NDefSession.Invalidate('Error: ' + Error.Description);           end           else           begin             if not Tag.IsAvailable then             begin               AddLine('Tag not available');               NDefSession.Invalidate('Tag not available');             end             else               <em>// retrieve NDEF tag info</em>               Tag.QueryStatus(                 procedure (Status: TNDefStatus; Capacity: LongWord; Error: TNfcError)                 begin                   if Error.IsError then                   begin                     ShowError(Error);                 NDefSession.Invalidate('Error: ' + Error.Description);                   end                   else  | 
					
When NFC is active, a line with text is added to the TMemo with text ‘Active event’.
| 
					 1 2 3 4  | 
						procedure TFormMain.NfcActive(Sender: TObject); begin   AddLine('Active event'); end;  | 
					
To get a better vision of how this library works, check the video with the Demo in Action below.
To get the Demo and test it, please download the library from the link below.
https://winsoft.sk/download/infclib.zip
With Firemonkey’s Native Ios App Builder, you can try the Core NFC API Library in the Delphi or C++ environments. Try the free tool here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition






