【发布时间】:2016-08-19 18:27:37
【问题描述】:
即使应用程序终止,我也需要获取位置并将其发送到服务器,我找到了如何获取位置的解决方案,但是我如何将其发送到服务器 - 我找不到。我想我可以将它与后台任务一起发送,但它是如何一起工作的,我无法想象,我尝试使用这段代码 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 它是空的,我做错了什么,可能是我使用了错误的后台任务??
How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended
【问题讨论】:
-
在后台方法中试试这段代码
标签: ios core-location