我编写了一个小应用程序,当应用程序在后台时,它可以在iOs上测试Geofence。
我可以毫不费力地在模拟器上获取入口/出口区域,或者在我的设备上使用xcode>debug>simulate位置模拟位置更新时,但当我在街上测试设备时,我没有得到任何入口/出口区域。
我激活了后台位置更新功能,并将关键字“隐私-位置始终使用描述”添加到info.plist中。
我使用下面的gpx文件在我的设备上测试CLCircleRegion -这项工作发现,但当我走在街上,我不能得到任何通知。
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
<wpt lat="2.349304" lon="48.875630">
<name>First Location</name>
</wpt>
<wpt lat="2.356139" lon="48.876516">
<name>Near Point 1</name>
</wpt>
<wpt lat="2.347933" lon="48.872340">
<name>Near Point 2 - 100 meter</name>
</wpt>
<wpt lat="2.345648" lon="48.863901">
<name>Near Point 2 - 80 meter</name>
</wpt>
</gpx>下面是我非常简单的代码:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
@synthesize locationManager, uuidRegion;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
[self startLocation];
return YES;
}
- (void)startLocation{
if(locationManager == nil){
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
if ([locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[locationManager setAllowsBackgroundLocationUpdates:YES];
}
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;
[self startMonitoringLocationWithLat: 2.357330 andLong:48.876375 withId:@"Point 1" andRadius:200];
[self startMonitoringLocationWithLat:2.345141 andLong:48.870672 withId:@"Point 2" andRadius:400];
[self startMonitoringLocationWithLat:2.345133 andLong:48.863615 withId:@"POint 3" andRadius:300];
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:@"xxxx"];
NSString *beaconIdentifier = @"beacon region";
uuidRegion = [[CLBeaconRegion alloc] initWithProximityUUID:
beaconUUID identifier:beaconIdentifier];
uuidRegion.notifyOnEntry=YES;
uuidRegion.notifyOnExit=YES;
uuidRegion.notifyEntryStateOnDisplay=YES;
[locationManager startMonitoringForRegion:uuidRegion];
}
}
-(void) startMonitoringLocationWithLat:(double) lat andLong:(double) longitude withId:(NSString *) identifier andRadius:(double) radius{
CLLocationCoordinate2D coordinate1 = (CLLocationCoordinate2D){.latitude = lat, .longitude = longitude};
CLLocationDistance radiusDistance = (CLLocationDistance) radius;
CLCircularRegion *circleRegion1 = [[CLCircularRegion alloc] initWithCenter:coordinate1 radius:radiusDistance identifier:identifier];
circleRegion1.notifyOnEntry=YES;
circleRegion1.notifyOnExit=YES;
[locationManager startMonitoringForRegion:circleRegion1];
}
-(void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@">>>>>>>>>>>>> ENTER IN : %@ <<<<<<<<<<<<<<", region.identifier);
[self createNotificationTitle:@"enter" andDescription:region.identifier];
}
-(void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@">>>>>>>>>>> EXIT FROM : %@ <<<<<<<<<<<<", region.identifier);
[self createNotificationTitle:@"exit" andDescription:region.identifier];
}
-(void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error{
[self createNotificationTitle:@"monitoring error" andDescription:@"error while monitoring"];
NSLog(@">>>>>>>>>>>>>>> Monitoring ERRROR %@ <<<<<<<<<<<<<<<<<", region.identifier);
NSLog(@">>>>>>>>>>differes update ERROR %@", error);
if (error != nil){
NSLog(@"ERROR IS VALID as CLError");
if (error.code == kCLErrorLocationUnknown){
NSLog(@"Error: Location Unknown");
} else if (error.code == kCLErrorRegionMonitoringDenied){
NSLog(@"Error: region monitoring denied");
} else if (error.code == kCLErrorRegionMonitoringFailure){
NSLog(@"Error: region monitoring failure");
} else if (error.code == kCLErrorRegionMonitoringSetupDelayed){
NSLog(@"Error: setup delay");
} else if (error.code == kCLErrorRegionMonitoringResponseDelayed){
NSLog(@"Error: response delay");
} else {
NSLog(@"Error not handled");
}
}
}
-(void) createNotificationTitle:(NSString *) title andDescription:(NSString *) description {
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:description];
[notification setAlertTitle:title];
[[UIApplication sharedApplication] presentLocalNotificationNow: notification];
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
@end我错过了什么有什么线索吗?
发布于 2016-10-27 23:01:49
我只是在我的应用中反转了纬度和经度...一切都很正常。享受这个小的地理围栏演示代码。
https://stackoverflow.com/questions/40266942
复制相似问题