iOs Geofencing - 在使用模拟器

时间:2016-10-26 15:57:21

标签: ios geolocation cllocationmanager

当应用程序处于后台时,我编写了一个小应用程序来测试iO上的Geofence。

我可以毫不费力地在模拟器上获取条目/出口区域或使用xcode模拟位置更新> debug>模拟我的设备上的位置但是当我在街道上的设备上进行测试时,我没有得到任何进入/退出地区。

我激活了后台位置更新功能,并将关键字“隐私 - 位置始终使用说明”添加到info.plist。

一切正常如果我使用iBeacon区域。

我使用以下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

关于我想念的任何线索?

1 个答案:

答案 0 :(得分:0)

我只是在我的应用程序中反转纬度和经度......一切正常。 享受这个小型地理围栏演示代码。