我想知道如何在应用程序未运行时捕获位置并保存数据库。已经有几个教程,但没有一个工作。在得分重复之前,请你阅读完整的问题,因为它不可能。
我尝试了以下教程:
假设我没有捕获位置的实现。我需要每天中午,捕获用户的位置并保存数据库,然后发送到Web服务。 无论应用程序在后台运行还是未运行(中间按钮上的两个环并拖动),都必须进行此捕获位置。
我尝试了一些在互联网上发现的教程,甚至是社区建议的一些教程,但仍然没有工作。
background: linear-gradient(bottom, #FFFFFF 50%, #008ED3 50%);
background: -o-linear-gradient(bottom, #FFFFFF 50%, #008ED3 50%);
background: -moz-linear-gradient(bottom, #FFFFFF 50%, #008ED3 50%);
background: -webkit-linear-gradient(bottom, #FFFFFF 50%, #008ED3 50%);
background: -ms-linear-gradient(bottom, #FFFFFF 50%, #008ED3 50%);
。语言可以是客观的,也可以是快速的,最重要的是,我学习如何在所有州的应用程序中捕获这个位置。
答案 0 :(得分:5)
每个步骤都遵循此方法
AppDelegate.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) CLLocationManager *locationManager;
@end
AppDelegate.m
#define userDef [NSUserDefaults standardUserDefaults]
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#import "AFNetworking.h"
#import <GoogleMaps/GoogleMaps.h>
@implementation AppDelegate{
BOOL fromTerminated;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
fromTerminated = NO;
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
fromTerminated = YES;
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.activityType = CLActivityTypeOtherNavigation;
[self.locationManager startUpdatingLocation];
}
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if(self.locationManager){
[self.locationManager stopMonitoringSignificantLocationChanges];
self.locationManager = nil;
self.locationManager.delegate = nil;
}
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.activityType = CLActivityTypeOtherNavigation;
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startMonitoringSignificantLocationChanges];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"locationManager didUpdateLocations: %@",locations);
if(fromTerminated){
CLLocation * newLocation = [locations lastObject];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
[userDef setObject:[NSString stringWithFormat:@"%f",theLocation.longitude] forKey:@"LONGITUDE"];
[userDef setObject:[NSString stringWithFormat:@"%f",theLocation.latitude] forKey:@"LATITUDE"];
self.myLocation = theLocation;
self.myLocationAccuracy = theAccuracy;
[self updateLocation];
}
}
-(void)updateLocation{
// call your webservice for updating
}
@end
此代码将执行以下操作 - &gt;
后台提取将触发位置更改并将启动应用程序,并且将在选项字典中使用didFInishLaunchingWithOptions
调用UIApplicationLaunchOptionsLocationKey
。如果它发现这意味着应用程序已终止并且已唤醒以进行后台提取。现在你有30秒左右的时间做你的东西。因此,您创建一个位置管理器对象并开始更新,这将触发您的didUpdateLocations
委托方法,然后您可以调用您的方法来触发服务器或数据库中更改的位置。
在普通VC中创建另一个位置管理器对象,就像在didFinishiLaunchingWithOptions
方法中创建的那样,并实现委托方法didUpdateLocation
,这将运行,直到应用程序处于前台或后台。应用程序委托方法hack将在终止时触发应用程序。
干杯:)
[编辑]
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *locationManager;
@end
@implementation ViewController{
}
-(void)viewDidAppear:(BOOL)animated{
if(self.locationManager == nil){
_locationManager = [CLLocationManager new];
}
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
[_locationManager requestAlwaysAuthorization];
} else {
[_locationManager startUpdatingLocation];
}
}
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
_locationManager = nil;
CLLocation *location = [locations lastObject];
theUser.latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
theUser.longitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
}
}
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
} break;
case kCLAuthorizationStatusDenied: {
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[_locationManager startUpdatingLocation]; //Will update location immediately
} break;
default:
break;
}
}
@end
要检查应用程序是否在终止状态后启动执行此更改并点击运行按钮并从故事板更改设备的位置
执行此更改,然后运行项目(在设备的调试模式下执行此操作)
并通过此更改位置,然后在applicationDidFinishLaunchingWithOptions
中粘贴断点,您将看到断点被击中,这意味着应用程序处于已终止状态,但此位置更改已触发操作系统启动应用程序。
希望可以让你明白