使用registerRegionWithCircularOverlay方法注册区域

时间:2015-02-11 13:29:50

标签: ios objective-c

我可以使用CoreLocation框架获取当前位置。我想注册我当前的位置,以便我想知道谁进入和退出该地区。为此,我使用Apple记录的方法来注册- (void)registerRegionWithCircularOverlay:(MKCircle*)overlay andIdentifier:(NSString*)identifier { }的区域。我的问题是这个方法何时被调用?

文档链接:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

任何帮助?

1 个答案:

答案 0 :(得分:1)

您必须注意访问用户设备位置的权限。因此该应用程序能够跟踪用户位置。

您可以通过以下方式执行此操作:

  1. 在.plist文件中添加密钥NSLocationWhenInUseUsageDescription。这个键的值为String类型。您必须注意不要使用String以外的其他类型。
  2. enter image description here

    1. 在访问用户位置之前询问用户,添加以下行。

      self.locationManager = [[CLLocationManager alloc] init];
      self.locationManager.delegate = self;
      [self.locationManager requestWhenInUseAuthorization];
      
    2. 下面的代码将开始监控区域:

      // Tell location manager to start monitoring for the region
      [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
      
    3. 还覆盖以下方法以确保发现该区域:

      - (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
      {
          // We entered a region!
          NSLog(@"Entered in region");
      }
      
      -(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
      {
          // Exited the region
          NSLog(@"Exited from region");
      }