如何获取位置(即使应用程序已终止或暂停)并将其发送到服务器
How to get and send location(even if application is terminated or suspended) to server
即使应用程序终止,我也需要获取位置并将其发送到服务器,我找到了如何获取位置的解决方案,但如何将其发送到服务器 - 我找不到。我想我可以用后台任务发送它,但它是如何一起工作的,我无法想象,我尝试使用这段代码 indidFinishLaunchingWithOptions:
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
// This "afterResume" flag is just to show that he receiving location updates
// are actually from the key "UIApplicationLaunchOptionsLocationKey"
self.shareModel.afterResume = YES;
[self.shareModel startMonitoringLocation];
[self.shareModel addResumeLocationToPList];
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:str forKey:@"DOWNLOADTASK"];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
但是我用这个键打印 NSUserDefaults 为空,我做错了什么,可能是我使用了错误的后台任务??
https://github.com/voyage11/GettingLocationWhenSuspended
iOS 9 how get locations even if app terminated
根据apple在文档中关于后台的描述,任何app后台任务执行时间为10minutes.After10分钟,app都会iOS强制挂机。
但是有五种类型的应用程序允许 "unlimited" 后台 运行 宁时间。
音频。
Location/GPS。
网络电话。
报亭。
外部配件。
以上五种类型的app声明你都可以拿来获取无限时间的背景,但是当你把app提交到app Store时,apple会审核你的app,一旦发现你"abuse" 后端 API,您的应用将被拒绝。
但是在企业开发的情况下,不存在"abuse"问题——企业app可以通过OTA部署,而不是通过apple store审核。
在企业部署中,你可以声明为VoIP app,但程序根本与VoIP无关,我们只是为了iOS给我们无限后台执行permissions.Declaration进程而已应用程序信息。添加以下键的文件:
首先在文件中添加以下两个所需的后台模式:在应用程序中播放音频或流 audio/video 使用 AirPlay 和应用程序提供 IP 语音服务。
UIBackgroundModes
网络电话
我测试了以下代码:
- (void)backgroundHandler {
NSLog(@"### -->backgroundinghandler");
UIApplication* app = [UIApplicationsharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
通过测试,我收到了一个"infinite"后台执行time.I不知道你认为是多长时间"infinite",但是在这种情况下,后台任务要运行至少55个小时,直到我失去耐心停止测试。
我的英文很差,转载此博客!
您不能按照 Apple 的指南在暂停或终止状态下执行任何任务。您可以在后台状态下获取位置更新,但在应用程序终止或暂停时无法执行此操作。请参阅此 link and this tutorial 获取后台位置信息。希望这会有所帮助:)
即使应用程序终止,我也需要获取位置并将其发送到服务器,我找到了如何获取位置的解决方案,但如何将其发送到服务器 - 我找不到。我想我可以用后台任务发送它,但它是如何一起工作的,我无法想象,我尝试使用这段代码 indidFinishLaunchingWithOptions:
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
// This "afterResume" flag is just to show that he receiving location updates
// are actually from the key "UIApplicationLaunchOptionsLocationKey"
self.shareModel.afterResume = YES;
[self.shareModel startMonitoringLocation];
[self.shareModel addResumeLocationToPList];
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:str forKey:@"DOWNLOADTASK"];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
但是我用这个键打印 NSUserDefaults 为空,我做错了什么,可能是我使用了错误的后台任务??
https://github.com/voyage11/GettingLocationWhenSuspended
iOS 9 how get locations even if app terminated
根据apple在文档中关于后台的描述,任何app后台任务执行时间为10minutes.After10分钟,app都会iOS强制挂机。 但是有五种类型的应用程序允许 "unlimited" 后台 运行 宁时间。
音频。
Location/GPS。
网络电话。
报亭。
外部配件。
以上五种类型的app声明你都可以拿来获取无限时间的背景,但是当你把app提交到app Store时,apple会审核你的app,一旦发现你"abuse" 后端 API,您的应用将被拒绝。 但是在企业开发的情况下,不存在"abuse"问题——企业app可以通过OTA部署,而不是通过apple store审核。 在企业部署中,你可以声明为VoIP app,但程序根本与VoIP无关,我们只是为了iOS给我们无限后台执行permissions.Declaration进程而已应用程序信息。添加以下键的文件: 首先在文件中添加以下两个所需的后台模式:在应用程序中播放音频或流 audio/video 使用 AirPlay 和应用程序提供 IP 语音服务。 UIBackgroundModes
网络电话
我测试了以下代码:
- (void)backgroundHandler {
NSLog(@"### -->backgroundinghandler");
UIApplication* app = [UIApplicationsharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
通过测试,我收到了一个"infinite"后台执行time.I不知道你认为是多长时间"infinite",但是在这种情况下,后台任务要运行至少55个小时,直到我失去耐心停止测试。 我的英文很差,转载此博客!
您不能按照 Apple 的指南在暂停或终止状态下执行任何任务。您可以在后台状态下获取位置更新,但在应用程序终止或暂停时无法执行此操作。请参阅此 link and this tutorial 获取后台位置信息。希望这会有所帮助:)