What you will learn

This tutorial will teach you how to manage:

  • Location permission
  • Bluetooth permission

Pre-requisites - What you need to get started

Step 1: Clone the sdk-tutorial repository

  • Clone the sdk-tutorial repository
git clone https://github.com/Connecthings/sdk-tutorial.git
  • Open the iOs>beacon>9-Permission>Starter folder

Step 2: Configure the SDK

  • Configure your CocoaPod files and .plist
  • Configure your SDK with:
    • the UUID of your beacon
    • the appropriate Adtag Environment (DEMO / PREPROD / PROD / PROD_US)
    • your SDK credentials

Switch to Swift

[[[ATAdtagInitializer sharedInstance] configureUrlType:ATUrlTypeProd
                                              andLogin:@"__USER__" 
                                           andPassword:@"__PSWD__" 
                                            andCompany:@"__COMPANY__"] 
                                           synchronize];
ATAdtagInitializer.sharedInstance().configureUrlType(__UrlType__, 
                                            andLogin: "__USER__", 
                                         andPassword: "__PSWD__", 
                                          andCompany: "__COMPANY__")
                                   .synchronize();

If you need more informations, have a look to the 5 minutes quickstart tutorial

Step 3: Check the authorization status

  • Open the ViewController.h file.
  • Add the ATBeaconBleLocationStatusDelegate protocole

Switch to Swift

@interface ViewController : UIViewController<ATBeaconBleLocationStatusDelegate>
class ViewController: UIViewController, ATBeaconBleLocationStatusDelegate {
  • Open the ViewController.m file.
  • Implement the ATBeaconBleLocationStatusDelegate method

Switch to Swift

-(void)checkBleStatus:(CBCentralManagerState)bleStatus
	   locationStatus:(CLAuthorizationStatus)locationStatus{
}
func checkBleStatus(_ bleStatus: CBManagerState, locationStatus: CLAuthorizationStatus){

}

Note:

  • The CBCentralManagerState returns the Bluetooth status.
  • The CLAuthorizationStatus returns the location permission status.
  • Use the information from the delegate to notify the user about activating bluetooth or authorizing location access.

Switch to Swift

-(void)checkBleStatus:(CBCentralManagerState)bleStatus
       locationStatus:(CLAuthorizationStatus)locationStatus{

    if(locationStatus==kCLAuthorizationStatusDenied || locationStatus == kCLAuthorizationStatusRestricted){
        _labelStatus.text = NSLocalizedString(@"adtag.warning.Location.status.text", nil);
    }else if (bleStatus== CBCentralManagerStateUnauthorized || bleStatus == CBCentralManagerStatePoweredOff){
        _labelStatus.text = NSLocalizedString(@"adtag.warning.ble.status.text", nil);
    }else if(bleStatus == CBCentralManagerStateUnsupported){
        _labelStatus.text = NSLocalizedString(@"adtag.notsupported.ble.status.text", nil);
    }else{
         _labelStatus.text = NSLocalizedString(@"adtag.ready", nil);
    }

    [_labelStatus setNeedsDisplay];
}
func checkBleStatus(_ bleStatus: CBManagerState, locationStatus: CLAuthorizationStatus){
    if locationStatus == CLAuthorizationStatus.denied || locationStatus == CLAuthorizationStatus.restricted {
        self.labelStatus.text = "Please authorize the application to access to your location"
    } else if bleStatus == .unauthorized || bleStatus == .poweredOff {
        self.labelStatus.text = "Please activate bluetooth"
    } else if bleStatus == .unsupported {
        self.labelStatus.text = "The bluetooth is not supported on your phone"
    } else {
        self.labelStatus.text = "It's ready for beacon detection"
    }
    labelStatus.setNeedsDisplay()
}
  • Register the delegate on the viewDidAppear method

Switch to Swift

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[ATBeaconManager sharedInstance] registerAdtagBeaconBleLocationDelegate:self];
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    ATBeaconManager.sharedInstance().registerAdtagBeaconBleLocationDelegate(self)
}
  • Unregister the delegate in the viewDidDisappear method

Switch to Swift

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [[ATBeaconManager sharedInstance] registerAdtagBeaconBleLocationDelegate:nil];
}
override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    ATBeaconManager.sharedInstance().registerAdtagBeaconBleLocationDelegate(nil)
}

Step 4: Start testing

  1. In Xcode, click on "Play" to launch the installation process of the application you have just built on your phone

  2. Refuse access to your location

  3. The application displays: "Please authorize the application to access your location".

  4. Deactivate Bluetooth.

  5. The application displays: "Please activate bluetooth".