PerseusGeoLocationKit released as the component for both macOS and iOS apps as well.
Collection of tools for easy dealing with native geo location services.
Features:
- Location data delivery via Notification center by subscription.
- Custom permit calculation to take control of Location services access.
- PerseusLocationDealer as Singleton.
- macOS 10.13.6+
- Xcode 10.1+
- Swift 4.2+
- iOS: 9.3+, UIKit SDK
- macOS: 10.9+, AppKit SDK
- SwiftLint Shell Script Runner
- SwiftLint / 0.31.0: Busy Laundromat for macOS High Sierra
Using "Exact" with the Version field is strongly recommended.
Make a copy of the file PerseusGeoLocationSingle.swift
then put it into a place required of a host project.
Cartfile should contain:
github "perseusrealdeal/PerseusGeoLocationKit" == 0.1.0
Some Carthage usage tips placed here.
Podfile should contain:
target "ProjectTarget" do
use_frameworks!
pod 'PerseusGeoLocationKit', '0.1.0'
end
- As a package dependency so Package.swift should contain the following statements:
dependencies: [
.package(url: "https://github.com/perseusrealdeal/PerseusGeoLocationKit.git",
.exact("0.1.0"))
],
- As an Xcode project dependency:
Project in the Navigator > Package Dependencies > Add Package Dependency
Using "Exact" with the Version field is strongly recommended.
Step 1:
Get ready for location services
Info.plist | iOS | macOS | PerseusLocationDealer's method |
---|---|---|---|
NSLocationUsageDescription | optional | askForAuthorization() | |
NSLocationAlwaysUsageDescription | required | askForAuthorization() | |
NSLocationWhenInUseUsageDescription | required | askForAuthorization(.whenInUse) |
Recomendation for macOS only:
PerseusLocationDealer should be loaded in launch time on macOS.
To do so create a reference to the PerseusLocationDealer instance as a property in a class that is also allocated in launch time such as AppDelegate. Take a look at the following sample statements.
class AppDelegate: NSObject, NSApplicationDelegate {
let locationDealer = PerseusLocationDealer.shared // Once only is enough.
func applicationDidFinishLaunching(_ aNotification: Notification) {
...
Step 2:
Create a notification observer then ask for a value
Notification name | PerseusLocationDealer's method | Value |
---|---|---|
.locationDealerCurrentNotification | askForCurrentLocation(_ :) | current location |
.locationDealerUpdatesNotification | askToStartUpdatingLocation(_ :) | location changes |
.locationDealerStatusChangedNotification | askForAuthorization(_ :, _ :) | permission |
.locationDealerErrorNotification | error |
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
NotificationCenter.default.addObserver(
self,
selector: #selector(locationDealerCurrentHandler(_:)),
name: .locationDealerCurrentNotification,
object: nil
)
}
@objc private func locationDealerCurrentHandler(_ notification: Notification) {
guard
let result = notification.object as? Result<PerseusLocation, LocationDealerError>
else { return }
switch result {
case .success(let data):
log.message("\(data)")
case .failure(let error):
log.message("\(error)", .error)
}
}
Step 3:
Ask for value, authorization and then current location
@IBAction func buttonLocationPermissionTapped(_ sender: NSButton) {
PerseusLocationDealer.shared.askForAuthorization { permit in
let text = "[\(type(of: self))].\(#function) — It's already determined .\(permit)"
log.message(text, .error)
}
}
@IBAction func buttonCurrentLocationTapped(_ sender: NSButton) {
try? PerseusLocationDealer.shared.askForCurrentLocation()
}
All files from this repository is under license based on MIT.
Copyright © 7531 Mikhail Zhigulin of Novosibirsk.
- The year starts from the creation of the world according to a Slavic calendar
- September, the 1st of Slavic year
LICENSE for details.
PerseusGeoLocationKit
was written at Novosibirsk by Mikhail Zhigulin i.e. me, mzhigulin@gmail.com.