没有调用委托方法,看不清楚原因

时间:2013-12-07 16:27:00

标签: ios objective-c cllocationmanager ibeacon

FIXED:我在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,但是从那里停止。我无法弄清楚为什么。有什么想法吗?

干杯。

2 个答案:

答案 0 :(得分: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;

我认为你需要为培根区域提供主要和次要价值。

答案 1 :(得分:0)

我在viewDidLoad方法中创建了我的CLLocationManager,ARC几乎立即将其删除。将它移动为类实例而不是方法实例可以解决问题。