2017-05-04 81 views
0

我試圖讓我的iPhone 7升級到iBeacon顯示用於測試目的爲什麼我的委託從未被叫?

我使用下面的代碼,但我代表永遠不會被調用,因此不會啓動廣告

- (IBAction)adminViewBeaconSwitchToggled:(id)sender { 

    NSUserDefaults *tillUserDefaults = [NSUserDefaults standardUserDefaults]; 

    if(_adminViewBeaconSwitch.isOn) { 
     [tillUserDefaults setInteger:1 forKey:@"beaconIsOn"]; 
     if ([NWTillHelper isDebug] == 1) { 
      NSLog(@"iBeacon Mode: ON"); 

      NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:@"39876A4B-43B2-4BE8-9A9C-41BAE913D56A"]; 

      CLBeaconRegion *beaconRegion = [[ CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:@"me.netwizards.office"]; 

      _beaconPeripheralData = [beaconRegion peripheralDataWithMeasuredPower:nil]; 

      _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:nil queue:nil]; 

      //[peripheralManager startAdvertising:beaconPeripheralData]; 
     } 
    } else { 
     [tillUserDefaults setInteger:0 forKey:@"beaconIsOn"]; 
     if ([NWTillHelper isDebug] == 1) { 
      NSLog(@"iBeacon Mode: OFF"); 
     } 
    } 
} 

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { 
    NSLog(@"iBeacon update state was triggered"); 

    switch (peripheral.state) { 
     case CBManagerStatePoweredOn: 
      NSLog(@"Powered on"); 
      [peripheral startAdvertising:_beaconPeripheralData]; 
      break; 
     case CBManagerStatePoweredOff: 
      NSLog(@"Powered Off"); 
      [peripheral stopAdvertising]; 
      break; 
     case CBManagerStateUnsupported: 
      NSLog(@"Device not supported"); 
      break; 
     default: 
      break; 
    } 
} 

我怎樣才能讓它開始實際做廣告並確保委託被調用?

+0

'[[CBPeripheralManager alloc] initWithDelegate:nil queue:nil]'在這裏代替nil,您應該通過'initWithDelegate:self' – Rajat

+0

,導致以下警告/ Users/mdouhan/Documents/dev/NWMobileTill/NWMobileTill/AdminView.m:110:80:將'AdminView * const __strong'發送到不兼容類型'id _Nullable'的參數 –

+0

您是否像這樣在類中定義了委託類'ClassName Rajat

回答

0

這裏在初始化CBPeripheralManager

_peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:nil queue:nil]; 

你逝去的委託爲nil,你應該通過爲self的對象。

所以更新的代碼應該是

_peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; 

此外,在類中定義的委託CBPeripheralManagerDelegate