Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning ibeacon

You're reading from   Learning ibeacon

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781784397128
Length 196 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Craig Gilchrist Craig Gilchrist
Author Profile Icon Craig Gilchrist
Craig Gilchrist
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Welcome to iBeacon FREE CHAPTER 2. Detecting Beacons – Showing an Advert 3. Broadcasting Advertisements – Sending Offers 4. Ranging Beacons – Hunting for Treasure 5. Detecting Beacons in the Background – Location Dating 6. Leaving Regions – Don't Forget Your Stuff 7. Vendor SDKs – Buying and Configuring Beacons 8. Advanced Tutorial – iBeacon Museum 9. iBeacon Security – Understanding the Risks Index

Hello world

In time-honored tradition, we can't conclude the first chapter without the traditional "Hello World" tutorial. We'll be using your Mac as an iBeacon broadcaster using the companion app, and the app will simply show an alert every time it enters or exits the region.

We will cover all of these concepts in more detail in later chapters, but for reference, here are the classes we'll be using:

  • CLLocationManager: The CLLocationManager class delivers location-related events to your app and tells you when you enter or exit a region
  • CLLocationManagerDelegate: The CLLocationManagerDelegate protocol defines the delegate methods used to receive location and heading updates from CLLocationManager
  • CLBeaconRegion: A CLBeaconRegion object defines a type of region that is based on the device's proximity to a Bluetooth beacon

Let's get started

Fire up Xcode and start a new project. Choose Single View Application from the iOS template menu as your project type.

Let's get started

A new project dialog

Set up your new project using the values shown in the following screenshot:

Let's get started

A new project options dialog

Adding the Core Location framework

All of the features we need for this app to work are present in the Core Location framework.

Click on the project in the project navigator and scroll down to the Linked Frameworks and Libraries section of the General tab and then click on the add icon. We'll need to add the CoreLocation framework as shown in the following screenshot:

Adding the Core Location framework

Adding the Core Location framework

We only really care about the LIViewController class for this tutorial as that's where we're going to be presenting to the user when we're moving in and out of regions. In order to do this, we need to add a reference to CoreLocation. Open LIViewController.h and add the following line just below the existing UIKit import:

#import <CoreLocation/CoreLocation.h>

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Adding a permission message

Since iOS 8.0, you must specify a location description message in your plist settings. This is a nice friendly message for the users to help them understand why they need permission to use those location services. To do this, open the project file, click on your target Hello World, and then under the Info tab, add a new item to the dictionary under Custom iOS Target Properties with the following values:

  • Key: NSLocationAlwaysUsageDescription
  • Value: This app needs your location to show you how cool iBeacon is.

Configuring the CLLocationManagerDelegate method

Our ViewController instance is where all the action happens, so it makes sense for ViewController to be aware of location events. For that, we need to make it CLLocationManagerDelegate.

Go ahead and add the declaration to the LIViewController interface declaration. Change the interface declaration in LIViewController.h so that it looks like the following code:

@interface LIViewController : UIViewController<CLLocationManagerDelegate>

We also need to implement the CLLocationManagerDelegate methods so that we can show our notification when the device enters a region. Add the following code to the end of the LIViewController implementation in the LIViewController.m file:

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    UIAlertView * av = [[UIAlertView alloc] init];
    av.title = [NSString stringWithFormat:@"Entered Region '%@'", region.identifier];
    [av addButtonWithTitle:@"OK"];
    [av show];
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    UIAlertView * av = [[UIAlertView alloc] init];
    av.title = [NSString stringWithFormat:@"Left Region '%@'", region.identifier];
    [av addButtonWithTitle:@"OK"];
    [av show];
}

Tip

"Wait, what's CLRegion?" I hear you asking. Well, CLBeaconRegion inherits CLRegion and so CLBeaconRegion is CLRegion. Remember that CLLocationManager is used to deliver location-related events, which don't necessarily need to come from a beacon-related activity.

Adding a CLLocationManager instance

Now, our view controller will be notified when a CLLocationManager instance receives events, but we don't have an instance of CLLocationManager yet. Add the following property to the LIViewController interface inside LIViewController.m:

@property (nonatomic, strong) CLLocationManager * locationManager;

Preparing UUID

For our app to know which region it's looking out for, we need some way of storing the UUID. Add the following line just below the implementation in LIViewController.m:

static NSString * uuid = @"EB9AB493-32C2-4E5C-BF67-76E86E338BB9";

Start monitoring

Our app is already ready to start accepting location-based updates. All we need to do now is create a region, instantiate our location manager, and start monitoring our regions. Overwrite the viewDidLoad method of our LIViewController with the following code. We'll go through the most important code shortly.

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUUID * regionUUID = [[NSUUID alloc] initWithUUIDString:uuid];
    
    CLBeaconRegion * region = [[CLBeaconRegion alloc] initWithProximityUUID:regionUUID identifier:@"My Region"];
    
    [region setNotifyOnEntry:YES];
    [region setNotifyOnExit:YES];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    [self.locationManager requestAlwaysAuthorization];

    [self.locationManager startMonitoringForRegion:region];
}

Line by line

Let's break the preceding code down line by line:

  1. First, we create an NSUUID instance using our string identifier (uuid) since CLBeaconRegion requires an object of this type in order to be initialized:
    NSUUID * regionUUID = [[NSUUID alloc] initWithUUIDString:uuid];
  2. Next, we create a new CLBeaconRegion passing in our NSUUID:
    CLBeaconRegion * region = [[CLBeaconRegion alloc] initWithProximityUUID:regionUUID identifier:@"My Region"];
  3. Next, we configure our region events. We're interested in being notified when we enter and leave the region:
    [region setNotifyOnEntry:YES];
    [region setNotifyOnExit:YES];
  4. Next, we instantiate CLLocationManager and add ViewController as its delegate:
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
  5. Finally, we request permission for location services, then start monitoring for the CLBeaconRegion we've just created:
    [self.locationManager requestAlwaysAuthorization];
    [self.locationManager startMonitoringForRegion:region];
You have been reading a chapter from
Learning ibeacon
Published in: Nov 2014
Publisher:
ISBN-13: 9781784397128
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image