Skip to content

perseusrealdeal/PerseusGeoLocationKit

Repository files navigation

PerseusGeoLocationKit — Xcode 10.1+

PerseusGeoLocationKit released as the component for both macOS and iOS apps as well.

Actions Status Version Pod Platforms Xcode 10.1 Swift 4.2 License

Integration Capabilities

Standalone Carthage compatible CocoaPods manager Swift Package Manager compatible

In Brief

Collection of tools for easy dealing with native geo location services.

Features:

Requirements

First-party software

Third-party software

Installation

Using "Exact" with the Version field is strongly recommended.

Standalone

Make a copy of the file PerseusGeoLocationSingle.swift then put it into a place required of a host project.

Carthage

Cartfile should contain:

github "perseusrealdeal/PerseusGeoLocationKit" == 0.1.0

Some Carthage usage tips placed here.

CocoaPods

Podfile should contain:

target "ProjectTarget" do
  use_frameworks!
  pod 'PerseusGeoLocationKit', '0.1.0'
end

Swift Package Manager

  • 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.

Usage

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()
}

License MIT

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.

Author

PerseusGeoLocationKit was written at Novosibirsk by Mikhail Zhigulin i.e. me, mzhigulin@gmail.com.

About

Swift Location Dealing lib. macOS 10.9+, iOS 9.3+, Xcode 10.1+.

Resources

License

Stars

Watchers

Forks