0

修复:我在 viewDidLoad 方法中创建了我的 CLLocationManager,所以它几乎立即被 ARC 清理了。我将我的实例更改为类实例而不是方法实例,问题就解决了。

我有一个类,一个 NSObject,我用它来控制信标区域的进入和退出。

它的实现在这里:

#import "dcBeaconManager.h"

@implementation dcBeaconManager

-(id)init
{
    self = [super init];
    if (self != nil){}
    return self;
}

bool testRanging = true;
bool firstRegionEntered = true;

- (void)initBeaconManager {
    NSLog(@"initBeaconManager called");
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"digiConsRegion"];
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
}

- (void)stopBeaconManager {
    [self.locationManager stopMonitoringForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    NSLog(@"Started looking for regions");
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"Region discovered");
    if (firstRegionEntered) {
        NSLog(@"First time in region");
        firstRegionEntered = false;
    }
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"Region left");
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"We hope you enjoyed the event, thank you for coming.";
    notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    NSLog(@"locationManager initiated");
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];
    //Store some information about this beacon
    NSNumber *currentBeaconMajor = beacon.major;  //it's major (group) number
    NSNumber *currentBeaconMinor = beacon.minor;  //it's minor (individual) number

    if (([currentBeaconMinor floatValue] == 59204) && ([currentBeaconMajor floatValue] == 33995) && (beacon.proximity == CLProximityNear)) {
        NSLog(@"Mint discovered");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocateMint" object:nil];
    } else if (([currentBeaconMinor floatValue] == 7451) && ([currentBeaconMajor floatValue] == 63627) && (beacon.proximity == CLProximityNear)) {
        NSLog(@"Blue discovered");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocateBlue" object:nil];
    } else if (([currentBeaconMinor floatValue] == 51657) && ([currentBeaconMajor floatValue] == 26976) && (beacon.proximity == CLProximityNear)) {
        NSLog(@"Purple discovered");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocatePurple" object:nil];
    }

}


- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if (testRanging) {
        NSLog(@"Testing: forced ranging");
        if ([region isEqual:self.beaconRegion] && state == CLRegionStateInside) {
            [_locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region];
        }
    }
}

@end

这是标题:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface dcBeaconManager : NSObject <CLLocationManagerDelegate>

//properties
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;  //used to define which beacons we are looking for
@property (strong, nonatomic) CLLocationManager *locationManager;  //set up location services and allow beacons to be found

//methods
- (void)initBeaconManager;
- (void)stopBeaconManager;


@end

现在,这段代码在包含在主视图控制器中之前运行良好,但我正试图在 Obj-C 中的 OOP 方面做得更好。日志显示对象已创建并且主 initBeaconManager 被调用,但从那里只是停止。我不知道为什么。有什么想法吗?

干杯。

4

2 回答 2

0

我在 viewDidLoad 方法中创建了我的 CLLocationManager,而 ARC 几乎立即将其杀死。将其移动为类实例而不是方法实例解决了这个问题。

于 2013-12-07T19:40:18.880 回答
0

我不确定这是否有帮助,但您能否替换此行:

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"digiConsRegion"];

与那个:

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:2 identifier:@"digiConsRegion"];
self.beaconRegion.notifyOnEntry = entry;
self.beaconRegion.notifyOnExit = exit;
self.beaconRegion.notifyEntryStateOnDisplay = YES;

我相信您需要为培根地区提供主要和次要价值。

于 2013-12-07T16:35:12.893 回答