我尝试了以下教程:
> Getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended
> Background Modes Tutorial: Getting Started
> to run app continously in the background
> Not exactly what I need,but still I tried
假设我没有捕获位置的实现.我需要每天中午,捕获用户的位置并保存数据库,然后发送到Web服务.
无论应用程序在后台运行还是未运行(中间按钮上的两个环并拖动),都必须进行此捕获位置.
我尝试了在互联网上找到的一些教程,甚至社区建议的一些教程,但仍然没有工作.
>我添加了背景模式 – 位置.
>我允许获取位置requestAlwaysAuthorization.
语言可以是客观c或快速,最重要的是,我学习如何在所有州的应用程序中捕获此位置.
解决方法 好的,我将尽可能简单.启用背景模式和勾选背景,如this每个步骤都遵循此方法
当应用程序被终止时
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
此代码将执行以下 *** 作 – >
后台获取将触发位置更改并将启动应用程序,并且将使用选项字典中的UIApplicationLaunchOptionsLocationKey调用dIDFInishLaunchingWithOptions.如果它发现这意味着应用程序已终止并且已唤醒以进行后台提取.现在你有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中粘贴断点,您将看到断点被命中,这意味着应用程序处于已终止状态,但此位置更改已触发 *** 作系统启动应用程序.
希望可以让你明白
总结以上是内存溢出为你收集整理的ios – 在所有州app中捕获位置全部内容,希望文章能够帮你解决ios – 在所有州app中捕获位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)