Tutorial 1: Use the built-in Scanner - iOS

In this tutorial, I will walk you through how to use the builtin scanner. The scanner opens the camera with a scanning overlay animation indicating the app is actively querying for a match. Once a match is found, result is returned with a URL payload which is opened by the builtin web scanner. As to how to install and initialise SDK, please refer to 'How to Use SDK Quick Start'

1. Create a new app

Create new Xcode project with any name you want, for example, AIQKit-Example. Project type here we use is Single View App,

2. Background and Button

In the ViewController.m, find the function,

ViewController.m

- (void)viewDidLoad

In this function, we change background colour:

Code

[self.view setBackgroundColor:UIColorFromRGB(0xf6921e)];

and then add a button to invoke the builtin scanner,

Code

// add button

UIButton *scannerButton = [UIButton buttonWithType:UIButtonTypeCustom];

scannerButton.frame = CGRectMake(padding, 260.0 + padding, self.view.frame.size.width - 2*padding, 44.0);

scannerButton.backgroundColor = [UIColor whiteColor];

[scannerButton setTitleColor:UIColorFromRGB(0xf6921e) forState:UIControlStateNormal];

scannerButton.layer.cornerRadius = 16.0;

[scannerButton setTitle:btnTitle forState:UIControlStateNormal];

[scannerButton addTarget:self action:btnClicked forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:scannerButton];

In above code, we installed a callback function for button click event, which is called btnClicked, then add this button to view.

3. Add Scanner

To initiate visual search, add the following code to an appropriate view controller, in our example, it should be ViewController.m

ViewController.m

iQScannerViewController *scannerViewController = [[iQScannerViewController alloc] init];

scannerViewController.delegate = self;

[self presentViewController:scannerViewController animated:NO completion:nil];

4. Process response

When the search is complete, you will get a callback via the iQScannerViewControllerDelegate protocol:

ViewController.m

(void)scannerViewController:(iQScannerViewController *)scannerViewController didLoadSearchResponse:(iQAPISearchResponse *)searchResponse
{    
    [self dismissViewControllerAnimated:YES completion:^{
                NSLog(@"Payload URL: %@", searchResponse.payload);
    }];
}

In the callback, the iQAPISearchResponse object has a payload property which is the URL returned by the search. Use this URL, typically to load in a web view or to open the AIQ app.

5. Test the app

You can build your own scanner app based on the above steps or you can always check our source code at https://github.com/IQNECT-ORG/aiqkit-ios

Download example project and execute command,

Code


$> pod install
                    

After the above command is executed, cocoapods libraries will be downloaded and installed to project, which can be seen in Pods folder.

Open workspace in Xcode, choose scheme – FromScanner,

And build or [CMD]+B

Connect your device and run.

When app is running, you're supposed to see following,