iBeacon not found if it is already in range - ios

I found this good tutorial for develop an app with iBeacon:
http://www.appcoda.com/ios7-programming-ibeacons-tutorial/
But with this implementation, as the author say, if you start the receiver when it's already in the beacon range, it's not going to fire. If you want find an ibeacon you need to walk far away from its region, and then walk back into range.
How can I modify this code to find a beacon that it is in range when I lunch the app?
I use Xcode6, IPad air, IOS 8
This is the simplified code from the tutorial:
in ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController<CLLocationManagerDelegate>
#property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
In ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"ACFD065E-C3C0-11E3-9BBE-1A514932AC01"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.appcoda.testregion"];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
NSLog(#"Finding beacons.");
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
NSLog(#"None found.");
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
NSLog(#"Beacon found");
}

The didEnterRegion and didExitRegion callbacks only fire when the user crosses the region boundary. In the case of an iBeacon this means moving from "Inside" to "Outside" or vice versa.
When you fire up the app and start monitoring for your beacon region, you can request the current state for your beacon region to determine if your user is inside or outside.
Implement the didDetermineState callback:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
This callback gets triggered after you start monitoring your region, anytime a region boundary is crossed (so be careful you don't duplicate logic here and inside didEnter/ExitRegion), and in response to a call to requestStateForRegion:
Hope this helps... if you need more -> https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/index.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didDetermineState:forRegion:

Related

iOS Location permission not work to show by Objective-C

I create a sample project to get user location.
But When I run the app, the location permission not show to me.
What's wrong with my code?
Thanks.
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController<CLLocationManagerDelegate>
#property (nonatomic, strong) CLLocationManager *locationManager;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *latLabel;
#property (weak, nonatomic) IBOutlet UILabel *longLabel;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *currentLocation = [locations lastObject];
if(currentLocation != nil){
self.latLabel.text = [NSString stringWithFormat:#"%.2f",currentLocation.coordinate.latitude];
self.longLabel.text = [NSString stringWithFormat:#"%.2f",currentLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
}
}
#end
You need to start by checking locationServicesEnabled. If they are enabled, preceed to call authorizationStatus to learn your app's actual authorization status. You ask for the authorization dialog only if the status is "not determined".
If the status is anything else, there is no point asking for the authorization dialog; it won't appear.
Another problem is that this code is useless:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *currentLocation = [locations lastObject];
if(currentLocation != nil){
self.latLabel.text = [NSString stringWithFormat:#"%.2f",currentLocation.coordinate.latitude];
self.longLabel.text = [NSString stringWithFormat:#"%.2f",currentLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
}
}
You are calling stopUpdatingLocation as soon as you get your first location update. But the chances of your getting a useful location on the first location update are basically nil, because the sensors are just warming up.
(Also please note that it is pointless to check "Location updates" in your background modes. You will not get any location updates in the background unless you set the location manager's allowsBackgroundLocationUpdates to YES, and you are not doing that.)

Core Location not responding

When I enter the following code and run it on the simulator and set the location to "City Run", it does not log anything. I don't know what I am doing wrong though.
.h
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <UITextFieldDelegate,CLLocationManagerDelegate> {
CLLocationManager *locMgr;
}
#property (nonatomic, retain) CLLocationManager *locMgr;
#property NSInteger speed;
.m
#synthesize locMgr;
- (void)viewDidLoad {
[super viewDidLoad];
self.locMgr = [[CLLocationManager alloc] init];
self.locMgr.delegate = self;
self.locMgr.desiredAccuracy = kCLLocationAccuracyBest;
[self.locMgr startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.speed = roundf([newLocation speed]);
NSLog(#"speed: %ld", (long)self.speed);
}
Do you see anything that I am missing?
Try adding the NSLocationWhenInUseUsageDescription key, to your info.plist
You need to request authorization to use location services using either:
[self.locMgr requestAlwaysAuthorization];
or
[self.locMgr requestWhenInUseAuthorization];

startUpdatingLocation no longer working in XCode 6.1

I cannot get even the simplest location updating code to work. I have a location app that worked fine for over a year, and now when I try to compile and run it (after upgrading to xcode 6.1 and with no changes to the code), after calling startUpdatingLocation, the didUpdateLocations callback never fires, and I can see that the gps indicator never appears next to the battery indicator as it should.
I have started a new test project that does nothing but attempts to register for location updates, and still the same results: no location updates on device or simulator.
Here is the code for the single view controller of the test project:
//ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
{
}
#property (strong, nonatomic) CLLocationManager* locationManager;
#end
//ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"got a location");
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Error: %#",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"got a location");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
It would be fantastic if someone with the latest version of xcode could create a project like this that does nothing but receive location updates, verify that it works, and post the code in full. I have tried adding things to the plist file and all other remedies from similar questions to no avail.
Yes, a few things have changed in the new version and it can be a headache. I was having the same problem and this is the thread that solved it for me here.
You need to add these lines info.plist:
<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
And make sure that you call [CLLocationManager requestWhenInUseAuthorization] or [CLLocationManager requestAlwaysAuthorization] before you try to update the users location.

StartMonitoringSignificantLocationChanges not calling didupdatedLocations

I am developing an iOS 7 app that uses location. I never get the location updated. If I do the same with startUpdatingLocation it works good. I have read the documentation and I don't know where is the problem.
The location is enabled for the app, and it ask you if you want to enabled it the first time I run the app. Also the location icon in the status bar is not showed.
Update: In the simulator it works, it gives me an updated location. Why it doesn't work in the device?
It's only going to be used in a class, here is the configuration.
#import <CoreLocation/CoreLocation.h>
#interface ClassLocation : UIViewController <CLLocationManagerDelegate>{
}
Setting the property in the .h and in the .m the synthesize
#property (strong, nonatomic) CLLocationManager *localizacionManager;
#synthesize localizacionManager;
ViewDidLoad:
localizacionManager=[[CLLocationManager alloc] init];
[localizacionManager setDelegate:self];
[localizacionManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
[localizacionManager startMonitoringSignificantLocationChanges];
And the two delegate methods:
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(#"error: %#",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(#"Location updated");
}
Add a key to your Info.plist and request authorization from the location manager asking it to start.
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
You need to request authorization for the corresponding location method.
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
The same happens to me in iOS 7. I presume it's due to the last location being cached; if the current location has no significant changes compared to the last one, the delegate never gets called.
You can access the last cached location by using the "location" property of the CLLocationManager :
#property (readonly, nonatomic, copy) CLLocation *location ;
Hope this helps.

iBeacon App only working sometimes, transmitting never working on iPhone 5

I am having a really weird problem here. I am trying to build an app to detect beacons. I don't have any real beacons yet so I am testing with an iPhone 5 and an iPad 3. The strange thing is that the transmitting is only working on the iPad, the iPhone doesn't work as a transmitter even though I used the same app on it.
But even with the iPad as a transmitter, the app only works sometimes - sometimes the iPhone will notify me that it has found a beacon, sometimes it doesn't. I've force-closed the app on the iPad and after the restart it worked, but then another time it doesn't.
Since everything is working sporadically I think it can't be the code causing that behaviour, but it might be - I am not an experienced coder, I've just started this. My code is based on this tutorial http://www.devfright.com/ibeacons-tutorial-ios-7-clbeaconregion-clbeacon/
I first thought this might be the answer, but taht didn't solve it: it still did work sometimes, and sometimes it didn't.
Can anybody tell me what I am dealing with her?
Here is my code for the Tracker:
ladBeaconTracker.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#interface ladBeaconTracker : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLBeaconRegion *beaconRegion;
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (weak, nonatomic) IBOutlet UILabel *beaconFoundLabel;
#end
ladBeaconTracker.m
#import "ladBeaconTracker.h"
#interface ladBeaconTracker ()
#property NSUUID *uuid;
#end
#implementation ladBeaconTracker
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self initRegion];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"A5456D78-C85B-44C6-9F20-8268FD25EF8A"];
self.beaconRegion = [[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:#"Museum"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
NSLog(#"Region %# initated", _beaconRegion.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
self.beaconRegion.notifyEntryStateOnDisplay = YES;
NSLog(#"Region %# entered", _beaconRegion.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
NSLog(#"Region %# exit", _beaconRegion.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
CLBeacon *beacon = [[CLBeacon alloc] init];
beacon= [beacons lastObject];
self.beaconFoundLabel.text =#"Yes";
NSLog(#"Ranged Region %#", _beaconRegion.identifier );
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
And this is the transmitter-Code:
configViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#interface ConfigViewController : UIViewController <CBPeripheralManagerDelegate>
#property (strong,nonatomic) CLBeaconRegion *beaconRegion;
#property(strong,nonatomic) NSDictionary *beaconPeripheralData;
#property (strong, nonatomic) CBPeripheralManager *PeripheralManager;
#end
ConfigViewController.m
#import "ConfigViewController.h"
#interface ConfigViewController ()
#end
#implementation ConfigViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initBeacon];
}
- (void) peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
NSLog(#"Powered ON");
[self.PeripheralManager startAdvertising:self.beaconPeripheralData];
}
else if (peripheral.state == CBPeripheralManagerStatePoweredOff){
NSLog(#"Powered OFF");
[self.PeripheralManager stopAdvertising];
}
}
- (void)initBeacon {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"A5456D78-C85B-44C6-9F20-8268FD25EF8A"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
major:1
minor:1
identifier:#"Museum"];
}
- (IBAction)transmitBeacon:(UIButton *)sender {
self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
self.PeripheralManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil options:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Your code looks fine for detecting beacons in the background. Two suggestions:
I suspect the problem when the iPad is transmitting is not with the iPad but that the iPhone cannot receive. Try cycling power to the iPhone tto clear a known bug in iOS 7.1.
iOS can detect iBeacons much more quickly in the foreground if you set up ranging at the same time you set up monitoring. Move [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; into initRegion and take out stopRangingBeaconsInRegion entirely.
Once you have done (2), repeat your tests in the foreground and look for your log statement Ranged Region. You should see this every second when the beacon is on.
In the background, know that it can take up to 15 minutes to both detect an iBeacon and detect that an iBeacon is no longer around. Again, watch your log lines for hints about the current state, and know that you cannot get a new region entry event until you first get a region exit event or reboot your phone.

Resources