2017-05-25 21 views
1

以下是我正在嘗試使用CLLocationManager進行信標區域監測的一個簡短版本。使用CLLocationManager進行Swift 3信標監測

我需要知道多個(少於20個)區域的進入/退出事件。

即使在後臺,我也可以獲得一致的位置輸入事件。在「客戶端視圖」或後臺中,我沒有在前臺獲取退出事件。

在某一點,我成功地在後臺獲取退出事件,但我沒有獲得可變數量的信標區域進行收聽。

這段代碼是否有任何的總體規則/邏輯瑕疵?

//------------------ 
 
/* Client View Controller - Main Profile View */ 
 
class ClientVC: UIViewController, ClientVC_Preferences_Protocol, OpenPreferencesProtocol, ClientVCProtocol { 
 

 
\t /* Reference to AppDelegate file where location manager resides */ 
 
\t let appDelegate = UIApplication.shared.delegate as! AppDelegate 
 
\t override func viewDidLoad(){ 
 

 
\t \t // .. Obtain some beacon info - not shown 
 

 
\t \t for beacon in beacons { 
 
\t   /* Create a region for each beacon and start monitoring */ 
 
\t   var region = CLBeaconRegion(proximityUUID: UUID(uuidString:beacon.UUID)!, identifier: beacon.Identifier) 
 
\t   region.notifyEntryStateOnDisplay = true 
 
\t   region.notifyOnExit = true 
 
\t   region.notifyOnEntry = true 
 
\t   self.appDelegate.locationManager.startMonitoring(for: region) 
 
\t  } 
 
\t } 
 

 
\t /* Protocol function to alert client when exit event occured */ 
 
\t func alertClient(businessName:String) { 
 
     
 
     let notification = UILocalNotification() 
 
     notification.alertBody = "Did you leave " + businessName + "?" 
 
     UIApplication.shared.presentLocalNotificationNow(notification) 
 
     
 
     let alertController = UIAlertController(title: businessName, message: "Did you leave?", preferredStyle: .alert) 
 
     
 
     let okAction = UIAlertAction(title: "Yes, I Left!", style: UIAlertActionStyle.default) { 
 
      UIAlertAction in 
 
     \t // .. Do some firebase work - not shown 
 
     } 
 

 
     let cancelAction = UIAlertAction(title: "No, I'm Here!", style: UIAlertActionStyle.cancel) { 
 
      UIAlertAction in 
 
      // .. Do some firebase work - not shown 
 
     } 
 
     
 
     alertController.addAction(okAction) 
 
     alertController.addAction(cancelAction) 
 
     
 
     self.present(alertController, animated:true, completion:nil) 
 
    } 
 
} 
 

 
//------------------ 
 
/* AppDelegate */ 
 

 
/* Protocol connected to Client View Controller */ 
 
protocol ClientVCProtocol{ 
 
    func alertClient(businessName:String) /* Displays alert to client when exiting the region */ 
 
    func reloadTableView() /* Refreshes table view */ 
 
} 
 

 
@UIApplicationMain 
 
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { 
 

 
\t /* Delegate for ClientVC Protocol - used to call methods */ 
 
\t var ClientVCDelegate:ClientVCProtocol? 
 

 
\t /* Instantiate location manager */ 
 
\t var locationManager = CLLocationManager() 
 

 
\t /* Triggered by entering a region */ 
 
\t func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
 
     print("Enter event " + region.identifier) 
 
     // .. Do some firebase work - not shown 
 
\t } 
 

 
\t /* Triggered by exiting a region */ 
 
\t func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { 
 

 
\t \t /* Break set on this line to ensure whether or not this function is called */ 
 
\t \t print("Exit Attempt") 
 

 
\t \t /* Gets business name associated with region and calls protocol function to ClientVC */ 
 
\t \t if let beaconRegion = region as? CLBeaconRegion { 
 
      self.getBusinessName(region: region){ 
 
       (result: String) in 
 
       print("Exit event " + region.identifier) 
 
       self.ClientVCDelegate?.alertClient(businessName:result) 
 
      } 
 
     } 
 
\t } 
 

 
\t /* Runs when application finishes launching - configures location manager */ 
 
\t func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {   
 
     self.fbRef = Database.database().reference() 
 
     UIApplication.shared.registerUserNotificationSettings(
 
      UIUserNotificationSettings(types: .alert, categories: nil)) 
 
     
 
     /* Not sure how relevent all of these calls are */ 
 
     locationManager.delegate = self 
 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
 
     locationManager.distanceFilter = 2000 
 
     if #available(iOS 9.0, *) { 
 
      locationManager.allowsBackgroundLocationUpdates = true 
 
     } 
 
     locationManager.startMonitoringSignificantLocationChanges() 
 
     locationManager.pausesLocationUpdatesAutomatically = true 
 
     locationManager.requestAlwaysAuthorization() 
 
     
 
     return true 
 
    } 
 
}

+0

請原諒一些代碼indentation,我無法讓它繼續相同。 –

回答

1

的一個問題是,你只設置了地區的ViewController監測,這確實應該在AppDelegate如果你想背景進入/退出事件火災完成。

瞭解CoreLocation會跟蹤您正在監控的區域,並在區域狀態發生變化時自動將您的應用程序放入後臺。它只會觸發回調,但是,如果您設置了CLLocationManager區域,並將其設置在AppDelegate的didFinishLaunching方法中。

+0

我如何才能聽取我不知道的信標,直到檢索到用戶的數據?應用程序可以在登錄後「重新啓動」並傳遞了這些數據嗎? –

+0

Apple的文檔讓我相信在監控某個地區時,它只會聽UUID,專業和未成年人。當我聲明一個CLBeaconRegion時,它需要一個標識符,並將兩個具有相同UUID和不同標識符的兩個信標視爲兩個單獨的區域。我該如何監聽UUID關聯區域,並使用相同的UUID觸發事件擁有不同的信標? –

+0

測試時,我創建一個要監視的區域並給它一個默認標識符「test」。當我運行應用程序並引入具有相同UUID和自己的個人標識符的信標時,我確實退出並確實輸入區域方法,對於我用「test」標識符聲明的區域觸發一次,並且對於具有信標的標識符。出口和進入方法不應該觸發一次嗎? –

相關問題