Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - trigger event on Proximity using iBeacons

I am working on an iOS app using the new iOS7 API : iBeacon.

I am simply trying to trigger an event when I detect a given proximity, Immediate here (out of the 4, the others are Near, Far and Unknown).

When I build up my app on my iPhone 4S, it works. So I could say I'm done, but since I am very new to iOS, I am not sure at all if my implementation is in anyway correct or, even worse if it's not the case, safe.

I basically implemented my event in my view controller (objective-c class) and call it in the locationManager method where the beacons get ranged. I took the code given in the sample app AirLocate, if you want to see how it goes.

My event is simply calling another view (in order to give access to some new features on that particular view, only when you're in immediate proximity with my Beacon). I thought of this being smart because every time my beacons get ranged, an if condition runs too and if it is true, my event gets called.

Below is my if condition which is at the end of the locationManager method :

    //Beacons ranging method from Apple until here.
    //My code following the sample code.

    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];
    if (beacon.proximity == CLProximityImmediate) {
        [self immediateDetection];
    }
    //End of the method here, which would be closed by the last "}"

And here is my little method/event :

    - (void)immediateDetection
    {
        NSString *storyboardName = @"Main";
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HueSwitch"];

        //call another view
        [self presentViewController:viewController animated:YES completion:nil];
    }

As I said, it works fine on my iPhone but I have no way to know if it is a no go as it is.

So, am I just being silly and it seems to be all fine or does my code have some serious security or stability issues?

Thank you.

like image 513
ySiggen Avatar asked Nov 10 '22 11:11

ySiggen


1 Answers

a little rewrite of your code

CLBeacon *beacon = [beacons lastObject];
if (beacon && beacon.proximity == CLProximityImmediate) {
   [self immediateDetection];
}

and in second part, add check to not present view if it already visible

 - (void)immediateDetection
{
    if (self.presentedViewController)
    {
      return;
    }
    // rest of code here 
like image 140
sage444 Avatar answered Nov 15 '22 04:11

sage444