簡體   English   中英

iOS - 使用iBeacons在Proximity上觸發事件

[英]iOS - trigger event on Proximity using iBeacons

我正在使用新的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 "}"

這是我的小方法/事件:

    - (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];
    }

正如我所說,它在我的iPhone上工作正常,但我無法知道它是否是不行的。

所以,我只是愚蠢而且似乎一切都很好,或者我的代碼是否存在嚴重的安全性或穩定性問題?

謝謝。

稍微重寫一下你的代碼

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

在第二部分中,如果已經顯示,則將檢查添加到不存在的視圖

 - (void)immediateDetection
{
    if (self.presentedViewController)
    {
      return;
    }
    // rest of code here 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM