RxCoreLocation

Platforms License

Swift Package Manager Carthage compatible CocoaPods compatible

Travis JetpackSwift

RxCoreLocation 抽象了 Core Location 的 Rx 行为

要求

安装

CocoaPods

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

Carthage 是一个分散的依赖管理器,它可以自动将框架添加到您的 Cocoa 应用程序中。

您可以使用 Homebrew 通过以下命令安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 将 RxCoreLocation 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

github "RxSwiftCommunity/RxCoreLocation" ~> 1.5.1

Swift Package Manager

要将 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 集成到您的项目中。

Git Submodules

$ git init
$ git submodule add https://github.com/RxSwiftCommunity/RxCoreLocation.git
$ git submodule update --init --recursive

RxCoreLocation.framework 被自动添加为目标依赖项、链接框架和嵌入框架,并在复制文件构建阶段中,这是您在模拟器和设备上构建所需的一切。

Embeded Binaries (嵌入式二进制文件)

用法

RxCoreLocation 暴露了许多 Apple Core Location API,供您直接在应用程序中使用。

    /// 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)
    ///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)
    ///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 许可证下发布的。 有关详细信息,请参阅 LICENSETry me