2015-10-24 58 views
3

在我的iOS應用中,我希望在特定時間段內觸發位置更新一段時間。 例如,我希望在明天下午5點到當天下午7點之間更新服務器的位置。在某段時間在特定時間觸發位置更新

達到此目的的最佳方法是什麼?

我不是在尋找代碼,但有一些流量/算法來實現這一點。

+0

我覺得你的下一個挑戰將是該應用程序將處於暫停狀態,在下午5點,沒有代碼會被執行開始位置更新。爲了讓應用程序保持在後臺,您必須選擇一種策略,其中之一是始終保持位置更新。 – Kashif

+0

我想voip是要走的路 –

回答

0

試試這個: 我希望你知道如何設置基本的CoreLocation環境,比如將locationUsageDescription添加到info.plist並啓用背景模式。所以我不會解釋這些,這是我的代碼。您可以將此代碼添加到AppDelegate或您希望添加的任何ViewController。在我來說,我已經加入到AppDelegate-

- (void)initializeLocationManager { 

    /* These are dummy _startTime and _endTime, you can set up the required dateTime as per your requirement 
     _startTime is set to current dateTime + 60 seconds 
     _endTime is set to _start time + 60 seconds 
    */ 
    _startTime = [NSDate date]; 
    // secondsToAdd - 60 seconds 
    NSTimeInterval secondsToAdd = 60; 
    _startTime = [_startTime dateByAddingTimeInterval:secondsToAdd]; 
    _endTime = [_startTime dateByAddingTimeInterval:secondsToAdd]; 

    NSLog(@"Start time = %@", _startTime); 
    NSLog(@"endtime = %@", _endTime); 

    _locationManager = [[CLLocationManager alloc] init]; 

    // You can use requestWhenInUseAuthorization/ requestAlwaysAuthorization depending on your requirement 
    if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
     [_locationManager requestAlwaysAuthorization]; 
    } 
    [_locationManager setDelegate:self]; 
    [_locationManager startUpdatingLocation]; 
    [_locationManager startMonitoringSignificantLocationChanges]; 
    } 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
     [self initializeLocationManager]; 
     return YES; 
    } 

貫徹CLLocationManagerDelegate作爲

#pragma mark CLLocationManager Delegate 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    if (![CLLocationManager locationServicesEnabled]) { 
     NSLog(@"Couldn't update location, Location services are not enabled"); 
    } 

    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) { 

     NSLog(@"Couldn't update location, Location services not authorized."); 

    } 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    // Compare _startTime with the current dateTime 
    // If _startTime is less than current dateTime, then you can start sending updates to your server 

    switch ([_startTime compare:[NSDate date]]) { 
     case NSOrderedAscending: 

      // You can add condition to restrict the frequency of updating location 
      // Like here, I have added condition to send updates every 60 seconds 

      // lastLocationUpdatedTime should be initialize before calling [self initializeLocationManager]; 
      // lastLocationUpdatedTime = [NSDate date]; 

      // if ([[NSDate date] timeIntervalSinceDate:lastLocationUpdatedTime] > (1*60)) { 
      // lastLocationUpdatedTime = [NSDate date]; 
      // NSLog(@"send location update to server"); 
      // } 
      NSLog(@"send location update to server"); 

      if ([_endTime compare:[NSDate date]] == NSOrderedAscending) { 
       NSLog(@"end updates"); 
      // Stop updating location 
       [_locationManager stopUpdatingLocation]; 
      } 
      break; 
     case NSOrderedSame: 
      // If equals you can add your logic here... 
      break; 
     case NSOrderedDescending: 
      // If startime is greater than time [NSDate date] 
      break; 
     default: 
      break; 
    } 

} 
+0

感謝您的回覆。但是,如果應用程序處於暫停模式,則無法工作。所以我們在這裏有兩件事: 1.從暫停模式中喚醒應用程序 2.在停止LocaitonUpdate(確保它在10分鐘後未掛起)之前運行一段時間 –

+0

如果應用程序處於暫停模式,位置沒有重大變化。然後,這不會工作,但如果位置發生重大變化,無論停用模式如何,應用程序都會喚醒幾秒鐘(如10秒鐘)。除此之外,除非您正在處理voip應用程序,否則我沒有看到任何選項 – Vashum