2013-02-24 33 views
4

我正在研究一個應用程序,當他靠近使用區域監視的某些地標時提醒用戶。一切正常,但當應用程序在後臺我沒有得到警報。當我打開應用程序時,我會彈出所有警報。我想要的是在應用程序處於後臺時獲取它們。我想知道是否有可能或應用程序需要運行才能獲取警報?任何幫助將不勝感激。IOS Geofencing後臺警報

更新: 問題似乎是我用警報而不是本地通知。以下是我使用的代碼:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    NSLog(@"Entered Region - %@", region.identifier); 
    [self showRegionAlert:@"You are near: " forRegion:region.identifier]; 
} 

如何將此更改爲本地通知?

+2

在你的'Info.plist',你聲明'location'爲你的'UIBackgroundModes'? – yeesterbunny 2013-02-24 22:14:36

+0

其實我這樣做,這是爲什麼我認爲它仍然可以跟蹤位置變化,以便當我運行應用程序時,我得到所有警報彈出一個接一個後,我只是不會彈出時,應用程序運行在後臺 – XpApp 2013-02-24 22:34:48

+1

時你說你'沒有得到警報',你的意思是你在地標到達時產生UIAlertViews?這將無法正常工作,因爲您無法在後臺顯示警報。你應該使用UILocalNotifications如果你還沒有 – 2013-02-25 00:15:48

回答

7

入住

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

如果您來回切換前景色和背景的閾值條件之間的「測試您的應用區域監視」一節可能無法得到滿足,觸發你把應用推到前再次前景。

此外,當後臺應用獲取通知時,只有一個小窗口用於處理消息。試圖做網絡請求可能會超時...

檢查你的plist設置 - 聲明位置爲UIBackgroundModes只需要如果您需要高精度定位。即使沒有定義位置,重要的位置變化也能起作用

檢查locationManager:didUpdateLocations:和locationManager:didFailWithError:正在被調用,並且沒有發佈錯誤。

檢查您是否在您的plist中將ApplicationRunsInBackground設置爲NO。

嘗試實施AppDelegates applicationDidEnterBackground :,應用程序:didFinishLaunchingWithOptions:和朋友來確定您在給定時間的應用生命週期中的哪個位置。

+1

感謝您的回答。其實我讀了這個,這裏是我的問題,當在後臺應用程序監視區域,我甚至可以在控制檯中看到它,但問題是,我沒有得到任何警報,而應用程序是在後臺。當我開始時,我得到了在後臺發生的所有事件的警報 – XpApp 2013-02-24 23:01:39

+0

我用更多的指針更新了答案。 – 2013-02-25 10:02:12

+0

其實問題似乎是,我用UIAlert,而不是本地通知,我更新我的問題與我使用的代碼,你知道我可以切換到localnotifications而不是警報? – XpApp 2013-02-25 15:14:34

0
  1. 對於地理圍欄解決方案,您可以參考@Niels Castle給出的答案。
  2. 對於本地通知,你可以參考下面的代碼:
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 

localNotification.alertBody = @"I'M IN THE REGION"; 

localNotification.userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:@"nearBy",@"type", nil]; 

localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

localNotification.soundName = UILocalNotificationDefaultSoundName; 

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; 
0
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    NSLog(@"Yes! Welcome to %@",region.identifier); 
    UILocalNotification* notify = [UILocalNotification new]; 
    notify.alertBody = [NSString stringWithFormat:@"Welcome to %@",region.identifier]; 
    notify.soundName = UILocalNotificationDefaultSoundName; 
    if (notify.applicationIconBadgeNumber == 0) { 
     notify.applicationIconBadgeNumber = 1; 
    } 
    [[UIApplication sharedApplication] presentLocalNotificationNow:notify]; 
}