简体   繁体   English

iBeacon和通知

[英]iBeacon and Notification

i'm developing an iOS application and i'm using beacons. 我正在开发一个iOS应用程序,并且正在使用信标。

I've a problem. 我有问题 I'm at the beginning of the development, so I only have my appdelegate. 我正在开发的初期,所以只有我的appdelegate。 In appdelegate.m I have initialized like so 在appdelegate.m中,我已经像这样初始化了

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

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

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"8AEFB031-6C32-486F-825B-E26FA193487D"];
    CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                                identifier:@"Region"];

    if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
    {
        NSLog(@"I'm looking for a beacon");
        [self.locationManager startRangingBeaconsInRegion:region];
    } else {
        NSLog(@"Device doesn't support beacons ranging");
    }

    return YES;
}

and then I wrote two delegate methods 然后我写了两个委托方法

- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"EXIT");
}

- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"ENTER");
}

but they never get called!!! 但是他们从来没有被叫过!!! What's the problem here? 这是什么问题

you RANGE but you never MONITOR the regions. 您的范围很广,但您从不监视区域。

Ranging for beacons will only call: 定位信标只会调用:
locationManager:didRangeBeacons:inRegion:

The methods enterRegion/exitRegion you want are for monitoring only. 所需的enterRegion / exitRegion方法仅用于监视。 So call: 因此致电:
- (void)startMonitoringForRegion:(CLRegion *)region

I don't know why there are not calling but this is how we can handle beacons... 我不知道为什么不打电话,但这是我们如何处理信标...

- (void)createBeaconRegion
{
    self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"8AEFB031-6C32-486F-825B-E26FA193487D"];
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                            identifier:@"Region"];

if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
    NSLog(@"I'm looking for a beacon");
    [self.locationManager startRangingBeaconsInRegion:region];
} else {
    NSLog(@"Device doesn't support beacons ranging");
}
}

- (void)turnOnRanging
{
    NSLog(@"Turning on ranging...");

    if (![CLLocationManager isRangingAvailable]) {
        NSLog(@"Couldn't turn on ranging: Ranging is not available.");
        self.rangingSwitch.on = NO;
        return;
    }

    if (self.locationManager.rangedRegions.count > 0) {
        NSLog(@"Didn't turn on ranging: Ranging already on.");
        return;
    }

    [self createBeaconRegion];
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];

    NSLog(@"Ranging turned on for region: %@.", self.beaconRegion);
}


- (void)startRangingForBeacons
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    self.detectedBeacons = [NSArray array];

    [self turnOnRanging];
}

- (void)stopRangingForBeacons
{
    if (self.locationManager.rangedRegions.count == 0) {
        NSLog(@"Didn't turn off ranging: Ranging already off.");
        return;
    }

    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
 NSLog(@"Turned off ranging.");
}

You can refer the entire sample project using below link 您可以使用以下链接引用整个示例项目

https://github.com/nicktoumpelis/HiBeacons https://github.com/nicktoumpelis/HiBeacons

That uses to display Beacons.. 用来显示信标。

Hope it helps you....! 希望它能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM