RxCoreLocation 抽象了 Core Location 的 Rx 行为
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 RxCoreLocation 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'RxCoreLocation', '~> 1.5.1'
然后,运行以下命令
$ pod install
Carthage 是一个分散的依赖管理器,它可以自动将框架添加到您的 Cocoa 应用程序中。
您可以使用 Homebrew 通过以下命令安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 RxCoreLocation 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "RxSwiftCommunity/RxCoreLocation" ~> 1.5.1
要将 RxCoreLocation 用作 Swift Package Manager 包,只需在您的 Package.swift 文件中添加以下内容即可。
import PackageDescription
let package = Package(
name: "HelloRxCoreLocation",
dependencies: [
.Package(url: "https://github.com/RxSwiftCommunity/RxCoreLocation.git", "1.5.1")
]
)
如果您不想使用上述任何一个依赖管理器,您可以手动将 RxCoreLocation 集成到您的项目中。
cd
进入您的顶级项目目录,如果您的项目没有初始化为 git 仓库,请运行以下命令$ git init
$ git submodule add https://github.com/RxSwiftCommunity/RxCoreLocation.git
$ git submodule update --init --recursive
打开新的 RxCoreLocation
文件夹,然后将 RxCoreLocation.xcodeproj
拖到应用程序 Xcode 项目的项目导航器中。
它应该嵌套在应用程序的蓝色项目图标下。它在所有其他 Xcode 组之上还是之下无关紧要。
在项目导航器中选择 RxCoreLocation.xcodeproj
并验证部署目标是否与应用程序目标匹配。
接下来,在项目导航器中选择您的应用程序项目(蓝色项目图标)以导航到目标配置窗口,然后在侧边栏的“Targets”标题下选择应用程序目标。
在该窗口顶部的选项卡栏中,打开“General”面板。
单击“Embedded Binaries”部分下的 +
按钮。
您将看到两个不同的 RxCoreLocation.xcodeproj
文件夹,每个文件夹都有两个不同版本的 RxCoreLocation.framework
嵌套在 Products
文件夹中。
选择哪个
Products
文件夹无关紧要。
选择 RxCoreLocation.framework
。
就这样!
RxCoreLocation.framework
被自动添加为目标依赖项、链接框架和嵌入框架,并在复制文件构建阶段中,这是您在模拟器和设备上构建所需的一切。
+
按钮。RxCoreLocation.framework
。RxCoreLocation 暴露了许多 Apple Core Location API
,供您直接在应用程序中使用。
CLPlacemark
/// Setup CLLocationManager
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
manager.rx
.placemark
.subscribe(onNext: { placemark in
guard let name = placemark.name,
let isoCountryCode = placemark.isoCountryCode,
let country = placemark.country,
let postalCode = placemark.postalCode,
let locality = placemark.locality,
let subLocality = placemark.subLocality else {
return print("oops it looks like your placemark could not be computed")
}
print("name: \(name)")
print("isoCountryCode: \(isoCountryCode)")
print("country: \(country)")
print("postalCode: \(postalCode)")
print("locality: \(locality)")
print("subLocality: \(subLocality)")
})
.disposed(by: bag)
CLLocation
更新或 [CLLocation]
///Subscribing for a single location events
manager.rx
.location
.subscribe(onNext: { location in
guard let location = location else { return }
print("altitude: \(location.altitude)")
print("latitude: \(location.coordinate.latitude)")
print("longitude: \(location.coordinate.longitude)")
})
.disposed(by: bag)
///Subscribing for an array of location events
manager.rx
.didUpdateLocations
.subscribe(onNext: { _, locations in
guard !locations.isEmpty,
let currentLocation = locations.last else { return }
print("altitude: \(currentLocation.altitude)")
print("latitude: \(currentLocation.coordinate.latitude)")
print("longitude: \(currentLocation.coordinate.longitude)")
})
.disposed(by: bag)
CLAuthorizationStatus
并根据您的需要做出反应 ///Monitoring authorization changes
manager.rx
.didChangeAuthorization
.subscribe(onNext: {_, status in
switch status {
case .denied:
print("Authorization denied")
case .notDetermined:
print("Authorization: not determined")
case .restricted:
print("Authorization: restricted")
case .authorizedAlways, .authorizedWhenInUse:
print("All good fire request")
}
})
.disposed(by: bag)
RxCoreLocation 是在 MIT 许可证下发布的。 有关详细信息,请参阅 LICENSE。