简体   繁体   English

为什么我的代表从未打电话过?

[英]Why is my delegate never called?

I am trying to make my iPhone 7 Plus into an iBeacon for testing purposes 我正在尝试将iPhone 7 Plus变成iBeacon以进行测试

I am using the code below but my delegate is never called so it does not start the advertising 我正在使用下面的代码,但从未调用我的代表,因此它不会启动广告

- (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;
    }
}

How can I make it start to actually advertise and make sure the delegate is called? 如何使它开始实际做广告并确保已调用该代表?

Here while initializing the object of CBPeripheralManager 在这里初始化CBPeripheralManager的对象

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

you are passing the delegate as nil , which you should pass as self . 您将nil传递给该代表,则您应将其作为self传递。

So your updated code should be 所以您更新的代码应该是

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

Also, define the delegate CBPeripheralManagerDelegate in your class. 另外,在您的类中定义委托CBPeripheralManagerDelegate

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

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