我正在通过使用 HMatalog 示例应用程序来实现 Homekit 应用程序。一切都很好,但在添加附件视图中,我们使用EAWiFiUnconfiguredAccessoryBrowser来检测外部附件。在这第一次一切正常,但如果我回去再出来,它不会显示外部配件。如果我从后台删除 HMCatalog 应用程序并重新启动,那么它工作正常。据我了解,应用程序中有一些缓存。但是我没有在应用程序中找到任何与缓存相关的代码。感谢您宝贵的时间。请帮我。
代码是:
// We can't use the ExternalAccessory framework on the iPhone simulator.
#if !TARGET_IPHONE_SIMULATOR
self.externalAccessoryBrowser = [[EAWiFiUnconfiguredAccessoryBrowser alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
#endif
[self startBrowsing];
#pragma mark - EAWiFiUnconfiguredAccessoryBrowserDelegate methods
- (void)accessoryBrowser:(EAWiFiUnconfiguredAccessoryBrowser *)browser didFindUnconfiguredAccessories:(NSSet *)accessories
{
[self reloadTable];
}
- (void)reloadTable
{
[self resetDisplayedAccessories];
dispatch_async(dispatch_get_main_queue(), ^{
[allDevicesTableView reloadData];
});
}
- (void)resetDisplayedAccessories
{
[self.displayedAccessories removeAllObjects];
[self.displayedAccessories addObjectsFromArray:[self allAccessories]];
}
- (NSArray *)allAccessories
{
NSMutableArray *allAccessories = [[NSMutableArray alloc] initWithCapacity:0];
NSArray *discoveredAccessories = self.accessoryBrowser.discoveredAccessories;
if (discoveredAccessories)
{
[allAccessories addObjectsFromArray:discoveredAccessories];
}
[allAccessories addObjectsFromArray:self.addedAccessories];
NSArray *externalAccessories = self.externalAccessoryBrowser.unconfiguredAccessories.allObjects;
if (externalAccessories)
{
// ExternalAcessory framework may still contain an accessory for a little while after it's been configured.
// If there is a HomeKit accessory with the same name as any external accessory, filter out the external accessory.
NSPredicate *existingHomeKitAccessoryPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return ![[allAccessories valueForKeyPath:@"@distinctUnionOfObjects.name"] containsObject:evaluatedObject];
}];
NSArray *filteredExternalAccessories = [externalAccessories filteredArrayUsingPredicate:existingHomeKitAccessoryPredicate];
[allAccessories addObjectsFromArray:filteredExternalAccessories];
}
NSSortDescriptor *nameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(caseInsensitiveCompare:)];
return [allAccessories sortedArrayUsingDescriptors:@[nameDescriptor]];
}