iOS - 使用iBeacons在Proximity上触发事件

ySi*_*gen 5 iphone objective-c ios ios7 ibeacon

我正在使用新的iOS7 API开发iOS应用程序:iBeacon.

当我检测到给定的接近度时,我只是试图触发一个事件,在这里立即(4个中,其他是近,远和未知).

当我在iPhone 4S上构建我的应用程序时,它可以工作.所以我可以说我已经完成了,但是因为我对iOS很新,所以我根本不确定我的实现是否正确,或者更糟糕的是,如果不是这样,那么安全.

我基本上在我的视图控制器(objective-c类)中实现了我的事件,并在信标得到范围的locationManager方法中调用它.我拿了示例应用程序AirLocate中给出的代码,如果你想看看它是怎么回事.

我的活动只是调用另一个视图(为了能够访问该特定视图的某些新功能,只有当您与我的Beacon紧密相连时).我认为这很聪明,因为每当我的信标得到远程,if条件也会运行,如果它是真的,我的事件就会被调用.

下面是我的if条件,它位于locationManager方法的末尾:

    //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 "}"
Run Code Online (Sandbox Code Playgroud)

这是我的小方法/事件:

    - (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];
    }
Run Code Online (Sandbox Code Playgroud)

正如我所说,它在我的iPhone上工作正常,但我无法知道它是否是不行的.

所以,我只是愚蠢而且似乎一切都很好,或者我的代码是否存在严重的安全性或稳定性问题?

谢谢.

sag*_*444 1

稍微重写一下你的代码

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

在第二部分中,如果视图已经可见,则添加对不存在视图的检查

 - (void)immediateDetection
{
    if (self.presentedViewController)
    {
      return;
    }
    // rest of code here 
Run Code Online (Sandbox Code Playgroud)