委托使用CLLocationManagerDelegate不工作目标c

时间:2014-12-10 07:02:12

标签: ios objective-c delegates ibeacon

当我在CLLocationManagerDelegate中调用委托方法时,我已经使用CLLocationManagerDelegate创建了一个自定义委托,但委托不能正常工作。我认为我创建代表的方式是错误的,有人能告诉我我做错了吗?

.h file

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>
@protocol iBeaconDeligate<NSObject>
@required
-(void)getArray:(NSArray *)beaconArray;
@end

@interface iBeaconDeligate : NSObject <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property CLProximity lastProximity;

-(void) initiBeaconDeligate;

@end

这是.m file

#import "iBeaconDeligate.h"

@implementation iBeaconDeligate


-(void) initiBeaconDeligate
{

    NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:
                          @"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
    NSString *beaconIdentifier = @"iBeaconModules.us";
    CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:
                                    beaconUUID identifier:beaconIdentifier];

    self.locationManager = [[CLLocationManager alloc] init];

    //[self.locationManager requestWhenInUseAuthorization];
    //[self.locationManager requestAlwaysAuthorization];
    // New iOS 8 request for Always Authorization, required for iBeacons to work!
    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;

    [self.locationManager startMonitoringForRegion:beaconRegion];
    [self.locationManager startRangingBeaconsInRegion:beaconRegion];
    [self.locationManager startUpdatingLocation];
}

-(void)sendLocalNotificationWithMessage:(NSString*)message {

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    notification.alertBody = message;
    notification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:
(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    NSString *message = @"";

    if(beacons.count > 0) {
        CLBeacon *nearestBeacon = beacons.firstObject;



        if(nearestBeacon.proximity == self.lastProximity ||
           nearestBeacon.proximity == CLProximityUnknown) {
            return;
        }

        self.lastProximity = nearestBeacon.proximity;



        switch(nearestBeacon.proximity) {
            case CLProximityFar:
                message = @"You are far away from the beacon";
                break;
            case CLProximityNear:
                message = @"You are near the beacon";
                break;
            case CLProximityImmediate:
                message = @"You are in the immediate proximity of the beacon";
                break;
            case CLProximityUnknown:
                return;
        }
    } else {
        message = @"No beacons are nearby";
    }


    [self performSelector:@selector(getArray:) withObject:beacons];
    NSLog(@"%@", message);
    [self sendLocalNotificationWithMessage:message];


}

-(void)locationManager:(CLLocationManager *)manager
        didEnterRegion:(CLRegion *)region {
    [manager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
    [self.locationManager startUpdatingLocation];

    NSLog(@"You entered the region.");
    [self sendLocalNotificationWithMessage:@"You entered the region."];
}

-(void)locationManager:(CLLocationManager *)manager
         didExitRegion:(CLRegion *)region {
    [manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
    [self.locationManager stopUpdatingLocation];

    NSLog(@"You exited the region.");
    [self sendLocalNotificationWithMessage:@"You exited the region."];
}

@end

这就是我在控制器类中调用它的方式

.H @interface HomeViewController : UIViewController<iBeaconDeligate>

的.m

iBeaconDeligate *sampleProtocol = [[iBeaconDeligate alloc]init];

[sampleProtocol initiBeaconDeligate];

问题是在CLLocationManagerDelegate中自动调用的方法未在我的自定义委托中调用

1 个答案:

答案 0 :(得分:2)

因为sampleProtocol是方法中的局部变量,所以当方法退出时,它将被释放。

您应该创建一个(strong) @property来存储此对象,以确保它被保留。