Code Monkey home page Code Monkey logo

earendil's Introduction

Earendil

Swift Package for fetching approximate user location without asking for their permission 👺.

Get their country, subregion or continent with one method call. This includes country names, their coordinates and even MKCoordinateRegion property, instantly usable with your MapKit views. Perfect for setting your Map's location for the first time, if you don't need GPS permission otherwise.

100% App Store safe, no-private APIs used.

Usage

Get the location at any time:

let userLocation = Earendil.inferredLocation

userLocation.country.name // "United States of America"
userLocation.country.region.center // (37.09, -95.7)
userLocation.country.region.span // (60, 60)

Use the inferred location for your needs:

let userLocation = Earendil.inferredLocation

// use location data as the first-time region displayed by the map
mapView.setRegion(userLocation.country.coordinateRegion, animated: true)

// use location title (ENG only) as a content of any label in your app
navigationItem.title = userLocation.region.name

// use location for analytics purposes
FirebaseAnalytics.logEvent(UserLocationEvent(userLocation))

What? How come? Why?

It's super simple - the idea is to fetch current OS system Locale (NSLocale.current), cast it back to Foundation's NSLocale and retrieve the two-character countryCode key. This key is mapped to internal json dataset, which contains names and coordinates for every worlds-location. That way there is no need to ask for any location permission. You can check it out right here: InferredLocationProvider.swift.

This approach has some consequences, mainly:

  • Returned location (even for most granular level, country, is really very broad)
  • Users travelling to another country won't update Earendil's location (which may even be desired).
  • Setting location-agnostic Locale (such as eu instead of eu_ES or en instead of en_US) will prevent Earendil from getting proper location.

Installation

Available through Swift Package Manager - open up the xcode, type this repos URL and you are good to go.

Platform Availability

 iOS 14.0
 iPadOS 14.0
 macOS 11.0 Big Sur
 watchOS 7.0
 tvOS 14.0

also compliant with  Mac Catalyst

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.