iOS位置更新甚至应用程序都没有运行(比如找我的iphone)

xco*_*123 3 iphone core-location ios

我想编写位置跟踪应用程序,如" 查找我的iPhone ",它运行在前台,后台,甚至终止(不运行).位置将定期发送到我的服务器.我在谷歌搜索并阅读了许多文档,教程评论,有关此主题的代码.然后我找到了这个教程.但在本教程中,位置被发送到前景中的".txt"文件并且后台没有终止 ...我的意思是,当应用程序被杀死时,它不会在后台重新启动以发送位置".txt"文件..所以我已经添加并更新了一些代码,以便在它没有运行时发送位置..但是,我没有这样做...我的意思是,当我在多任务中杀死(关闭)应用程序时(双击主页按钮),它是不发送到位置...

你能帮助我吗,我该如何解决这个问题?

提前致谢

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self log:[NSString stringWithFormat:@"Background location %.06f %.06f %@" , newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp]];
}

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

    id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
    if (locationValue)
    {
        // create a new manager and start checking for sig changes

        [self log:@"didFinishLaunchingWithOptions location key"];
        m_locManager = [[CLLocationManager alloc] init];

        [self log:@"didFinishLaunchingWithOptions created manager"];
        m_locManager.delegate = self;

        [self log:@"didFinishLaunchingWithOptions set delegate"];
        [m_locManager startMonitoringSignificantLocationChanges];

        [self log:@"didFinishLaunchingWithOptions monitoring sig changes"];

        return YES;
    }

    [self log:@"didFinishLaunchingWithOptions"];    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    [self log:@"applicationWillResignActive"];
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:viewController.m_significantSwitch.on forKey:@"significant"];
    [userDefaults synchronize];
}                


- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self log:@"applicationDidEnterBackground"];
    [m_locManager startMonitoringSignificantLocationChanges];
}


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

    [self log:@"applicationWillEnterForeground"];
    [m_locManager stopMonitoringSignificantLocationChanges];

}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self log:@"applicationDidBecomeActive"];

    if (![window.subviews count])
    {
        // Add the view controller's view to the window and display.
        [window addSubview:viewController.view];
        [window makeKeyAndVisible];

        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        viewController.m_significantSwitch.on = [userDefaults boolForKey:@"significant"];
        if (viewController.m_significantSwitch.on)
            [viewController actionSignificant:nil];
    }
}


- (void)applicationWillTerminate:(UIApplication *)application {
    [self log:@"applicationWillTerminate"];
    [m_locManager startMonitoringSignificantLocationChanges];

}
Run Code Online (Sandbox Code Playgroud)

小智 7

这是不正确的,当您的应用程序终止时,它仍然可以监控重要的位置更新.