【问题标题】:Add additional information to a region. iBeacons向区域添加附加信息。信标
【发布时间】:2013-10-28 23:28:13
【问题描述】:

我希望能够在初始化CLBeaconRegion 时添加更多信息,例如数组或字符串,以便我可以在didRangeBeacons 方法中接收它。 (非主要或次要)

目前看起来是这样的:

_advertRegion = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"003-002-001"];

但我真的很想像这样或类似的方式初始化它:

_advertRegion = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"003-002-001" setArrayOrSomething:myArray];

而且我显然应该能够从该地区获取信息,例如:

[region getArray];

当然,不一定要那样,只要你有一个想法,我“需要”什么。

我的尝试

  • 我尝试通过objc_setAssociatedObject 设置/获取它
  • 我尝试通过setValue forKey 设置它

【问题讨论】:

    标签: objective-c core-location cllocationmanager core-bluetooth ibeacon


    【解决方案1】:

    我建议您只使用一个单独的 NSDictionary 实例,该实例与您在构建 CLBeaconRegion 时使用的标识符相同。

    像这样:

    // Make this a class variable, or make it part of a singleton object
    NSDictionary *beaconRegionData = [[NSDictionary alloc] init];
    
    // Here is the data you want to attach to the region
    NSMutableArray *myArray = [[[NSMutableArray] alloc] init];
    
    // and here is your region
    _advertRegion = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"003-002-001"];
    
    // attach your data to the NSDictionary instead
    [beaconRegionData setValue:myArray forKey:_advertRegion.identifier];
    
    // and you can get it like this    
    NSLog(@"Here is my array: %@", [beaconRegionData valueForKey:_advertRegion.identifier]);
    

    【讨论】:

    • 作为简化,CLBeacon 符合 NSCopying 协议,所以据我所知,它可以直接用作字典中的键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2013-05-17
    • 2021-10-03
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多