Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiRAD Studio

Powerful Core NFC API Library For Delphi FireMonkey On iOS By Winsoft

time lapse photography of cars on road during night time
  1. 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.

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.

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];
      // connect to NDEF tag
      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
              // retrieve NDEF tag info
              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’.

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.

RAD Studio 13.1 Florence Now Available See What's New in RAD Studio 13.1 Delphi is 31 - Webinar Replay

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.

Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES