Skip to main content

CertiScan iOS SDK Tutorial

Overview

In this tutorial, we will guide you through writing a basic iOS app using the CertiScan SDK. This app will be able to retrieve data from a Med-ic package or eCAP.

Create a new Xcode project

  1. Launch Xcode.
  2. Click Create a new Xcode project.
  3. Select iOS, then Application, then App, then click Next.
  4. Enter “GetTagMessage” in the Product Name field.
  5. Select Storyboard from the Interface dropdown menu.
  6. Select Swift from the Language dropdown menu.
  7. Select your team name (log in via Xcode’s preferences, if necessary) and enter an organization name.
  8. Click Next, then select a location on your Mac to create this project, then click Create.

Configuration

  1. First, we have to add CertiScanSDK.framework to the project. You can put it in any location under the project. In order to easily find it, let’s create a New Group "frameworks" in GetTagMessage. Copy the CertiScanSDK.framework to "frameworks" and add it to the project. Make sure Copy items if needed is selected.

    Add Frameworks

  2. Turn on Near Field Communication Tag Reading under the Capabilities tab for the project’s target.

    1. Select GetTagMessage (GetTagMessage.xcodeproj).

    2. Select Target GetTagMessage.

    3. Select the Signing & Capabilities tab.

    4. Click + Capability.

      Capability

    5. Search “Near Field Communication Tag Reading” and add this Capability to the project.

      NFCTR1

      NFCTR2

  1. Add CoreNFC.framework and CertiScanSDK.framework to the “Frameworks, Libraries, and Embedded Content” for the project’s target.

    1. Select the General tab.

    2. Find "Frameworks, Libraries, and Embedded Content". The CertiScanSDK.framework should be already in it.

    3. Click + and search “CoreNFC.framework”. Then add it.

      frameworks

  2. Add properties to Info of the project’s target for NFC usage.

    1. Select the Info tab.

    2. Expand Custom iOS Target Properties.

    3. Add “Privacy - NFC Scan Usage Description” and write your description for NFC scan usage.

    4. Add “ISO7816 application identifiers for NFC Tag Reader Session” and set “D2760000850101” to the value of Item 0.

      frameworks

Implementing Get Tag Message (Swift)

  1. Open ViewController.swift and import CertiScanSDK.

    //  ViewController.swift
    // GetTagMessage

    import UIKit
    import CertiScanSDK // import CertiScanSDK framework

    class ViewController: UIViewController {

    }

  1. Add a button to the top of the screen.

    override func viewDidLoad() {
    super.viewDidLoad()

    // add buttons
    let btnGetTagMessage = UIButton.init(type: .system)
    btnGetTagMessage.setTitle("Get Tag Message (Swift)", for: .normal)
    btnGetTagMessage.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)

    self.view.addSubview(btnGetTagMessage)
    btnGetTagMessage.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
    btnGetTagMessage.widthAnchor.constraint(equalTo: self.view.widthAnchor),
    btnGetTagMessage.heightAnchor.constraint(equalToConstant:44),
    btnGetTagMessage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    btnGetTagMessage.topAnchor.constraint(equalTo: view.topAnchor,constant: 150),
    ])
    }

  1. Add a tap event to this button.

    btnGetTagMessage.addTarget(self,
    action: #selector(self.btnGetTagMessagePressed(_:)),
    for: .touchUpInside)
  1. Implement Get Tag Message in btnGetTagMessagePressed(_:)).

    @objc func btnGetTagMessagePressed(_ sender: AnyObject){
    NFCAPIManager.getTagMessage { responseJsonString in
    if let output = responseJsonString {
    print("output = \(output)")
    }
    }
    }

    Now the complete code of ViewController.swift:

    //
    // ViewController.swift
    // GetTagMessage
    //

    import UIKit
    import CertiScanSDK // import CertiScan SDK

    class ViewController: UIViewController {

    override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.white
    self.title = "CertiScan SDK"

    // add buttons
    let btnGetTagMessage = UIButton.init(type: .system)
    btnGetTagMessage.setTitle("Get Tag Message (Swift)", for: .normal)
    btnGetTagMessage.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)

    self.view.addSubview(btnGetTagMessage)
    btnGetTagMessage.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
    btnGetTagMessage.widthAnchor.constraint(equalTo: self.view.widthAnchor),
    btnGetTagMessage.heightAnchor.constraint(equalToConstant:44),
    btnGetTagMessage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    btnGetTagMessage.topAnchor.constraint(equalTo: view.topAnchor,constant: 150),
    ])


    // add tap events
    btnGetTagMessage.addTarget(self,
    action: #selector(self.btnGetTagMessagePressed(_:)),
    for: .touchUpInside)
    }

    @objc func btnGetTagMessagePressed(_ sender: AnyObject){
    NFCAPIManager.getTagMessage { responseJsonString in
    if let output = responseJsonString {
    print("output = \(output)")
    }
    }

    }
    }

Implementing Get Tag Message (Objective-C)

  1. If you want to write your app with Objective-C, CertiScan iOS SDK also supports it. Let’s create a new UIViewController and select Objective-C as its language.

    1. Create a new file and select Cocoa Touch Class, click Next.
    2. Enter “ObjectiveCViewController” in the Class field.
    3. Select UIViewController from the Subclass of dropdown menu.
    4. Select Objective-C from the Language dropdown menu, then click Next.
    5. Select a location, and then click Create.
    6. Then, Xcode will ask you if you want to create a bridging header. Click Create Bridging Header to create the “GetTagMessage-Bridging-Header.h”.
    7. Import “ObjectiveCViewController” to “GetTagMessage-Bridging-Header.h”
    #import "ObjectiveCViewController.h"
  2. Open ObjectiveCViewController.m and import CertiScanSDK.

    #import "ObjectiveCViewController.h"
    #import <CertiScanSDK/CertiScanSDK-Swift.h> // import CertiScanSDK header

    @interface ObjectiveCViewController ()

    @end

    @implementation ObjectiveCViewController

    - (void)viewDidLoad {

    }
    @end
  1. Add a button to the top of the screen.

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *btnGetTagMessageOC = [[UIButton alloc] initWithFrame:CGRectMake(0,
    120,
    self.view.frame.size.width,
    44)];
    [btnGetTagMessageOC setTitle:@"Get Tag Message (Objective-C)" forState:UIControlStateNormal];
    btnGetTagMessageOC.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [btnGetTagMessageOC setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
    [self.view addSubview:btnGetTagMessageOC];

    }
  2. Add a tap event to this button.

    [btnGetTagMessageOC addTarget:self
    action:@selector(getTagMessage:)
    forControlEvents:UIControlEventTouchUpInside];
  1. Implement Get Tag Message.

    - (IBAction)getTagMessage:(id)sender {
    [NFCAPIManager getTagMessageWithCompletionHandler:
    ^(NSString * _Nullable responseJsonString) {
    NSLog(@"output=%@", responseJsonString);
    }];
    }

    Now the complete code of ObjectiveCViewController.m is:

    //
    // ObjectiveCViewController.m
    // GetTagMessage
    //

    #import "ObjectiveCViewController.h"
    #import <CertiScanSDK/CertiScanSDK-Swift.h> // import CertiScanSDK header

    @interface ObjectiveCViewController ()

    @end

    @implementation ObjectiveCViewController

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *btnGetTagMessageOC = [[UIButton alloc] initWithFrame:CGRectMake(0,
    120,
    self.view.frame.size.width,
    44)];
    [btnGetTagMessageOC setTitle:@"Get Tag Message (Objective-C)" forState:UIControlStateNormal];
    btnGetTagMessageOC.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [btnGetTagMessageOC setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];

    [self.view addSubview:btnGetTagMessageOC];

    [btnGetTagMessageOC addTarget:self
    action:@selector(getTagMessage:)
    forControlEvents:UIControlEventTouchUpInside];


    }

    - (IBAction)getTagMessage:(id)sender {
    [NFCAPIManager getTagMessageWithCompletionHandler:
    ^(NSString * _Nullable responseJsonString) {
    NSLog(@"output=%@", responseJsonString);
    }];
    }


    @end


  2. Set ObjectiveCViewController as the Initial View Controller. In SceneDelegate.swift, set ObjectiveCViewController to the rootViewController.

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }

    window?.rootViewController = ObjectiveCViewController() // set “ObjectiveCViewController” to the “rootViewController”.

    }
    }

Run and Test

Select an iOS device and run GetTagMessage. Tapping the Get Tag Message button will start the NFC reader session.

frameworks frameworks frameworks

Scan a Medic or eCAP package and you will see the output in the Console.

output={
"tag_message" : {
"package_id" : "My Package ID",
"patient_id" : "Patient_1",
"tag_id" : "500465364-PR2",
"event_count" : 3,
"custom_data_1" : "My Custom Data A",
"scan_time" : 1626789791,
"started" : true,
"clock_coefficient_correction" : true,
"tag_events" : [
{
"timestamp" : 1623862993,
"type" : 1
},
{
"timestamp" : 1623862993,
"type" : 2
},
{
"timestamp" : 1623862993,
"type" : 6
}
],
"versioning" : {
"hardware_version" : "1.0.1",
"firmware_version" : "1.1.1.0.0.4.1",
"model" : "Med-ic"
}
},
"command_code" : 1,
"success" : true
}