Apple’s Swift Based iBeacon App Development with CoreLocation For iOS 7/8

Charles Darwin
SynapseIndia
Published in
4 min readJun 3, 2016

Now-a-days, a large number of users prefer using Apple’s new programming language Swift to develop their iBeacon applications. For enhanced development, the Apple’s developers are preferring using Swift, Xcode 6, and iOS 8.

In the last few years, the iBeacon app development has increased on a wide scale, which has provided a vast opportunity for the developers to learn about iBeacon technology and build their significant app through using it. If you’re a iOS developer, you can easily and efficiently develop a Swift based iBeacon App. Below are some essential steps, which developers need to follow for developing a valuable Swift-based iBeacon Template Application.

1. Create a Swift Project

In the first step, from the File menu, select New->Project. Then select IOS Application from the left menu, and Single View Application from the options on the right. Then click Next.

2. Configure Project Options

In the next step, configure a project name, organization name, and organization identifier. Also, make sure that you select Swift as the language, and select iPhone as the device.

3. Add the CoreLocation Framework To The Project

In the next step, click on the main project entry in the file browser. Then make a selection of the General tab. Then scroll to the bottom and click the + to add a Linked Framework. Once you add on CoreLocation.framework, you can find it in the list. Simply select it, and click the Add button.

However, in the General settings, into the Deployment target if you want to change the app older versions of iOS. 6.x into 7.0 version, as it won’t work properly, as well as don’t have the necessary CoreLocation functionality and can’t perform with Swift.

4. Configure Background Capabilities

To perform background ranging and monitoring, and for receiving notifications, it is necessary that the app use Location updates and Bluetooth LE accessories in the background. To implement this, click the main project entry in the file browser. Select the Capabilities tab and Background Modes, and turn them switch ON, and later check Location updates and uses Bluetooth LE accessories.

5. Define A Region To Be Monitored

In this step, we will set things up in the application:didFinishLaunchingWithOptions method in AppDelegate.swift. Initially, we need to import the CoreLocation header, and then we will set up a CLBeaconRegion with the UUID for our beacon(s). A UUID is an identifier that identifies a beacon or set of beacons.

6. Set Up Plist Entry For LocationAlways

For iOS 8 requires special permission for always-on location monitoring, which also includes iBeacon ranging. So, you need to add a specific plist entry that enables iOS to inform the user why they need this permission.

In this step, enter in the file browser under Supporting Files, open Info.plist. Then select the top row “Information Property List”. To add a Row, just click on the small plus sign. Give it the key (in the left column) NSLocationAlwaysUsageDescription. Then set type to String, and then set its value to whatever string you’d like to use to inform the user, when they are prompted for location permissions.

7. Set up CLLocationManager for Foreground/Background Monitoring and Ranging

After region defined, we need to instruct the device to monitor it. For this, we need to create an instance of CLLocationManager, and instruct it to start monitoring the region we just created. We also need to define a “delegate”, an object that gets notified whenever something happens with the location manager.

In this step, you need to import CoreLocation headers. Then you need to instruct the compiler that our AppDelegate class will implement the CLLocationManagerDelegate protocol, and declare a property to hold our CLLocationManager object, so that it survives for the entire lifetime of the application. Later, inside the didFinishLaunchingWithOptions method, you need to create the CLLocationManager instance. Then you request “Always” authorization for location information. As this is a new method in iOS 8, so you need to first check to see if the CLLocationManager responds to the selector and or not.

8. Clean Up

As background ranging implemented with background notifications, you will get a lot of redundant alerts, approx one per second, so we need to modify things to only alert us when the range changes. For this, you need to add a place to store the last proximity reading. Then add the following property to the top of AppDelegate.swift, just below the declaration of the window and locationManager. If the proximity hasn’t changed, or is temporarily unknown, it doesn’t re-alert us.

9. Foreground Display

Once the background mode work done properly, then you need to focus on the app foregrounded. In the app foregrounded, we have a plain white screen and no indication of what’s happening. Let’s fix that by adding a UITableView and populating it with information about all of the beacons in range.

Following the step, just open the Main.storyboard file. Then select the main view to give it focus. Then, in the list, find a Table View. Simply drag it onto the view and position and scale it so that it takes up the entire view except the area taken up by the status bar at the top.

Conclusion

By following the above mentioned points, you can build a iBeacon app using Swift, which can easily range beacons in the foreground and background, as well as display the data and push alerts. Now, you know how iBeacon can be used with iOS 7/8. But, if you are an Android developer, and want to use iBeacon, then with some study and guidance you can easily create an app by iBeacon Implementation on iOS.

--

--