iOS13のiBeacon関連の変更点

iOS13でiBeacon関連項目が一部変更になっていたので、まとめました。

# `CLBeaconRegion` のプロパティが変更された

iOS12までは `proximityUUID`
iOS13からは `uuid`
※ 名称変更のみ

```swift
@available(iOS 13.0, *)
public init(uuid: UUID, identifier: String)

@available(iOS, introduced: 7.0, deprecated: 13.0)
public init(proximityUUID: UUID, identifier: String)
```

### サンプル
```swift
var beaconRegion: CLBeaconRegion {
    if #available(iOS 13.0, *) {
        return CLBeaconRegion(uuid: UUID(uuidString: "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")!, identifier: "id")
    } else {
        return CLBeaconRegion(proximityUUID: UUID(uuidString: "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")!, identifier: "id")
    }
}
```

---

# `CLBeaconRegion`のプロパティに `beaconIdentityConstraint` が追加された

```swift
open class CLBeaconRegion : CLRegion {

    @available(iOS 13.0, *)
    @NSCopying open var beaconIdentityConstraint: CLBeaconIdentityConstraint { get }

    @available(iOS 13.0, *)
    open var uuid: UUID { get }

    @available(iOS, introduced: 7.0, deprecated: 13.0)
    open var proximityUUID: UUID { get }

    @NSCopying open var major: NSNumber? { get }

    @NSCopying open var minor: NSNumber? { get }

    open var notifyEntryStateOnDisplay: Bool
}
```

# `CLBeaconIdentityConstraint` クラス
iOS12までは `Ranging` は `CLBeaconRegion` クラスを用いて行っていましたが、
iOS13からはこのクラスを用いて行ってくれってことっぽいです。

```swift
@available(iOS 13.0, *)
open class CLBeaconIdentityConstraint : NSObject, NSCopying, NSSecureCoding {

    open var uuid: UUID { get }

    @NSCopying open var __major: NSNumber? { get }

    @NSCopying open var __minor: NSNumber? { get }

    public init(uuid: UUID)

    public init(uuid: UUID, major: CLBeaconMajorValue)

    public init(uuid: UUID, major: CLBeaconMajorValue, minor: CLBeaconMinorValue)
}

@available(iOS 13.0, *)
extension CLBeaconIdentityConstraint {

    public var major: UInt16? { get }

    public var minor: UInt16? { get }
}

```

# 具体的には
## `CLBeaconRegion` が `CLBeaconIdentityConstraint` で `init` できるようになった

```swift
@available(iOS 13.0, *)
public init(beaconIdentityConstraint: CLBeaconIdentityConstraint, identifier: String)
```

### サンプル
```swift
var beaconRegion = CLBeaconRegion(beaconIdentityConstraint: CLBeaconIdentityConstraint(uuid: UUID(uuidString: "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")!), identifier: "id")
```

# 一部のメソッドの引数が `CLBeaconRegion` から `CLBeaconIdentityConstraint` に置き換わった

下記メソッドでは
iOS12までは `CLBeaconRegion` クラスを引数にしていたが、
iOS13からは `CLBeaconIdentityConstraint` クラスが引数になった。  

## `RangingBeacons`
```swift
@available(iOS 13.0, *)
open func startRangingBeacons(satisfying constraint: CLBeaconIdentityConstraint)

@available(iOS 13.0, *)
open func stopRangingBeacons(satisfying constraint: CLBeaconIdentityConstraint)

@available(iOS, introduced: 7.0, deprecated: 13.0, message: "Use -startRangingBeaconsSatisfyingConstraint:")
open func startRangingBeacons(in region: CLBeaconRegion)

@available(iOS, introduced: 7.0, deprecated: 13.0, message: "Use -stopRangingBeaconsSatisfyingConstraint:")
open func stopRangingBeacons(in region: CLBeaconRegion)
```

### サンプル
```swift
func startRangingBeacons() {
    if #available(iOS 13.0, *) {
        locationManager.startRangingBeacons(satisfying: beaconRegion.beaconIdentityConstraint)
    } else {
        locationManager.startRangingBeacons(in: beaconRegion)
    }
}

func stopRangingBeacons() {
    if #available(iOS 13.0, *) {
        locationManager.stopRangingBeacons(satisfying: beaconRegion.beaconIdentityConstraint)
    } else {
        locationManager.stopRangingBeacons(in: beaconRegion)
    }
    
}

```

## `didRangeBeacons`
```objc
- (void)locationManager:(CLLocationManager *)manager
		didRangeBeacons:(NSArray<CLBeacon *> *)beacons
   satisfyingConstraint:(CLBeaconIdentityConstraint *)beaconConstraint API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(watchos, tvos, macos);

- (void)locationManager:(CLLocationManager *)manager
		didRangeBeacons:(NSArray<CLBeacon *> *)beacons
			   inRegion:(CLBeaconRegion *)region API_DEPRECATED_WITH_REPLACEMENT("Use locationManager:didRangeBeacons:satisfyingConstraint:", ios(7.0, 13.0)) API_UNAVAILABLE(macos, macCatalyst) API_UNAVAILABLE(watchos, tvos);
```

### サンプル
```swift
@available(iOS 13, *)
func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
    
}

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
    
}
```

## `rangingBeaconsDidFailForRegion`
```objc
- (void)locationManager:(CLLocationManager *)manager
didFailRangingBeaconsForConstraint:(CLBeaconIdentityConstraint *)beaconConstraint
				  error:(NSError *)error API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(watchos, tvos, macos);

- (void)locationManager:(CLLocationManager *)manager
rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region
			  withError:(NSError *)error API_DEPRECATED_WITH_REPLACEMENT("Use locationManager:didFailRangingBeaconsForConstraint:error:", ios(7.0, 13.0)) API_UNAVAILABLE(macos, macCatalyst) API_UNAVAILABLE(watchos, tvos);
```

### サンプル

```swift
@available(iOS 13, *)
func locationManager(_ manager: CLLocationManager, didFailRangingFor beaconConstraint: CLBeaconIdentityConstraint, error: Error) {
    
}

func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
    
}
```

# 公式のサンプルコード
https://developer.apple.com/documentation/corelocation/ranging_for_beacons

# WWDC19のセッション
https://developer.apple.com/videos/play/wwdc2019/705/