UniMag iOS SDK - Singleton Implementation
I'm having difficulty managing connection with the UniMag reader in my application integrated with multiple views or else retaining consistent functionality over a period of time. How can I fix this?
The UniMag SDK 7.20 sample source code demonstrates a managed auto-connect while leaving from and returning to the main view. Implementing a Singleton instance of the UniMag class across a multiple view project will enable the reader to be accessed without the established method.
In the main header class of the application, add a static method called sharedController (for example, I am using the fictional class name MainApp):
==================
MainApp.h:
______________
+(uniMag*) sharedController;
======================
Then in the main method class of the application, add the code for it, which is a static variable and the sharedController definition:
================
MainApp.m
_____________
static volatile uniMag *singletonUnimag = nil;
+(uniMag*) sharedController{
//Accesses a singleton instance of device class.
static uniMag *SharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SharedInstance = [uniMag new];
});
singletonUnimag = SharedInstance;
return SharedInstance;
}
========================
Instead of initializing the device as:
unimag* myDevice = [unimag new]; ( or [[unimag alloc] init] )
you use
unimag* myDevice = [MainApp sharedController];
With a singleton, and without any separate activation effort, even on the first time, functions such as ([MainApp sharedController] ReadCardSwipe) may be used.