didRangeBeacons not getting uuid in ios - ios

I used the following code to get message from a particular beacon but it always shows null in uuid. When i run this code the "viewdidLoad method works fine but when it comes to didRangebeacons it shows only the last uuid beacons. Actually i want to check that Is beacon available for particular uuid and receive that signals and keep on checking for uuids in array and when it receives another signal it should display that beacon signal also.
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
NSLog(#"Before assigning UUID");
NSArray *uuidArr = [NSArray arrayWithObjects: #"9146947B-441D-4116-B60E-4DD4659825AB", #"9146947B-441D-4116-B60E-4DD46598257C", #"9146947B-441D-4116-B60E-4DD46598257B", nil];
for(int i=0; i<[uuidArr count]; i++)
{
NSString *uuidTemp = [uuidArr objectAtIndex:i];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidTemp];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.gimbal.iosexample"];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
NSString *msg = [NSString stringWithFormat:#"Monitoring not available for %#", uuidTemp];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Monitoring not available" message:msg delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show]; return;
}
}
}
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
NSLog(#"iBeacons found");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Successfully found" message:nil delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
self.statusLabel.text = #"iBeacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];
NSLog(#"UUID: %#", uuid);
}
#end
Can anyone help me to do this.
Thanks in advance.

You are creating different beacon regions in your for loop, but your region identifier is the same, so the second region definition will replace the first, and then the second will be replaced by the third. You need to use a unique region identifier - something like
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:[NSString stringWithFormat:#"com.gimbal.iosexample.%d",i]];

Related

How to scan Beacons in IOS?

I am developing an app to search nearest beacon device but not able to calling any delegate, Already I have beacons available but not finding any beacons.
Also I did not understand NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"3863C90B-1BA6-4A4E-ADD2-D64FF1286898"]; is static is passed or not?
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"3863C90B-1BA6-4A4E-ADD2-D64FF1286898"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.jss.myidentifire"];
self.myBeaconRegion.notifyOnEntry = YES;
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startUpdatingHeading];
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
[locationManager requestStateForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside)
{
[self locationManager:locationManager didEnterRegion:region];
}
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
// We entered a region, now start looking for our target beacons!
self.statusLabel.text = #"Finding beacons.";
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"BeaconDetail" message:#"DidEnterRegion Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
// Exited the region
self.statusLabel.text = #"None found.";
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"BeaconDetail" message:#"didExitRegion Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
self.statusLabel.text = #"Beacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];
NSString *accuracy = [NSString stringWithFormat:#"%f", foundBeacon.accuracy];
accuracy = [NSString stringWithFormat:#"%f", foundBeacon.accuracy];
if (foundBeacon.proximity == CLProximityUnknown) {
self.distanceLabel.text = #"Unknown Proximity";
} else if (foundBeacon.proximity == CLProximityImmediate) {
self.distanceLabel.text = #"Immediate";
} else if (foundBeacon.proximity == CLProximityNear) {
self.distanceLabel.text = #"Near";
} else if (foundBeacon.proximity == CLProximityFar) {
self.distanceLabel.text = #"Far";
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:uuid forKey:#"uuid"];
[dict setObject:major forKey:#"major"];
[dict setObject:minor forKey:#"minor"];
[dict setObject:accuracy forKey:#"accuracy"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"BeaconDetail" message:[NSString stringWithFormat:#"%#",dict] delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
Make sure you have obtained permission to scan for beacons by adding the following:
if([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
For the above to work, you also need to add a new key to your Info.plist file: NSLocationAlwaysUsageDescription with a value set to something like "This app wants to scan for beacons"
if you start the receiver when it's already in the beacon range, it's not going to fire. Just checking that you're walking far away from the beacon and then walking back into range to get it to fire?

Trouble loading in app purchases

I implemented in app purchases in my iOS app a while ago and they worked well for a while, but now on iOS 9 they do not work. The price does not load and then when I click the button to make the purchase, I get an alert that says "No Internet Connection". My connection is fine and it should work.
I apologize for the extremely convoluted code. I wrote this early on when I started learning programming and haven't gotten around to cleaning it up a bit.
Implementation:
I have my product ids defined in my AppDelegate as so:
#define kRemoveAdsProductIdentifier #"0991"
#define kDoubleEarningRate #"199"
#define kQuadDogecoins #"399"
#define kMegaDogecoins #"1999"
#define kAll #"5991"
kProductIdentifiers = [NSArray arrayWithObjects:kRemoveAdsProductIdentifier, kDoubleEarningRate, kQuadDogecoins, kAll, kMegaDogecoins, nil];
In IAPView.m:
-(void)didLoadFromCCB
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
SKProductsRequest *productsRequest = [[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithArray:app.kProductIdentifiers]];
productsRequest.delegate = self;
[productsRequest start];
}
When purchase button is clicked:
-(void)removeAds{
if([SKPaymentQueue canMakePayments]){
if (validProduct != nil) {
NSLog(#"User can make payments");
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
app.appProduct = validProduct;
[app purchase:app.appProduct];
NSLog(#"Product ID: %#", app.appProduct.productIdentifier);
} else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"No Internet Connection" message:#"You must be connected to the internet to purchase this product." delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil, nil];
[alert show];
}
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"You do not have the ability to make payments." delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil, nil];
[alert show];
}
}
Product request:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
validProduct = nil;
doubleEarningProduct = nil;
quadrupleDogecoins = nil;
allOfTheAbove = nil;
megaDoge = nil;
NSInteger count = [response.products count];
NSLog(#"Products.count = %li", (long)count);
if(count > 0){
validProduct = [response.products objectAtIndex:0];
doubleEarningProduct = [response.products objectAtIndex:1];
quadrupleDogecoins = [response.products objectAtIndex:3];
megaDoge = [response.products objectAtIndex:2];
allOfTheAbove = [response.products objectAtIndex:4];
_numberFormatter = [[NSNumberFormatter alloc] init];
[_numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_numberFormatter setLocale:validProduct.priceLocale];
NSString *formattedPrice = [_numberFormatter stringFromNumber:validProduct.price];
formattedStringFinal = formattedPrice;
[adsPrice setString:formattedPrice];
formattedPrice = [_numberFormatter stringFromNumber:doubleEarningProduct.price];
formattedStringFinal = formattedPrice;
[doubleEarningLabel setString:formattedPrice];
formattedPrice = [_numberFormatter stringFromNumber:quadrupleDogecoins.price];
formattedStringFinal = formattedPrice;
[quadDogeLabel setString:formattedPrice];
formattedPrice = [_numberFormatter stringFromNumber:allOfTheAbove.price];
formattedStringFinal = formattedPrice;
[aboveLabel setString:formattedPrice];
formattedPrice = [_numberFormatter stringFromNumber:megaDoge.price];
formattedStringFinal = formattedPrice;
[megaDogeLabel setString:formattedPrice];
}
else if(!validProduct){
NSLog(#"No products available");
}
}
For some reason count gets output as 0. I got the feeling that it might be due to an error with provisioning profiles, and sure enough my provisioning profile for IAP was expired. I updated it and added it to XCode but the prices still do not load.
It works perfectly fine on my iPad (iOS 8.1), but not not on my iPhone (iOS 9).
Any ideas?

Getting user location not working?

I am new to the MapView topic.Now I am working on the map view.I am getting the san Francisco location longitude and latitude values.I am testing in the simulator.It is not showing the current location longitude and latitude values.
With the help of this tutorial http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/ I am developing the app.
In AppDelegate file I wrote the following code like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:#""
message:#"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:#""
message:#"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
self.locationTracker = [[LocationTracker alloc]init];
[self.locationTracker startLocationTracking];
//Send the best location to server every 60 seconds
//You may adjust the time interval depends on the need of your app.
NSTimeInterval time = 60.0;
self.locationUpdateTimer =
[NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:#selector(updateLocation)
userInfo:nil
repeats:YES];
}
return YES;
}
I have imported Location Tracker class in my ViewController
and I wrote the following code to get the location name and addresss
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
[geoCoder reverseGeocodeLocation:self.appDelgate.locationTracker.myLastLocation_ completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:#"%# %#",placemark.subLocality,placemark.subAdministrativeArea];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:#"address"];
}
}];
Now my problem is that it is not going inside block.So that I am getting the "returnAddress" as (null).
I wrote like this even though it is not coming
- (void)updateLocationToServer {
NSLog(#"updateLocationToServer");
// Find the best location from the array based on accuracy
NSMutableDictionary * myBestLocation = [[NSMutableDictionary alloc]init];
for(int i=0;i<self.shareModel.myLocationArray.count;i++){
NSMutableDictionary * currentLocation = [self.shareModel.myLocationArray objectAtIndex:i];
if(i==0)
myBestLocation = currentLocation;
else{
if([[currentLocation objectForKey:ACCURACY]floatValue]<=[[myBestLocation objectForKey:ACCURACY]floatValue]){
myBestLocation = currentLocation;
}
}
}
NSLog(#"My Best location:%#",myBestLocation);
NSLog(#"latitude %#",[myBestLocation valueForKey:#"latitude"]);
NSLog(#"longitude %#",[myBestLocation valueForKey:#"longitude"]);
self.DICT=[NSDictionary dictionaryWithDictionary:myBestLocation];
//If the array is 0, get the last location
//Sometimes due to network issue or unknown reason, you could not get the location during that period, the best you can do is sending the last known location to the server
if(self.shareModel.myLocationArray.count==0)
{
NSLog(#"Unable to get location, use the last known location");
self.myLocation=self.myLastLocation;
self.myLocationAccuracy=self.myLastLocationAccuracy;
}else{
CLLocationCoordinate2D theBestLocation;
theBestLocation.latitude =[[myBestLocation objectForKey:LATITUDE]floatValue];
theBestLocation.longitude =[[myBestLocation objectForKey:LONGITUDE]floatValue];
self.myLocation=theBestLocation;
self.myLocationAccuracy =[[myBestLocation objectForKey:ACCURACY]floatValue];
}
NSLog(#"Send to Server: Latitude(%f) Longitude(%f) Accuracy(%f)",self.myLocation.latitude, self.myLocation.longitude,self.myLocationAccuracy);
//TODO: Your code to send the self.myLocation and self.myLocationAccuracy to your server
//After sending the location to the server successful, remember to clear the current array with the following code. It is to make sure that you clear up old location in the array and add the new locations from locationManager
[self.shareModel.myLocationArray removeAllObjects];
self.shareModel.myLocationArray = nil;
self.shareModel.myLocationArray = [[NSMutableArray alloc]init];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
self.locationActual = [[CLLocation alloc]initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
//CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
// __block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:#"%# %#",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:#"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"" message:returnAddress delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}
What mistake i have done here.
Can anyone please help to clear this confusion.
Thanks in Advance.
Are you sure you are using the return address after completion block? I have used the above code and its working fine.
Here you can download the sample code
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:12.92243 longitude:80.23893];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:#"%# %#",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:#"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"" message:returnAddress delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
//If you try to access retunAddress here you will get nil.
OUTPUT
Go to iOS Simulator -> Debug -> Location -> Custom location... And enter your long/lat coordinates.
In the simulator there is no way to get the actual GPS based data so you need to simulate is with your lat-long which you can set by going
Debug -> Location -> Custom Location
and set your values there.
Probably you are simulating the location in the simulator. In your xCode's console panel there is an arrow button. Click that button and select "Don't simulate location". For reference, see the image.
If that doesn't resolve the problem then run the application. Go into the Debug menu of the simulator and choose the "Custom location" option as shown in the image.

didEnterRegion and startRangingForBeacons not being called

I've been having trouble figuring out why startRangingBeaconsInRegion is never called. I know for certain the startMonitoringForRegion is called, and I tried outputting the mRegionsArray as a string and it worked. But the didEnterRegion is not being called however. And I tried walking around back and forth trying to get a signal from my beacons (ie, entering the region), but no luck. I can't wrap my head around what might be wrong, went through a lot of questions on here and none of them mirrored my issue.
I have a Beacons table view and each cell is supposed to contain information (major, minor) on each beacon. Except, these cells aren't being filled because the ranging is not happening. :( I even tried to change it so it only detects one beacon. I know the problem doesn't lie within the Beacon class I created because the loadTestData() function works...
If anyone can help, it would be much appreciated.
BeaconTableViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface BeaconTableViewController : UITableViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLBeaconRegion *beaconRegion;
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
BeaconTableViewController.m
#import "BeaconTableViewController.h"
#import "Beacon.h"
#import "BeaconTableViewCell.h"
#interface BeaconTableViewController () <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *beaconsTableView;
#property (strong, nonatomic) NSMutableArray *beacons;
#end
#implementation BeaconTableViewController
- (void)loadTestData {
self.beacons = [[NSMutableArray alloc] init];
Beacon *beacon1 = [[Beacon alloc] init];
beacon1.major = [[NSNumber alloc] initWithInt:21311];
beacon1.minor = [[NSNumber alloc] initWithInt:21331];
[self.beacons addObject:beacon1];
Beacon *beacon2 = [[Beacon alloc] init];
beacon2.major = [[NSNumber alloc] initWithInt:10011];
beacon2.minor = [[NSNumber alloc] initWithInt:10012];
[self.beacons addObject:beacon2];
Beacon *beacon3 = [[Beacon alloc] init];
beacon3.major = [[NSNumber alloc] initWithInt:65535];
beacon3.minor = [[NSNumber alloc] initWithInt:30136];
[self.beacons addObject:beacon3];
[self.beaconsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.beacons.count-1 inSection:0];
[self.beaconsTableView insertRowsAtIndexPaths:#[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.beaconsTableView endUpdates];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:#"AB Region"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
UIAlertView *alertMonitoring = [[UIAlertView alloc] initWithTitle:#"User Notification"
message:#"Started monitoring for region."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertMonitoring show];
NSSet *mRegions = [self.locationManager monitoredRegions];
NSArray *mRegionsArray = [mRegions allObjects];
NSString *str = [mRegionsArray componentsJoinedByString:#","];
UIAlertView *alertRegion = [[UIAlertView alloc] initWithTitle:#"User Notification"
message:str
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertRegion show];
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
UIAlertView *alertRanging = [[UIAlertView alloc] initWithTitle:#"User Notification"
message:#"Started ranging."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertRanging show];
CLBeacon *foundBeacon = [beacons firstObject];
Beacon *beacon;
beacon.major = foundBeacon.major;
beacon.minor = foundBeacon.minor;
UIAlertView *alertBeaconFound = [[UIAlertView alloc] initWithTitle:#"User Notification"
message:[[[#"Major: " stringByAppendingString:[NSString stringWithFormat:#"%#", beacon.major]] stringByAppendingString:#", Minor: "] stringByAppendingString:[NSString stringWithFormat:#"%#", beacon.minor]]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertBeaconFound show];
[self.beacons addObject:beacon];
[self.beaconsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.beacons.count-1 inSection:1];
[self.beaconsTableView insertRowsAtIndexPaths:#[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.beaconsTableView endUpdates];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"User Notification"
message:#"Did enter region."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}
- (void)viewDidLoad {
[super viewDidLoad];
// [self loadTestData];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self initRegion];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.beacons count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BeaconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BeaconCell" forIndexPath:indexPath];
Beacon *beacon = [self.beacons objectAtIndex:indexPath.row];
cell.beacon = beacon;
return cell;
}
Under iOS 8, Apple added some new requirements to using the location manager (And iBeacons are a Location Manager function)
You have to add the keys NSLocationAlwaysUsageDescription and/or NSLocationWhenInUseUsageDescription to your info.plist file, and then before trying to start monitoring beacons you have to check the authorization status and if it is kCLAuthorizationStatusNotDetermined, you have to make a new call, either requestAlwaysAuthorization or requestWhenInUseAuthorization
The code might look something like this:
CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
if (status ==kCLAuthorizationStatusDenied)
{
NSLog(#"Location manager denied");
}
theLocManager = [[CLLocationManager alloc] init];
theLocManager.delegate = self;
if (status == kCLAuthorizationStatusNotDetermined
&& [theLocManager respondsToSelector: #selector(requestAlwaysAuthorization)])
[theLocManager requestAlwaysAuthorization];
(You have to add the check to make sure the location manage responds to the requestAlwaysAuthorization or requestWhenInUseAuthorization method, since they are only available in iOS >= 8.)
The thing I don't like about this OS change is that if you don't make the request call, your calls to start monitoring beacons fail silently.

iBeacon receiver app not working some devices

Thanks in advance
I have IBeacon Broadcaster and receiver sample apps for ios. But Ibeacon receiver not working in some devices.
Here is my code
enter code here- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"A77A1B68-49A7-4DBF-914C-760D07FBB87B"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.appcoda.testregion"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
// 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 didEnterRegion:(CLRegion *)region{
// We entered a region, now start looking for our target beacons!
self.statusLabel.text = #"Finding beacons.";
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region{
// Exited the region
self.statusLabel.text = #"None found.";
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region{
// Beacon found!
self.statusLabel.text = #"Beacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
// You can retrieve the beacon data from its properties
//NSString *uuid = foundBeacon.proximityUUID.UUIDString;
//NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
//NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];}
But these codes are working in some devices
1) You try to monitor before verifying that monitoring is available.
2) I don't see any request for authorization:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
3) For authorization to even work, you need to have a string in your plist:
NSLocationAlwaysUsageDescription
If you google around using "CLLocationManager requestAlwaysAuthorization NSLocationAlwaysUsageDescription" you will find numerous blogs with step-by-step instructions on how to get beacon tracking to work.

Resources