ios - 当应用程序未运行时按需获取用户位置,而不显示推送通知

标签 ios objective-c swift location

我正在开发一个应用程序(家庭安全),其中一个家庭成员可以请求并获取其他家庭成员的当前位置无需其他家庭成员的同意即使应用未运行。 这无法通过推送通知实现,因为当应用程序未运行时,推送通知 将不起作用。我不想在有人请求位置时向用户显示通知。无论应用程序是否运行,我都希望它能正常工作。请建议我完成此任务的任何解决方案。

附言。请不要告诉我我不应该像这样获取用户位置,否则我的应用将被拒绝。因为我正在为家庭安全公司开发此应用程序,所以家庭成员将首先阅读并接受公司的政策。

最佳答案

首先使用 requestAlwaysAuthorization 而不是 requestWhenInUseAuthorizationCLAuthorizationStatus 在应用安静时访问位置。

现在使用

UIApplication* application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
    [application endBackgroundTask: background_task];
    background_task = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [[self locationManager] startUpdatingLocation];//call you web-api here to update location on server.
    [application endBackgroundTask: background_task];
    background_task = UIBackgroundTaskInvalid;
});

这会有所帮助。

关于ios - 当应用程序未运行时按需获取用户位置,而不显示推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32352652/

相关文章:

ios - 在 xcode 8 中从 swift 2.2 迁移到 swift 3.0

ios - Objective C 中的@dynamic 属性

iphone - 使用 uitableview、tabbar 和更多对象查看

swift - 从属性的原始值获取枚举

swift - 实现枚举 ForwardIndexType

swift - glVertexAttribPointer() last at OpenGL 中的属性值可以是一个数组吗?

ios - UICollectionViewCell 中的 MPMoviePlayerController

ios - Trello iPhone 应用程序是如何开发的?

ios - 将标签栏移动到屏幕顶部

iphone - 以编程方式实现 UIScrollView