Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Fix permission requirements #22

Merged
merged 6 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## master

- move location permission request out of _ReactiveLocation_ (#22, kudos to @olejnjak)

## 4.0 beta 1

- completely new version (#19, kudos to @olejnjak)
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "ReactiveCocoa/ReactiveCocoa" "9.0.0"
github "ReactiveCocoa/ReactiveSwift" "5.0.0"
github "ReactiveCocoa/ReactiveSwift" "5.0.1"
github "antitypical/Result" "4.1.0"
16 changes: 5 additions & 11 deletions ReactiveLocation/ReactiveLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public protocol ReactiveLocationService {
}

public final class ReactiveLocation: NSObject, ReactiveLocationService, CLLocationManagerDelegate {
public static let shared = ReactiveLocation()
public typealias RequestPermissionCallback = (CLLocationManager) -> ()

public var locationManager: CLLocationManager { return _locationManager }
public var isVerbose = false

private let _locationManager: BetterLocationManager
private let observerLock = NSLock()
private let requestPermission: RequestPermissionCallback

private var observerCount = 0 {
didSet {
Expand Down Expand Up @@ -59,8 +60,9 @@ public final class ReactiveLocation: NSObject, ReactiveLocationService, CLLocati

// MARK: - Initializers

public override init() {
public init(requestPermission rp: @escaping RequestPermissionCallback) {
_locationManager = BetterLocationManager()
requestPermission = rp
super.init()
locationManager.delegate = self
}
Expand Down Expand Up @@ -98,15 +100,7 @@ public final class ReactiveLocation: NSObject, ReactiveLocationService, CLLocati
return
}

if Bundle.main.object(forInfoDictionaryKey: "NSLocationAlwaysAndWhenInUseUsageDescription") != nil {
locationManager.requestAlwaysAuthorization()
} else if Bundle.main.object(forInfoDictionaryKey: "NSLocationUsageDescription") != nil {
locationManager.requestAlwaysAuthorization()
} else if Bundle.main.object(forInfoDictionaryKey: "NSLocationAlwaysUsageDescription") != nil {
locationManager.requestAlwaysAuthorization()
} else if Bundle.main.object(forInfoDictionaryKey: "NSLocationWhenInUseUsageDescription") != nil {
locationManager.requestWhenInUseAuthorization()
}
self?.requestPermission(locationManager)
observer.send(value: ())
observer.sendCompleted()
}
Expand Down
4 changes: 3 additions & 1 deletion ReactiveLocationExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import UIKit
import ReactiveLocation

let reactiveLocation = ReactiveLocation { $0.requestWhenInUseAuthorization() } // but do your DI properly ☝️

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ReactiveLocation.shared.isVerbose = true
reactiveLocation.isVerbose = true
return true
}
}
2 changes: 1 addition & 1 deletion ReactiveLocationExample/LocationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class LocationViewController: UIViewController {
// MARK: - Private helpers

private func setupBindings() {
let coordinate = ReactiveLocation.shared.locationProducer()
let coordinate = reactiveLocation.locationProducer()
.map { $0.coordinate }

locationLabel.reactive.text <~ coordinate.map { String($0.latitude) + "," + String($0.longitude) }
Expand Down