HMAccessoryDe​​legates不调用Button操作

时间:2015-06-18 07:57:22

标签: ios objective-c homekit ios-homekit

我正在开发Homekit iOS应用程序。我有一个问题,我有一个附件,当我使用HomeKit模拟器更改其功率特性值时,HMAccessory的代表正在调用但是如果我以编程方式更改powr特征值(使用writevalue),则不会调用委托方法。请让我知道任何建议的想法。

Code 

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    selectedDevice.delegate = self;
}

HMAccessoryDe​​legate

- (void)accessory:(HMAccessory *)accessory service:(HMService *)service didUpdateValueForCharacteristic:(HMCharacteristic *)characteristic;
{
    NSLog(@"changed");
}

写入功能

UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(230, 5, 51, 31)];
[cell addSubview:sw];
sw.on = YES;
[sw addTarget:self action:@selector(updateState:) forControlEvents:UIControlEventValueChanged];

-(void)updateState:(UISwitch*)sender
{
HMCharacteristic *characteristic = self.selectedService.characteristics[tag];

[characteristic enableNotification:YES completionHandler:^(NSError *error)
             {
                 if(!error)
                 {

                 }
             }];
 if([characteristic.characteristicType isEqualToString:HMCharacteristicTypePowerState])
        {
        id val = characteristic.value;
        NSString *str = [NSString stringWithFormat:@"%@",val];

        if([str isEqualToString:@"0"])
        {
            id a = characteristic.value;

            BOOL b = [a boolValue];

            NSNumber *c = [NSNumber numberWithBool:!b];

            AppDelegate *appDel = [[UIApplication sharedApplication] delegate];

            [characteristic writeValue:c completionHandler:^(NSError *error) {
                if (error) {
                    UIAlertView *alertController = [[UIAlertView alloc] initWithTitle:@"Error" message:[appDel handleErrorCodes:error.code] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alertController show];
                    return;
                }
                else
                {
                    [serviceCharacteristicsTableView reloadData];
                }
            }];

        }
}

如果我不清楚,请告诉我

1 个答案:

答案 0 :(得分:1)

documentation表示在以编程方式设置值时未调用委托方法:

  

由于值的变化而调用此方法   配件。不会导致应用程序启动的程序化更改   在这个方法被调用。

如果您想在写入特征值成功(或失败)后执行某些操作,则可以在completionHandler:方法的writeValue:completionHandler:块中执行此操作。