This repo demonstrates a barcode scanning iOS native service and a web app utilizing that service.
It is assumed you already have a Predix Mobile sevice installation, have installed the Predix Mobile pm command line tool, and have installed a Predix Mobile iOS Container, following the Getting Started examples for those repos.
It is also assumed you have a basic knowledge of mobile iOS development using XCode and Swift.
To get started, follow this documentation:
- [Get Started with the Mobile Service and Mobile SDK] (https://www.predix.io/docs#rae4EfJ6)
- [Running the Predix Mobile Sample App] (https://www.predix.io/docs#EGUzWwcC)
- [Creating a Mobile Hello World Web App] (https://www.predix.io/docs#DrBWuHkl)
- Add the
BarcodeScannerService.swiftfile from this repo to your container project. - Open your Predix Mobile container app project.
- In the Project Manager in left-hand pane, expand the PredixMobileiOSApp project, then expand the PredixMobileiOSApp group. Within that group, expand the Classes group.
- In this group, create a group called "Services".
- Add the file
BarcodeScannerService.swiftto this group, either by dragging from Finder, or by using the Add Files dialog in XCode. When doing this, ensure the BarcodeScannerService.swift file is copied to your project, and added to your PredixMobileiOSApp target. - Add the "Privacy - Camera Usage Description" key to your Predix Mobile container app's Info.plist file with a string value explaining to the user how the app uses this data. for example you could use "Needed for bar code scanning functionality."
The BarcodeScannerService.swift file contains all the code needed for our example service, but you must register the service in the container in order for it to be available to your web app. Add a line of code to AppDelegate.
- In the
AppDelegate.swiftfile, navigate to the application: didFinishLaunchingWithOptions: method. In this method, you will see a line that looks like this:
PredixMobilityConfiguration.loadConfiguration()
- Directly after that line, add the following:
PredixMobilityConfiguration.additionalBootServicesToRegister.append(BarcodeScannerService.self)
This informs the iOS Predix Mobile SDK framework to load your new service when the app starts, thus making it available to your web app.
The Swift files you added to your container are heavily documented. Read through these for a full understanding of how they work, and what they are doing.
The comments take you through creating an implemenation of the ServiceProtocol protocol, handling requests to the service with this protocol, and returning data or error status codes to callers.
Your new iOS client service is exposed through the service identifier "barcodescanner". So calling http://pmapi/barcodescanner from a web app will call this service.
A simple demo web app is provided in the demo-webapp directory in the git repo.