iOs 4.2在后台的重要位置变化

时间:2010-12-06 04:24:05

标签: iphone cocoa-touch

我的appDelegate中有以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {    
  m_manager = [[[CLLocationManager alloc] init] autorelease];
  NSLog(@"Significant Location Change Available: %d", [CLLocationManager significantLocationChangeMonitoringAvailable]);
  [m_manager startMonitoringSignificantLocationChanges];
  [window addSubview:viewController.view];
  [window makeKeyAndVisible];

  m_manager = [[CLLocationManager alloc] init];
  m_manager.delegate = self; 
  viewController.manager = m_manager;

  [UIDevice currentDevice].batteryMonitoringEnabled = YES;
  return YES;
}

这是我的代表回调:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {
  viewController.locationInfo.text = [NSString stringWithFormat:@"Your current position:\nLatitude: %f\nLongitude: %f\nBattery: %f",
                                  newLocation.coordinate.latitude,
                                  newLocation.coordinate.longitude,
                                  [UIDevice currentDevice].batteryLevel];

  ASIFormDataRequest *req = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://myserver.com/location"]];
  [req setRequestMethod:@"POST"];
  [req addPostValue:[NSNumber numberWithDouble:newLocation.coordinate.latitude] forKey:@"latitude"];
  [req addPostValue:[NSNumber numberWithDouble:newLocation.coordinate.longitude] forKey:@"longitude"];
  [req addPostValue:[UIDevice currentDevice].uniqueIdentifier forKey:@"deviceid"];
  [req addPostValue:[NSNumber numberWithBool:[self.viewController.statusSwitch isOn]] forKey:@"gps"];
  if ([UIDevice currentDevice].batteryState != UIDeviceBatteryStateCharging &&
  [UIDevice currentDevice].batteryLevel > 0)
    [req addPostValue:[NSNumber numberWithFloat:[UIDevice currentDevice].batteryLevel] forKey:@"battery"];

  if (viewController.manualUpdate) {
    req.delegate = self;
    req.didReceiveResponseHeadersSelector = @selector(didReceiveResponse:);
  }
  [req startAsynchronous];

  UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
  if (alarm)
  {
    alarm.fireDate = [NSDate date];
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.repeatInterval = 0;
    alarm.alertBody = @"Received location delegate";

    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
  }
}

我的问题是激活我的GPS。如果我打电话

[m_manager startLocationUpdate];

我收到本地通知,服务器也会更新。但是,如果我使用

[m_manager startMonitoringSignificantLocationChanges];

我没有收到本地通知。

我甚至在高速公路上开车10分钟来测试通知,什么也没得到...... 任何人都可以在iOS 4.2下工作吗?

2 个答案:

答案 0 :(得分:2)

在调用 startMonitoringSignificantLocationChanges 并将委托分配给另一个实例后,您分配新CLLocationManager的原因是什么?

您可能想尝试一下:

  m_manager  = [[[CLLocationManager alloc] init] autorelease];
  m_manager.delegate = self; 
  viewController.manager = m_manager;
  NSLog(@"Significant Location Change Available: %d", [CLLocationManager significantLocationChangeMonitoringAvailable]);
  [manager startMonitoringSignificantLocationChanges];
  [window addSubview:viewController.view];
  [window makeKeyAndVisible];

答案 1 :(得分:0)

MLefrancois的答案应该解决你的主要问题。另一个问题是重要位置变更通知目前非常粗糙:当您的手机从一个手机信号塔传递到另一个手机信号塔时,您会收到通知,这就是它。区域监控的工作方式相同。