从后台杀死应用程序后,应用程序应该在ios中运行gps跟踪应用程序(目标c) [英] After killing the app from the background, app should run gps tracking application in ios (objective c)

查看:157
本文介绍了从后台杀死应用程序后,应用程序应该在ios中运行gps跟踪应用程序(目标c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个GPS跟踪应用程序。该应用程序应跟踪iPhone用户的位置。在我的情况下,它跟踪应用程序在后台并打开。如果我从后台查杀我的应用程序,我没有获取位置更新。这是任何可能的杀死应用程序后,应用程序应跟踪iOS中的位置(目标c)。

解决方案

添加位置更新在设置应用程序capabibities:



图片:设置位置更新的功能

然后,将位置使用的隐私政策说明添加到位置始终使用说明:



图片:地点总是在plist中使用说明



最后,将这段代码添加到appdelegate中:
$ b

 (BOOL)application :(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[self.locationManager startMonitoringSignificantLocationChanges];
} else {
[self.locationManager startUpdatingLocation];
}
返回YES;

(void)applicationWillResignActive:(UIApplication *)application {
//当应用程序即将从激活状态转移到非激活状态时发送。对于某些类型的临时中断(例如来电或SMS消息)或用户退出应用程序并开始转换到后台状态时,可能会发生这种情况。
//使用此方法暂停正在进行的任务,禁用定时器并使图形呈现回调无效。游戏应该使用这种方法来暂停游戏。
[locationManager stopUpdatingLocation];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
locationManager.pausesLocationUpdatesAutomatically = NO;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[locationManager startUpdatingLocation];


$ b(void)applicationDidEnterBackground:(UIApplication *)application {
[locationManager stopUpdatingLocation];

__block UIBackgroundTaskIdentifier bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
bgTask = UIBackgroundTaskInvalid;
}];

的NSTimer *计时器= [的NSTimer scheduledTimerWithTimeInterval:10.0
目标:自
选择器:@selector(startTrackingBg)
USERINFO:无
重复:YES] ;


$ b(void)applicationWillTerminate:(UIApplication *)application {
//当应用程序即将终止时调用。如果适用,保存数据。另请参阅applicationDidEnterBackground :.
//在应用程序终止之前保存应用程序的托管对象上下文中的更改。

__block UIBackgroundTaskIdentifier bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
bgTask = UIBackgroundTaskInvalid;
NSLog(@App terminated);
}];
}

(void)startTrackingBg {

[locationManager startUpdatingLocation];
NSLog(@App正在后台运行);
}


I am making a GPS tracking application. The app should track the iPhone user location. In my case it tracking when app is in background and open . if I am killing my app from background I am not getting location update. Is this an any possible after killing the app, app should track the location in iOS(objective c).

解决方案

Add location updates to your application capabibities in settings:

Image : Setting capabilities to Location updates

Then, add Privacy policy description for location usage set to location always usage description:

Image : Location Always usage description in plist

Finally, add this piece of code in appdelegate:

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
           [self.locationManager requestAlwaysAuthorization];
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager startMonitoringSignificantLocationChanges];
        } else {
            [self.locationManager startUpdatingLocation];
        }
return YES;
}
(void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    [locationManager stopUpdatingLocation];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    locationManager.pausesLocationUpdatesAutomatically = NO;
    locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    [locationManager startUpdatingLocation];
}


(void)applicationDidEnterBackground:(UIApplication *)application {
    [locationManager stopUpdatingLocation];

    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
    }];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                      target:self
                                                    selector:@selector(startTrackingBg)
                                                    userInfo:nil
                                                     repeats:YES];


}
(void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.

    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
        NSLog(@"App terminated");
    }];
}

(void)startTrackingBg {

    [locationManager startUpdatingLocation];
    NSLog(@"App is running in background");
}

这篇关于从后台杀死应用程序后,应用程序应该在ios中运行gps跟踪应用程序(目标c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆