Beacons in iOS
This tutorial discusses the client-side API for working with beacons in iOS.
This tutorial is a continuation of Working with Beacons (introduction, setup, and server-side API).
The following topics are covered:
- Configuring you application
- Simplified APIs for ranging/monitoring iBeacons
- Running the sample application
Configuring your application
- On iOS 8 and later, to enable Location services, add
NSLocationAlwaysUsageDescriptionin the iOS project
- Copy the
iOSNativeiBeaconsLibraryfolder of the iOSNativeiBeacons project into your native iOS application. That folder contains the library that provides the simplified set of APIs for ranging/monitoring iBeacons.
To request permission to use location services and local/push notifications, write the following code:
Simplified APIs for ranging/monitoring iBeacons
Loading beacons and triggers from the server
To load information pertaining to beacons, triggers, and their associations from the server and storing it in the application JSONStore, write the following code:
To set the monitoring/ranging of iBeacons and to fire trigger actions, write the following code:
To let the user opt out of iBeacon monitoring, write the following code:
Turning the device into an iBeacon
To turn a device into an iBeacon, write the following code:
Generally, use this feature for testing and development purposes. The UUID string must match the UUID of the beacons that are registered on server.
Increasing the application running time
To increase the running time of the application when it runs in background (as part of iBeacon monitoring), write the following code:
This code increases the running time of the application up to 3 minutes from the default 10 seconds.
Handling local notifications
To handle local notifications that originate from the application, write the following code:
Running the sample application
The sample is in two parts:
- The Beacons MobileFirst project contains a MobileFirst native API that you can deploy to your MobileFirst Server instance.
- The BeaconsObjC native project contains a native library that provides a simplified set of APIs for ranging/monitoring iBeacons and a native iOS application that demonstrates the usage of those APIs.
Make sure to update the
worklight.plistfile in iOSNativePush with the relevant server settings.
The sample showcases a simple banking scenario where actions are triggered based on proximity:
- When the user enters a Bank branch, a notification action is triggered with a Welcome message.
- When the user enters the loan processing section, an alert is triggered with a message stating that a bank personnel will assist the user soon.
- When the user exits the Bank branch, a notification action is triggered with a message.