在ios后台向服务器发送20米差异后的位置 -(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{ if([launchOptions objectForKey:UIApplication
在ios后台向服务器发送20米差异后的位置在ios后台向服务器发送20米差异后的位置 -(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{ if([launchOptions objectForKey:UIApplication,ios,objective-c,cllocationmanager,Ios,Objective C,Cllocationmanager,在ios后台向服务器发送20米差异后的位置 -(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{ if([launchOptions objectForKey:UIApplicationLaunchActionSlocationKey]){ self.locationManager=[[CLLocationManager alloc]init]; self.locationManager.delegate=self; self
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
if([launchOptions objectForKey:UIApplicationLaunchActionSlocationKey]){
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=KCallocationAccuracyBest;
self.locationManager.activityType=CLActivityTypeFitness;
//新事件的移动阈值。
self.locationManager.distanceFilter=20;
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
//[self.locationManager在未使用授权时请求];
[self.locationManager请求始终授权];
}
[self.locationManager startUpdatingLocation];
[self.locationManager启动监视重要位置更改];
}
返回YES;
}
-(无效)应用程序标识符背景:(UIApplication*)应用程序{
_isBackGround=是;
[self.locationManager停止监视重要的位置更改];
[self.locationManager请求始终授权];
[self.locationManager启动监视重要位置更改];
}
-(无效)应用IDBECOMEACTIVE:(UIApplication*)应用{
_isBackGround=NO;
[self.locationManager停止监视重要的位置更改];
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=KCallocationAccuracyBest;
self.locationManager.activityType=CLActivityTypeFitness;
//新事件的移动阈值。
self.locationManager.distanceFilter=20;
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
//[self.locationManager在未使用授权时请求];
[self.locationManager请求始终授权];
}
[self.locationManager startUpdatingLocation];
[self.locationManager启动监视重要位置更改];
}
-(无效)sendBackgroundLocationToServer
{
if([[AppHelper nullCheck:[AppHelper userDefaultsForKey:USER_ID]]length]>0){
NSString*query=[NSString stringWithFormat:@“localUpdate=='YES'”;
NSArray*arrData=[self-searchObjectsForEntity:@“UserLocation”,带谓词:[NSPredicate predicateWithFormat:query]和sortKey:nil和sortScending:NO];
if(arrData.count){
[self-sendUpdateUserLocation:arrData];
}
}
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
NSDate*eventDate=newLocation.timestamp;
NSTimeInterval howRecent=[eventDate timeIntervalSinceNow];
CLLocationDistance距离=[newLocation distanceFromLocation:oldLocation];
NSLog(@“%f”,dist);
if([[AppHelper nullCheck:[AppHelper userDefaultsForKey:K_LATITUDE]]长度]您请求始终使用位置服务,但您是否在.plist
中提供了使用位置服务的原因?是的,位置服务始终是,我已添加。plist nslocationalways usagedescription nslocationwhen niusagedescription,并且我已检查后台模式功能位置更新后台F蚀刻远程通知,我还查看了一件事,数据在本地数据库中。只有数据在5分钟后不会发送到服务器上。请给出任何想法,并说明:我已经检查过了,但它不起作用。您一直要求使用定位服务,但您是否在.plist
中提供了使用它的原因?是的位置服务总是,我已经添加了。plist NSLocationAlwaysUsageDescription nsLocationWhenUsageDescription,而且我已经检查了后台模式功能位置更新后台获取远程通知,而且我已经查看了本地数据库中的一件事数据。之后,只有数据不会发送到服务器上5分钟。请尽快给出任何想法。另外:我已经检查过了,但它不起作用。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
// Movement threshold for new events.
self.locationManager.distanceFilter = 20;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
// [self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
_isBackGround=YES;
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
_isBackGround=NO;
[self.locationManager stopMonitoringSignificantLocationChanges];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
// Movement threshold for new events.
self.locationManager.distanceFilter = 20;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
// [self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
-(void) sendBackgroundLocationToServer
{
if([[AppHelper nullCheck:[AppHelper userDefaultsForKey:USER_ID]] length]>0){
NSString * query = [NSString stringWithFormat:@"localUpdate=='YES'"];
NSArray* arrData=[self searchObjectsForEntity:@"UserLocation" withPredicate:[NSPredicate predicateWithFormat:query] andSortKey:nil andSortAscending:NO];
if(arrData.count){
[self sendUpdateUserLocation:arrData];
}
}
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
CLLocationDistance dist=[newLocation distanceFromLocation:oldLocation];
NSLog(@"%f",dist);
if ([[AppHelper nullCheck:[AppHelper userDefaultsForKey:K_LATITUDE]] length] <3) {
[AppHelper saveToUserDefaults:[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude] withKey:K_LATITUDE];
[AppHelper saveToUserDefaults:[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude] withKey:K_LONGITUDE];
[[Service sharedEventController]parseUserLocationeList:[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude] andLong:[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]];
}
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
[AppHelper saveToUserDefaults:[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude] withKey:K_LATITUDE];
[AppHelper saveToUserDefaults:[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude] withKey:K_LONGITUDE];
[[Service sharedEventController]parseUserLocationeList:[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude] andLong:[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]];
[self sendBackgroundLocationToServer];
}
-(void)sendUpdateUserLocation:(NSArray*)ArrData{
if([AppHelper appDelegate].checkNetworkReachability)
{
self.bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
}];
NSMutableArray *arr=[NSMutableArray new];
for(UserLocation *loc in ArrData){
NSMutableDictionary *parameters= [NSMutableDictionary new];
parameters[@"lat"]=loc.lat;
parameters[@"long"]=loc.longT;
[arr addObject:parameters];
}
NSMutableDictionary *parameters =[NSMutableDictionary new];
parameters[@"uid"]=[AppHelper userDefaultsForKey:USER_ID];
parameters[@"data"]=arr;
NSString *baseURL = [NSString stringWithFormat:@"%@%@",BaseUrl,UPDATE_LOCATION];
if (self.bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:baseURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
[[AppHelper sharedInstance]hideIndicator];
for (NSManagedObject *manObj in ArrData) {
[self.managedObjectContext deleteObject:manObj];
// NSLog(@"delete objects for predicate %@", predicate);
}
[self saveContext];
NSLog(@"JSON: %@", responseObject);
if (self.bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[AppHelper sharedInstance]hideIndicator];
if (self.bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
}];
}
}