先確定可以從背景取得 location 資訊
首先開啟 Xcode background modes 設定
確認 Info.plist
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
<string>location</string>
Location Services 取得權限為 "Always"
locationManager!.requestAlwaysAuthorization()
locationManager = CLLocationManager()
locationManager!.requestAlwaysAuthorization()
locationManager!.desiredAccuracy = kCLLocationAccuracyBest
locationManager!.allowsBackgroundLocationUpdates = true
locationManager!.pausesLocationUpdatesAutomatically = false
locationManager!.delegate = self
偵測 iBeacon
當 App 存在前景或背景,可以使用 startRangingBeaconsInRegion 偵測 Beacon
偵測到後會觸發下列函式:
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion)
當 App 不執行的狀態下,可以使用 startMonitoringForRegion 偵測 Beacon
偵測到後會觸發下列函式:
func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion)
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)
在 App 不執行的狀態下,此時可以發送本地推播?
或是進行 http request/response?
甚至連接藍芽裝置嗎?
答案是可以
如果要發送本地推播,記得要註冊
let notificationType:UIUserNotificationType = [UIUserNotificationType.Sound, UIUserNotificationType.Alert]
let notificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)