2014-06-09 60 views
0

我正在開發一個iOS應用程序,我正在使用信標。iBeacon和通知

我遇到了問題。我正處於開發階段,所以我只有我的appdelegate。在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; 
} 

,然後我寫了兩個委託方法

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

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

但他們從來沒有得到所謂!這裏有什麼問題?

回答

3

你範圍但你從來沒有監測的地區。

測距信標將呼叫:
locationManager:didRangeBeacons:inRegion:

方法enterRegion/exitRegion你想僅用於監控。於是呼:
- (void)startMonitoringForRegion:(CLRegion *)region

+0

你是對的。非常感謝! – dylaniato

-1

我不知道爲什麼不叫,但這是我們如何能夠處理標...

- (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."); 
} 

您可以通過以下鏈接

指整個樣本項目

https://github.com/nicktoumpelis/HiBeacons

使用顯示信號燈..

希望它可以幫助喲ü....!