@@ -73,9 +73,7 @@ public final class AsyncLocationManager {
7373 @available ( * , deprecated, message: " Use new function requestPermission(with:) " )
7474 public func requestAuthorizationWhenInUse( ) async -> CLAuthorizationStatus {
7575 let authorizationPerformer = RequestAuthorizationPerformer ( )
76- return await withTaskCancellationHandler {
77- proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
78- } operation: {
76+ return await withTaskCancellationHandler ( operation: {
7977 await withCheckedContinuation { continuation in
8078 let authorizationStatus = getAuthorizationStatus ( )
8179 if authorizationStatus != . notDetermined {
@@ -86,16 +84,16 @@ public final class AsyncLocationManager {
8684 locationManager. requestWhenInUseAuthorization ( )
8785 }
8886 }
89- }
87+ } , onCancel: {
88+ proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
89+ } )
9090 }
9191
9292#if !APPCLIP
9393 @available ( * , deprecated, message: " Use new function requestPermission(with:) " )
9494 public func requestAuthorizationAlways( ) async -> CLAuthorizationStatus {
9595 let authorizationPerformer = RequestAuthorizationPerformer ( )
96- return await withTaskCancellationHandler {
97- proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
98- } operation: {
96+ return await withTaskCancellationHandler ( operation: {
9997 await withCheckedContinuation { continuation in
10098 if #available( iOS 14 , * ) , locationManager. authorizationStatus != . notDetermined {
10199 continuation. resume ( with: . success( locationManager. authorizationStatus) )
@@ -105,20 +103,22 @@ public final class AsyncLocationManager {
105103 locationManager. requestAlwaysAuthorization ( )
106104 }
107105 }
108- }
106+ } , onCancel: {
107+ proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
108+ } )
109109 }
110110#endif
111111
112112 public func requestPermission( with permissionType: LocationPermission ) async -> CLAuthorizationStatus {
113113 switch permissionType {
114114 case . always:
115115 #if APPCLIP
116- return await requestAuthorizationWhenInUse ( )
116+ return await locationPermissionWhenInUse ( )
117117 #else
118- return await requestAuthorizationAlways ( )
118+ return await locationPermissionAlways ( )
119119 #endif
120120 case . whenInUsage:
121- return await requestAuthorizationWhenInUse ( )
121+ return await locationPermissionWhenInUse ( )
122122 }
123123 }
124124
@@ -141,14 +141,14 @@ public final class AsyncLocationManager {
141141
142142 public func requestLocation( ) async throws -> LocationUpdateEvent ? {
143143 let performer = SingleLocationUpdatePerformer ( )
144- return try await withTaskCancellationHandler ( handler: {
145- proxyDelegate. cancel ( for: performer. uniqueIdentifier)
146- } , operation: {
144+ return try await withTaskCancellationHandler ( operation: {
147145 return try await withCheckedThrowingContinuation ( { continuation in
148146 performer. linkContinuation ( continuation)
149147 self . proxyDelegate. addPerformer ( performer)
150148 self . locationManager. requestLocation ( )
151149 } )
150+ } , onCancel: {
151+ proxyDelegate. cancel ( for: performer. uniqueIdentifier)
152152 } )
153153 }
154154
@@ -228,3 +228,40 @@ public final class AsyncLocationManager {
228228 locationManager. stopRangingBeacons ( satisfying: satisfying)
229229 }
230230}
231+
232+ extension AsyncLocationManager {
233+ private func locationPermissionWhenInUse( ) async -> CLAuthorizationStatus {
234+ let authorizationPerformer = RequestAuthorizationPerformer ( )
235+ return await withTaskCancellationHandler ( operation: {
236+ await withCheckedContinuation { continuation in
237+ let authorizationStatus = getAuthorizationStatus ( )
238+ if authorizationStatus != . notDetermined {
239+ continuation. resume ( with: . success( authorizationStatus) )
240+ } else {
241+ authorizationPerformer. linkContinuation ( continuation)
242+ proxyDelegate. addPerformer ( authorizationPerformer)
243+ locationManager. requestWhenInUseAuthorization ( )
244+ }
245+ }
246+ } , onCancel: {
247+ proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
248+ } )
249+ }
250+
251+ private func locationPermissionAlways( ) async -> CLAuthorizationStatus {
252+ let authorizationPerformer = RequestAuthorizationPerformer ( )
253+ return await withTaskCancellationHandler ( operation: {
254+ await withCheckedContinuation { continuation in
255+ if #available( iOS 14 , * ) , locationManager. authorizationStatus != . notDetermined {
256+ continuation. resume ( with: . success( locationManager. authorizationStatus) )
257+ } else {
258+ authorizationPerformer. linkContinuation ( continuation)
259+ proxyDelegate. addPerformer ( authorizationPerformer)
260+ locationManager. requestAlwaysAuthorization ( )
261+ }
262+ }
263+ } , onCancel: {
264+ proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
265+ } )
266+ }
267+ }
0 commit comments