【问题标题】:iphone : using Background location services is stopped after a little timeiphone:一段时间后停止使用后台定位服务
【发布时间】:2012-06-25 23:10:58
【问题描述】:

我正在尝试获取位置并将其与在线位置进行比较,所有这些都是在后台发生的 我创建的方法使用后台位置服务运行良好,但大约一分钟后,状态栏中的位置图标消失,并且不再调用该方法

这里是代码

- (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];

那有什么问题

【问题讨论】:

    标签: iphone ios geolocation location locationmanager


    【解决方案1】:

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

    【讨论】:

    • 我正在使用应用程序寄存器进行定位,正如我在帖子中所说的那样.. 并且连接速度不是这种情况.. 问题是状态栏中的位置图标在之后消失了一分钟左右,好像它已经完成检查或暂停!
    • 尝试注释你在线连接的行并等待响应,如果它没有停止那么问题是你在线连接,因为这会减慢didUpdateLocation方法返回的速度......你应该按照我在回答中描述的方式实现它......
    • 谢谢..我已经尝试过您评论 url 的想法,同样的情况发生了 :)
    • 你在某个地方打电话stopUpdatingLocation吗?
    • 不..我应该..我认为它被调用是因为它在后台!
    【解决方案2】:

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

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

    @property (nonatomic, strong) CLLocationManager *locationManager
    

    【讨论】:

    • 我这样做了,但它在 20 秒后仍然停止
    • 奇怪的是......它在模拟器上运行良好......但任何设备都没有运气
    • 我假设您已经尝试注释掉所有网络代码并在 didUpdateTo 回调中只留下一条日志语句?应用程序完全崩溃了吗?如果它没有崩溃,则可能意味着视图控制器正在某处卸载并且位置管理器已释放。
    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 2018-02-19
    • 2019-03-03
    • 1970-01-01
    • 2020-08-09
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多