如果用戶在應用程序處於後臺模式時進入或退出區域,則需要執行調用localNoification的簡單任務。只有一組座標會觸發通知。例如:如果用戶在應用程序處於後臺時進入/退出區域,則調用localNotification
緯度:41.8500 經度:87.6500 半徑:300
我知道如何調用localNotification,以及如何使用的LocationManager的基本功能,但似乎無法跟蹤的位置背景。任何幫助將是偉大的!
如果用戶在應用程序處於後臺模式時進入或退出區域,則需要執行調用localNoification的簡單任務。只有一組座標會觸發通知。例如:如果用戶在應用程序處於後臺時進入/退出區域,則調用localNotification
緯度:41.8500 經度:87.6500 半徑:300
我知道如何調用localNotification,以及如何使用的LocationManager的基本功能,但似乎無法跟蹤的位置背景。任何幫助將是偉大的!
你有[此代碼是從上面鏈接的蘋果直營店採取]請閱讀CLLocationManager的startMonitoringForRegion:
方法?我認爲這將做到你想要的。該代碼來設置它會是這個樣子:
CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter: CLLocationCoordinate2DMake(41.8500, 87.6500) radius: 300 identifier: @"regionIDstring"];
CLLocationManager * manager = [[CLLocationManager alloc] init];
[manager setDelegate: myLocationManagerDelegate];
[manager startMonitoringForRegion: region];
之後,該設備將監測入口/出口到指定的區域,甚至當你的應用程序是在後臺。當一個地區通過時,代表將收到電話locationManager:didEnterRegion:
或locationManager:didExitRegion:
。您可以使用此機會發布UILocalNotification
。如果您的應用程序在區域越過時未運行,它將在後臺啓動,您需要在application: didFinishLaunchingWithOptions:
中查找相應的密鑰。使用如下代碼:
if ([launchOptions objectForKey: UIApplicationLaunchOptionsLocationKey] != nil) {
// create a location manager, and set its delegate here
// the delegate will then receive the appropriate callback
}
要知道,應用程序將只有很短的時間量,同時在後臺(幾秒鐘)運行的執行;如果您需要執行較長時間的任務,請在應用程序收到區域通知通知後立即致電Nebs在其答案中提到的beginBackgroundTaskWithExpirationHandler:
方法。這會讓你在後臺運行大約600秒。
看看UIApplication
的beginBackgroundTaskWithExpirationHandler:
方法。它允許您在應用程序處於後臺時請求額外的時間來運行任務。
欲瞭解更多信息,我建議您閱讀iOS app programming guide的「後臺執行和多任務」部分。它詳細解釋了當應用程序進入後臺並允許你執行什麼時會發生什麼。
它具體顯示了運行一項艱鉅的任務此示例代碼時,應用程序進入後臺:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
似乎創建位置管理器並在檢測到UIApplicationLaunchOptionsLocationKey時設置委託不足以獲取觸發基於位置的應用程序啓動的數據。 – Daniel