AsyncLocationKit - Async/await CoreLocation With Swift

Overview

AsyncLocationKit

Wrapper for Apple CoreLocation framework with new Concurency Model. No more delegate patter or completion blocks.

import AsyncLocationKit

let asyncLocationManager = AsyncLocationManager(desiredAccuracy: .bestAccuracy)

Task {
    let permission = await self.asyncLocationManager.requestAuthorizationWhenInUse() //returns CLAuthorizationStatus
}

You can use all methods from Apple CLLocationManager.

Task {
    let coordinate = try await asyncLocationManager.requestLocation() //Request user location once
}

Start monitoring update of user location with AsyncStream.

Task {
    for await locationUpdateEvent in await asyncLocationManager.startUpdatingLocation() {
        switch locationUpdateEvent {
        case .didUpdateLocations(let locations):
            // do something
        case .didFailWith(let error):
            // do something
        case .didPaused, .didResume: 
            break
        }
    }
}

If Task was canceled, Stream finished automaticaly.

Comments
  • Suggestion: Deal with AsyncLocationManager constructed in async code

    Suggestion: Deal with AsyncLocationManager constructed in async code

    I have existing code based on PromiseKit that I am migrating to Swift Concurrency, and in migrating it to use AsyncLocationKit, my actual sequence of events differed from your sample code in the README.md. Because of this, I created an AsyncLocationManager instance inside an async function, it didn't work at all, due to that fact that AsyncLocationManager's initializer creates a CLLocationManager without first switching to the main thread. It took me a long time to figure out what was wrong. Because of this, I would suggest that AsyncLocationKit do one or more of the following:

    1. Include text in the main README.md stating that when an instance of AsyncLocationManager is created, that must happen in a thread with an active RunLoop (probably the main thread). Side note on this: a globally initialized AsyncLocationManager doesn't work (I'm guessing because it is initialized before the main thread's RunLoop is created).
    2. Include a check inside AsyncLocationManager.init that throws an exception if there isn't an active RunLoop on the current thread. (I don't actually know how to check this, since RunLoop.current will create an inactive RunLoop on the current thread when called on a thread with no RunLoop.)
    3. Update the first line of AsyncLocationManager.init to be wrapped in a call to DispatchQueue.main.sync() if it is called from anything other than the main thread. (Note: calling DispatchQueue.main.sync() on the main thread will result in deadlock, so this would need to check Thread.isMainThread.)
    4. Add DocC-compatible documentation to the library and make sure the documentation for AsyncLocationManager.init and AsyncLocationManager.init(desiredAccuracy:) states the fact that they must be called from a thread with a RunLoop.

    In short, given that AsyncLocationManager is designed to be used from async code, the fact that it must be constructed outside of async code is something that I feel should be either mentioned in the documentation, or corrected to not be the case. I realize that CLLocationManager itself makes it clear in its main class documentation that there must be a RunLoop at the time of initialization. But it is not at all clear (to me, anyway) that the same is also true of AsyncLocationManager, and AsyncLocationManager's very nature of being used with async code makes it much more likely to be created from an async function.

    opened by tcobbs-bentley 2
  • fix stuck authorization request

    fix stuck authorization request

    I managed to put in a workaround based on this approach. Looking forward to hearing your thoughts on the solution.

    This branch comes off my other branch for PR #25, so if you want to see only the changes for this PR, look at the last commit (a37d735).

    Fixes #30

    opened by humblehacker 1
  • Add accuracy authorization support (iOS 14+) and authorizationStatus monitoring

    Add accuracy authorization support (iOS 14+) and authorizationStatus monitoring

    I found your library easy to work with (nice work!), so I went ahead and added support for CLAccuracyAuthorization, and also added CLAuthorizationStatus monitoring. I also built a quick and dirty sample app that uses these new API - Locations

    Update: I also added support for getting and monitoring CLLocationManager.locationServicesEnabled()

    Update2: I also fixed a bug where requests for always authorization would be ignored if we already have when-in-use authorization. (See: Request Always Authorization After Getting When In Use)

    closes #24

    opened by humblehacker 1
  • Cannot use framework in AppClip

    Cannot use framework in AppClip

    Just by importing the library into my app Clip Target in Xcode I get the following error/warning when uploading to TestFlight/AppStore:

    ITMS-90842: Invalid SDK usage - App clip 'BuildTarget_AC_Base.app/AppClips/BuildTarget_AppClip.app' uses the SDK selector 'requestAlwaysAuthorization', which is not supported. Ensure your app clip is using supported SDK features.

    In the rest of my code I use a "#if APPCLIP" to skip code that reference API that are not allowed in AppClips. Is it possible to solve this in the framework as well?

    bug 
    opened by appfrilans 1
  • Can checkPermissions be public instead of private?

    Can checkPermissions be public instead of private?

    Discussed in https://github.com/AsyncSwift/AsyncLocationKit/discussions/17

    Originally posted by lmillett October 25, 2022 In my app I would like to check what location permissions have already been authorized before I ask them to grant me location permission. That way I'm not forced to ask for permissions before they have even used the app. I can delay it to a later point when they do an operation that really requires that authorization.

    I have that change made locally to my package and it seems to work as I would like it to.

    Happy to submit that change if others agree.

    feature 
    opened by gre4ixin 1
  • Requesting Always authorization never returns in some circumstances

    Requesting Always authorization never returns in some circumstances

    Steps:

    1. Starting with a clean app that has never requested location authorizations
    2. Request when-in-use authorization
    3. Tap "Allow Once"
    4. Request always authorization

    Expected:

    Always authorization request returns

    Observed:

    Always authorization request never returns, since the underlying call to CLLocationManager.requestAlwaysAuthorization() fails silently.

    This one is tricky, as there's no straightforward way to tell the difference between when the uses taps Allow While Using App – which will allow a follow-on call to CLLocationManager.requestAlwaysAuthorization() – and Allow Once – which will not. I'm investigating this approach.

    opened by humblehacker 0
  • Add allows background location update support

    Add allows background location update support

    This PR adds support for initialising and updating allowsBackgroundLocationUpdates. It also includes a test for the new code. This code builds on my previous PR by using default parameters in the initialisers.

    opened by AndrewDodd42 0
  • Fix desired accuracy initialiser

    Fix desired accuracy initialiser

    The convenience initialiser init(desiredAccuracy:) wasn't setting the desiredAccuracy of the wrapped LocationManager. This PR fixes the problem by using default parameters to remove the need for this initialiser.

    opened by AndrewDodd42 0
  • WatchOS Support

    WatchOS Support

    I started with ElyesDer's WatchOS commit 575745ad2b2e367080fbd81d2c53975f15ce9f9a and updated it to the latest base repository code. I've tested it on a real Series 5 Watch during a 25 minute run and it appears to work fine.

    close #23

    opened by AndrewDodd42 0
  • Support for WatchOS

    Support for WatchOS

    Could you add support for WatchOS? Currently it is failing to build because of references to regions, beacons and visits that are note available on the Watch.

    opened by AndrewDodd42 0
Releases(1.6.1)
  • 1.6.1(Dec 13, 2022)

  • 1.6.0(Dec 6, 2022)

    What's Changed

    • Delete the files generated by Xcode by @RomanPodymov in https://github.com/AsyncSwift/AsyncLocationKit/pull/33
    • Update .gitignore by @RomanPodymov in https://github.com/AsyncSwift/AsyncLocationKit/pull/34

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.5.7...1.6.0

    Source code(tar.gz)
    Source code(zip)
  • 1.5.7(Dec 5, 2022)

    What's Changed

    • Fix desired accuracy initialiser by @AndrewDodd42 in https://github.com/AsyncSwift/AsyncLocationKit/pull/27
    • WatchOS Support by @AndrewDodd42 in https://github.com/AsyncSwift/AsyncLocationKit/pull/26
    • Add allows background location update support by @AndrewDodd42 in https://github.com/AsyncSwift/AsyncLocationKit/pull/29
    • fix stuck authorization request by @humblehacker in https://github.com/AsyncSwift/AsyncLocationKit/pull/31
    • Add accuracy authorization support (iOS 14+) and authorizationStatus monitoring by @humblehacker in https://github.com/AsyncSwift/AsyncLocationKit/pull/25

    New Contributors

    • @AndrewDodd42 made their first contribution in https://github.com/AsyncSwift/AsyncLocationKit/pull/27
    • @humblehacker made their first contribution in https://github.com/AsyncSwift/AsyncLocationKit/pull/31

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.5.6...1.5.7

    Source code(tar.gz)
    Source code(zip)
  • 1.5.6(Nov 2, 2022)

  • 1.5.5(Oct 30, 2022)

  • 1.5.4(Oct 29, 2022)

    What's Changed

    • Check permission method by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/19
    • Rename method by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/20
    • Check app clip by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/22

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.5.2...1.5.4

    Source code(tar.gz)
    Source code(zip)
  • 1.5.3(Oct 28, 2022)

    What's Changed

    • Check permission method by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/19

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.5.2...1.5.3

    Source code(tar.gz)
    Source code(zip)
  • 1.5.2(Oct 25, 2022)

    What's Changed

    • Change readme.md by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/13
    • Fix readme by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/14
    • CocoaPods by @RomanPodymov in https://github.com/AsyncSwift/AsyncLocationKit/pull/15

    New Contributors

    • @RomanPodymov made their first contribution in https://github.com/AsyncSwift/AsyncLocationKit/pull/15

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.5.1...1.5.2

    Source code(tar.gz)
    Source code(zip)
  • 1.5.1(Sep 19, 2022)

    What's Changed

    • Update README.md by @tgmPaul in https://github.com/AsyncSwift/AsyncLocationKit/pull/9
    • Remove empty lines by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/12

    New Contributors

    • @tgmPaul made their first contribution in https://github.com/AsyncSwift/AsyncLocationKit/pull/9

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.0.5...1.5.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.5(Jun 12, 2022)

    What's Changed

    • 6 Support for updating accuracy by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/8

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.0.4...1.0.5

    Source code(tar.gz)
    Source code(zip)
  • 1.0.4(Jan 9, 2022)

    What's Changed

    • macOS support by @gre4ixin in https://github.com/AsyncSwift/AsyncLocationKit/pull/5

    Full Changelog: https://github.com/AsyncSwift/AsyncLocationKit/compare/1.0.3...1.0.4

    Source code(tar.gz)
    Source code(zip)
  • 1.0.3(Jan 8, 2022)

  • 1.0.2(Jan 5, 2022)

Owner
AsyncSwift
Async Swift Community
AsyncSwift
⏳ Collection of Swift 5.5 async/await utility functions.

⏳ FunAsync Collection of Swift 5.5 async/await utility functions. Throw <-> Result conversion asyncThrowsToAsyncResult asyncResultToAsyncThrows More C

Yasuhiro Inami 23 Oct 14, 2022
🎭 Swift async/await & Actor-powered effectful state-management framework.

?? Actomaton ??‍?? Actor + ?? Automaton = ?? Actomaton Actomaton is Swift async/await & Actor-powered effectful state-management framework inspired by

Yasuhiro Inami 199 Dec 20, 2022
A lightweight swift network layer with Combine, Async-Await, and a traditional completion block.

CombineNetwork A simple light-weight network library to make network requesting simpler. It supports newer techonology such as async/await as well as

Dushant Singh 4 Jan 3, 2022
Hydra: Lightweight full-featured Promises, Async-Await Library in Swift

Async Functions for ECMAScript The introduction of Promises and Generators in EC

Ecma TC39 1.6k Oct 17, 2022
Edit images and video with async / await in Swift, powered by Metal.

AsyncGraphics The core value type in AsyncGraphics is a Graphic. It's like an image, tho it can be used with various async methods. Documentation Swif

Anton Heestand 33 Dec 27, 2022
A Swift lib for network with async/await

SmileNetwork A Swift network utility with async/await applied UseAge enum MockEndpoint { case weather(cityId: String) } extension MockEndpoint: S

null 2 Jul 13, 2022
A simple network layer for use in small iOS projects with async/await support

SimpleNetwork Intro SimpleNetwork is simple network layer for use in small projects. Swift Package Manager Note: Instructions below are for using Swif

Alexandre Garrefa 1 Nov 30, 2021
Example project showing how to use async/await with iOS 13

ios13AsyncAwait Example project showing how to use async/await with iOS 13 Article This source code is a part of an article published at This Dev Brai

Michał Tynior 7 Oct 2, 2022
Using async / await on iOS 13: everything you need to know

Using async / await on iOS 13: everything you need to know! ?? Content This repository contains the code sample I've used during December 14th's lives

Vincent Pradeilles 11 Feb 1, 2022
iOS 13-compatible backports of commonly used async/await-based system APIs that are only available from iOS 15 by default.

AsyncCompatibilityKit Welcome to AsyncCompatibilityKit, a lightweight Swift package that adds iOS 13-compatible backports of commonly used async/await

John Sundell 367 Jan 5, 2023
straightforward networking and error handling with async-await and URLSession

AsyncAwaitNetworkingPlayground How To Run Just clone the project, open it and run. Some notes about AsyncAwaitNetworkingPlayground It's a straightforw

Fırat Yenidünya 17 Dec 11, 2022
AsyncTaskKit - contains some additions to async/await Task

AsyncTaskKit This repo contains some additions to async/await Task. In general i

Michał Zaborowski 0 Jan 2, 2022
Lightweight async/await networking library with interceptor support - usable from iOS 13+.

Lightweight async/await networking library with interceptor support. ?? Getting started AsyncNetwork's session acts as a wrapper to URLSession by addi

Paul 9 Oct 4, 2022
Test app for the InStat company. The project uses MVVM architecture, interface created with SwiftUI, network requests with async/await

FootStats Test app for the InStat company. The Project uses MVVM architecture, interface created with SwiftUI, network requests with async/await using

Nizami Tagiyev 4 Dec 15, 2022
⚡️ Fast async task based Swift framework with focus on type safety, concurrency and multi threading

Our apps constantly do work. The faster you react to user input and produce an output, the more likely is that the user will continue to use your appl

Said Sikira 814 Oct 30, 2022
Async and concurrent versions of Swift’s forEach, map, flatMap, and compactMap APIs.

CollectionConcurrencyKit Welcome to CollectionConcurrencyKit, a lightweight Swift package that adds asynchronous and concurrent versions of the standa

John Sundell 684 Jan 9, 2023
Swift TableView pagination with async API request.

SwiftTableViewPagination Swift TableView pagination with async API request. Output UML Create puml file. $ cd SwiftTableViewPagination/scripts/swiftum

y-okudera 1 Dec 26, 2021
AsyncExtensions aims to mimic Swift Combine operators for async sequences.

AsyncExtensions AsyncExtensions provides a collection of operators, async sequences and async streams that mimics Combine behaviour. The purpose is to

AsyncCommunity 200 Dec 27, 2022
A demonstration for bridging between Combine and your new async functions

CombineAsyncually This is a DEMONSTRATION of how you can bridge the new async / await functionality in Swift 5.5 with Combine. There is NO WARRANTY. T

null 48 Dec 4, 2022