简体   繁体   English

15分钟后,后台位置更新在ios中不起作用?

[英]After 15 mins Background Location update is not working in ios?

I have implemented background location update in ios based on the timer (like every 1 min). 我已经根据计时器(像每1分钟)在ios中实现了后台位置更新。 Its working good and getting location(lat and Long values) but after 15 it is disconnecting. 它的工作良好,并获得位置(纬度和经度值),但15点后断开连接。 I'm not able to trigger it anymore. 我无法再触发它。 Can any one please help me out. 谁能帮帮我。

This is my code.. 这是我的代码。

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [[GlobalClass sharedGlobals].locationManager stopMonitoringSignificantLocationChanges];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        [[GlobalClass sharedGlobals].locationManager requestAlwaysAuthorization];
    }
    [[GlobalClass sharedGlobals].locationManager startMonitoringSignificantLocationChanges];

    int timeInterval = 60; // (i.e., 1 mins )

    if (!setTimer) {
        setTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target: self
                                                  selector: @selector(toMakeServerCall) userInfo: nil repeats: YES];
    }
}

#pragma mark - Send Current lat & long to the server

- (void)toMakeServerCall {

    NSString *latitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.longitude];

    NSLog(@"Current Lat is :%@ \n Current Long is :%@ ",latitude,longitude);

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.google.com/myLatitude is: %@ and My Longitude is: %@",latitude,longitude]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

}

Thanks in advance. 提前致谢。

When you use "significant change" service (or frankly any location services), you don't use a timer. 当您使用“重大更改”服务(或坦率地说,是任何位置服务)时,您无需使用计时器。 The OS calls you when there's a change in location, not the other way around. 位置发生变化时,操作系统会打电话给 ,而不是相反。

Regarding your timer, when the app is suspended, the timer won't run any more. 关于您的计时器,当应用暂停时,计时器将不再运行。 So, don't use a timer. 因此,请勿使用计时器。 But with significant change service, when the device detects that it has been moved a significant distance, your app will we awaken and be informed. 但是,借助重大更改服务,当设备检测到它已经移动了很远的距离时,您的应用将被唤醒并得到通知。 As the Location and Maps Programming Guide: Starting the Significant-Change Location Service says: 如《 位置和地图编程指南:启动重要更改位置服务 》所述:

If you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives. 如果您使重大更改位置服务保持运行状态,并且随后您的iOS应用被暂停或终止,则当新位置数据到达时,该服务会自动唤醒您的应用。 At wake-up time, the app is put into the background and you are given a small amount of time (around 10 seconds) to manually restart location services and process the location data. 唤醒时,该应用程序将进入后台,并且您会得到一小段时间(大约10秒)来手动重新启动位置服务并处理位置数据。 (You must manually restart location services in the background before any pending location updates can be delivered, as described in Knowing When to Start Location Services .) Because your app is in the background, it must do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. (您必须先在后台手动重新启动位置服务,然后才能交付任何未决的位置更新,如了解何时启动位置服务中所述 。)由于您的应用程序在后台运行,因此它必须做最少的工作并且避免执行任何任务(例如查询网络),可能会阻止它在分配的时间到期之前返回。 If it does not, your app will be terminated. 否则,您的应用将被终止。 If an iOS app needs more time to process the location data, it can request more background execution time using the beginBackgroundTaskWithName:expirationHandler: method of the UIApplication class. 如果iOS应用需要更多时间来处理位置数据,则可以使用UIApplication类的beginBackgroundTaskWithName:expirationHandler:方法请求更多后台执行时间。

Location updates may be paused even if you set pausesLocationUpdatesAutomatically to false. 即使您将pausesLocationUpdatesAutomatically自动设置为false,也可能会暂停位置更新。 The good thing is that you can get notified when the updates are paused, so you can restart location updates. 好消息是,您可以在暂停更新时收到通知,因此可以重新启动位置更新。

Define locationManagerDidPauseLocationUpdates in your CLLocationManagerDelegate like this: 像这样在您的CLLocationManagerDelegate定义locationManagerDidPauseLocationUpdates

func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
    manager.startUpdatingLocation()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM