简体   繁体   中英

Can't detect iBeacon in iOS 8

I've followed online tutorials from AppCoda and Devfright to create an iBeacon detection app. I'm using an iBeacon from estimote, an iPad 3 with iOS 8. The app simply does not detect my iBeacon whereas its being detected by other iBeacon apps. I can't understand what I'm missing or doing wrong in my code.

Here's my .h file:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property (nonatomic, strong) CLBeaconRegion   *beaconRegion;
@property (nonatomic, strong) CLLocationManager *locManager;

@property (nonatomic, strong) IBOutlet UILabel *label1;
@property (nonatomic, strong) IBOutlet UILabel *label2;

@end

Here's my .m file:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

@end

@implementation ViewController


- (void)viewDidLoad {
[super viewDidLoad];

self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;

//default uuid for estimote beacons
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];

self.beaconRegion = [[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:@"com.rk.testregion"];

[self.locManager startMonitoringForRegion:self.beaconRegion];
[self locationManager:self.locManager didStartMonitoringForRegion:self.beaconRegion];

// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; return;
}

}

-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
// Beacon found!
self.label1.text = @"Welcome to";
self.label2.text = @"Location 1";

CLBeacon *foundBeacon = [beacons firstObject];

// retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];

NSLog(@"UUID: %@", uuid);
NSLog(@"major: %@", major);
NSLog(@"minor: %@", minor);

}

- (void)viewDidDisappear:(BOOL)animated
{
[self.locManager stopRangingBeaconsInRegion:self.beaconRegion];

[super viewDidDisappear:animated];
}

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self.locManager startRangingBeaconsInRegion:self.beaconRegion];
}

-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self.locManager stopRangingBeaconsInRegion:self.beaconRegion];
self.label1.text = @"Searching again...";
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {

[self.locManager startRangingBeaconsInRegion:self.beaconRegion];
}

@end

The delegate method didRangeBeacons simply does not get called. Could someone please let me know how to fix this.

Check out the answer below, which describes some extra hoops you now need to jump through to get this working:

Location Services not working in iOS 8

Full disclosure: I have not tried this myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM