【问题标题】:didEnterRegion in iOS 8 - Region MonitoringiOS 8 中的 didEnterRegion - 区域监控
【发布时间】:2015-05-21 05:58:09
【问题描述】:

我正在编写一个用于区域监控的原型类型。下面是我的一组代码

        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager requestWhenInUseAuthorization];//commented for iOS 7

这就是我创建 locationManager 的方式

在 plist 中,

    <key>NSLocationAlwaysUsageDescription</key>
    <string>Location is required to find out where you are</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Location is required to find out where you are</string>

然后

-(BOOL) checkLocationManager
    {
        if(![CLLocationManager locationServicesEnabled])
        {
            [self showMessage:@"You need to enable Location Services"];
            return  FALSE;
        }
        if(![CLLocationManager isMonitoringAvailableForClass:[CLRegion class]])
        {
            [self showMessage:@"Region monitoring is not available for this Class"];
                    return  FALSE;
        }
        if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied ||
           [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted  )
        {
            [self showMessage:@"You need to authorize Location Services for the APP"];
            return  FALSE;
        }
        return TRUE;
    }

我正在检查这些条件

-(void) addGeofence:(NSDictionary*) dict
{

    CLRegion * region = [self dictToRegion:dict];
    [locationManager startMonitoringForRegion:region];
}
- (CLRegion*)dictToRegion:(NSDictionary*)dictionary
{
    NSString *identifier = [dictionary valueForKey:@"identifier"];
    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    if(regionRadius > locationManager.maximumRegionMonitoringDistance)
    {
        regionRadius = locationManager.maximumRegionMonitoringDistance;
    }

    NSString *version = [[UIDevice currentDevice] systemVersion];
    CLRegion * region =nil;

    if([version floatValue] >= 7.0f) //for iOS7
    {
        region =  [[CLCircularRegion alloc] initWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:identifier];
    }
    else // iOS 7 below
    {
        region = [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                       radius:regionRadius
                                                   identifier:identifier];
    }
    return  region;
}

这组代码在 iOS 7 上完美运行。

但是当我在 iOS 8 中运行相同的程序时,当我开始监控时,委托方法 didStartMonitoringForRegion 被调用。但是didEnterRegion在我进入该区域时永远不会被调用。

对于 iOS 8,我有什么需要特别注意的吗?

【问题讨论】:

标签: ios objective-c iphone core-location cllocationmanager


【解决方案1】:

区域监控需要用户始终授权。这是 iOS8 的新功能。

【讨论】:

    【解决方案2】:

    在iOS8中添加代码

     [self.locationManager requestWhenInUseAuthorization];
    

    并将名为“NSLocationWhenInUseUsageDescription”的键添加到 plist 中,并在提示中显示一条消息,以便在前台时定位。

    您还必须添加此代码

     [self.locationManager startUpdatingLocation]; 
    

    之后

    [self.locationManager requestWhenInUseAuthorization];
    

    【讨论】:

    • 已经添加了。在我的问题中,我也分享了我的 plist
    • 这个代码你加了吗[self.locationManager startUpdatingLocation]; [self.locationManager requestWhenInUseAuthorization] 之后;如果没有,请尝试一下。
    • 是的。 startUpdatingLocation 也已添加,并且正在调用相应的代表。
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2018-05-21
    • 2014-11-23
    • 2011-07-17
    • 2014-05-23
    相关资源
    最近更新 更多