以下是我正在嘗試使用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
}
}
請原諒一些代碼indentation,我無法讓它繼續相同。 –