According to the Swift 2.0 documentation for CLBeaconRegion, it should still be possible to pass the output of the peripheralDataWithMeasuredPower: method to the startAdvertising: method of CLPeripheralManager.
Getting Beacon Advertisement Data
- peripheralDataWithMeasuredPower:Retrieves data that can be used to advertise the current device as a beacon.
Declaration
SWIFT
func peripheralDataWithMeasuredPower(_measuredPower: NSNumber?) -> NSMutableDictionaryOBJECTIVE-C
- (NSMutableDictionary<NSString *,id> * _Nonnull)peripheralDataWithMeasuredPower:(NSNumber * _Nullable)measuredPowerParameters
measuredPowerThe received signal strength indicator (RSSI) value (measured in decibels) for the device. This value represents the measured strength of the beacon from one meter away and is used during ranging. Specifynilto use the default value for the device.Return Value
A dictionary of data that you can use in conjunction with a
CBPeripheralManagerto advertise the current device as a beacon.Discussion
The returned dictionary encodes the beacon’s identifying information along with other information needed to advertise the beacon. You should not need to access the dictionary contents directly. Pass the dictionary to the
startAdvertising:method of aCBPeripheralManagerto begin advertising the beacon.Availability
Available in iOS 7.0 and later.
However, peripheralDataWithMeasuredPower: returns an NSMutableDictionary whereas the startAdvertising: method of CLPeripheralManager accepts a Swift Dictionary of [String : AnyObject]?, although the documentation contends that it accepts an NSDictionary. The following code that worked in Swift 1.0:
// Set up a beacon region with the UUID, Major and Minor values
let region = CLBeaconRegion(proximityUUID:beaconUUID!, major:withMajor.unsignedShortValue, minor:withMinor.unsignedShortValue, identifier:"com.example.beacon")
// Attempt to set up a peripheral with the measured power
let peripheralData = region.peripheralDataWithMeasuredPower(withPower)
_peripheralManager!.startAdvertising(peripheralData)
In Swift 2.0 the same code fails to compile with a warning: fails to compile, with a warning:
NSDictionaryis not convertible to[String : AnyObject]; did you mean to useas!to force downcast?
However, forcing a downcast always fails.
Is this a documentation bug, a bug in Swift 2.0, or am I missing something?