iphone:使用后台位置服务会在一段时间后停止

时间:2012-06-24 10:39:24

标签: iphone ios geolocation location locationmanager

我正在尝试获取一个位置并将其与在线位置进行比较,所有这些都发生在后台 我创建的方法使用后台位置服务正常工作但是一分钟左右后状态栏中的位置图标消失了,方法不再被调用

这是代码

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {


    double  lat = newLocation.coordinate.latitude;
    double  lon = newLocation.coordinate.longitude;
    NSURL * locationURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://somedomainname.com/iphoneLocation?lat=%f&lon=%f",lat,lon]];
    NSData * responseData = [NSData dataWithContentsOfURL:locationURL];
    NSString* aStr;
    aStr = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];



    if ([aStr isEqualToString:@"out Of Any Knowen Range"] ){
            UILocalNotification *notify =[[UILocalNotification alloc] init];
            notify.alertAction = [[NSString alloc] initWithString: @"View"];
            notify.fireDate=nil;
            notify.alertBody = [NSString stringWithFormat:@"New Data Occured"];
            notify.soundName = UILocalNotificationDefaultSoundName;
            NSLog(@"Local notification should display");
            [[UIApplication sharedApplication] presentLocalNotificationNow:notify];
        }

}

并且在viewDid加载中我正在使用类似这样的东西

locationManager = [[CLLocationManager alloc] init];

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    locationManager.delegate = self;

    [locationManager startUpdatingLocation];

    CLLocation *userLocation = [locationManager location];

所以它有什么问题

2 个答案:

答案 0 :(得分:1)

您需要修改 AppName-Info.plist 文件,方法是添加一个键Required background modes,其中包含值为App registers for location updates的项目。我认为你在网上连接时应该做的另一件事情可能不会很快发生,所以你在线连接,发布位置和等待响应的操作应该从另一个线程开始,同时如果你从{{收到了另一个位置1}}并且您之前的请求尚未完成以跳过发布新位置...

答案 1 :(得分:0)

我想知道位置管理器是否在某处发布,因此不再发送任何更新。

您是否尝试过将位置管理器设置为视图控制器中的保留属性?

@property (nonatomic, strong) CLLocationManager *locationManager