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
- Launch Xcode.
- Click Create a new Xcode project.
- Select iOS, then Application, then App, then click Next.
- Enter “GetTagMessage” in the Product Name field.
- Select Storyboard from the Interface dropdown menu.
- Select Swift from the Language dropdown menu.
- Select your team name (log in via Xcode’s preferences, if necessary) and enter an organization name.
- Click Next, then select a location on your Mac to create this project, then click Create.
Configuration
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.
Turn on Near Field Communication Tag Reading under the Capabilities tab for the project’s target.
Select GetTagMessage (GetTagMessage.xcodeproj).
Select Target GetTagMessage.
Select the Signing & Capabilities tab.
Click + Capability.
Search “Near Field Communication Tag Reading” and add this Capability to the project.
Add CoreNFC.framework and CertiScanSDK.framework to the “Frameworks, Libraries, and Embedded Content” for the project’s target.
Select the General tab.
Find "Frameworks, Libraries, and Embedded Content". The CertiScanSDK.framework should be already in it.
Click + and search “CoreNFC.framework”. Then add it.
Add properties to Info of the project’s target for NFC usage.
Select the Info tab.
Expand Custom iOS Target Properties.
Add “Privacy - NFC Scan Usage Description” and write your description for NFC scan usage.
Add “ISO7816 application identifiers for NFC Tag Reader Session” and set “D2760000850101” to the value of Item 0.
Implementing Get Tag Message (Swift)
Open ViewController.swift and import CertiScanSDK.
// ViewController.swift
// GetTagMessage
import UIKit
import CertiScanSDK // import CertiScanSDK framework
class ViewController: UIViewController {
}
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),
])
}
Add a tap event to this button.
btnGetTagMessage.addTarget(self,
action: #selector(self.btnGetTagMessagePressed(_:)),
for: .touchUpInside)
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)
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.
- Create a new file and select Cocoa Touch Class, click Next.
- Enter “ObjectiveCViewController” in the Class field.
- Select UIViewController from the Subclass of dropdown menu.
- Select Objective-C from the Language dropdown menu, then click Next.
- Select a location, and then click Create.
- Then, Xcode will ask you if you want to create a bridging header. Click Create Bridging Header to create the “GetTagMessage-Bridging-Header.h”.
- Import “ObjectiveCViewController” to “GetTagMessage-Bridging-Header.h”
#import "ObjectiveCViewController.h"
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
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];
}Add a tap event to this button.
[btnGetTagMessageOC addTarget:self
action:@selector(getTagMessage:)
forControlEvents:UIControlEventTouchUpInside];
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);
}];
}
@endSet 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.
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
}