【问题标题】:iOS Location update even app is not running (like find my iphone)iOS 位置更新,即使应用程序没有运行(比如找到我的 iphone)
【发布时间】:2013-08-27 10:34:43
【问题描述】:

我想编写像“find my iphone”这样的位置跟踪应用程序,它可以在前台、后台运行,甚至终止(不运行)。位置将定期发送到我的服务器。我在谷歌搜索并阅读了很多关于这个主题的文档、教程、代码。然后我找到了this tutorial。但是在本教程中,位置被发送到前台和后台的“.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];

}

【问题讨论】:

    标签: iphone ios core-location


    【解决方案1】:

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

    【讨论】:

    • 您是正确的,因为发布者没有指定他们使用的 iOS 版本,但从 iOS7 开始,如果用户从应用切换器终止应用,则不再在后台接收位置更新跨度>
    • 这是终止应用后的链接mobileoop.com/…
    【解决方案2】:

    朋友,你走错路了。实际上,在 iOS 中杀死应用程序后,没有任何方法可以执行任务。你可以阅读这个here in Apple documentation

    查找我的 iPhone - 这是一个 Apple 应用程序,他们正在使用私有 api 来实现这一点。并且这些类型的功能不适用于通用开发人员编程。

    所以请不要走这条路。一切都是徒劳的。

    希望对您有所帮助!

    【讨论】:

    • 嗯,好的,我的朋友.. 这个答案可能会让我失望 :) 我的应用程序只能在前台和后台运行:/ 所以,可能会出现电池问题......再次感谢。 .
    • ok.. :) 我是 stackoverflow 的新手,我不知道这个平台的规则 :))
    【解决方案3】:

    当应用程序被杀死时,你不能运行任何东西,除了你可以接收推送通知或 UILocalNotifications。你可以尝试复制 UILocalNotifications 功能并做你的事情,即使你的应用程序被杀死/在 bg/ 中。顺便说一句,这是一件复杂的事情,您可以对 ios7 后台任务或其他内容进行更多挖掘。

    【讨论】:

    • 好吧,我的朋友,我在这个应用程序中工作的过程会很困难......谢谢..
    【解决方案4】:

    这可能会对您有所帮助。 Getting the User’s Location

    参见Starting the Significant-Change Location Service 部分。它说

    如果您让发生重大变化的位置服务继续运行,而您的 iOS 应用随后被暂停或终止,则该服务会在新的位置数据到达时自动唤醒您的应用。唤醒时,应用程序进入后台,您有一小段时间(大约 10 秒)手动重新启动位置服务并处理位置数据。

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 2017-11-16
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多