@@ -73,9 +73,7 @@ public final class AsyncLocationManager {
73
73
@available ( * , deprecated, message: " Use new function requestPermission(with:) " )
74
74
public func requestAuthorizationWhenInUse( ) async -> CLAuthorizationStatus {
75
75
let authorizationPerformer = RequestAuthorizationPerformer ( )
76
- return await withTaskCancellationHandler {
77
- proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
78
- } operation: {
76
+ return await withTaskCancellationHandler ( operation: {
79
77
await withCheckedContinuation { continuation in
80
78
let authorizationStatus = getAuthorizationStatus ( )
81
79
if authorizationStatus != . notDetermined {
@@ -86,16 +84,16 @@ public final class AsyncLocationManager {
86
84
locationManager. requestWhenInUseAuthorization ( )
87
85
}
88
86
}
89
- }
87
+ } , onCancel: {
88
+ proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
89
+ } )
90
90
}
91
91
92
92
#if !APPCLIP
93
93
@available ( * , deprecated, message: " Use new function requestPermission(with:) " )
94
94
public func requestAuthorizationAlways( ) async -> CLAuthorizationStatus {
95
95
let authorizationPerformer = RequestAuthorizationPerformer ( )
96
- return await withTaskCancellationHandler {
97
- proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
98
- } operation: {
96
+ return await withTaskCancellationHandler ( operation: {
99
97
await withCheckedContinuation { continuation in
100
98
if #available( iOS 14 , * ) , locationManager. authorizationStatus != . notDetermined {
101
99
continuation. resume ( with: . success( locationManager. authorizationStatus) )
@@ -105,20 +103,22 @@ public final class AsyncLocationManager {
105
103
locationManager. requestAlwaysAuthorization ( )
106
104
}
107
105
}
108
- }
106
+ } , onCancel: {
107
+ proxyDelegate. cancel ( for: authorizationPerformer. uniqueIdentifier)
108
+ } )
109
109
}
110
110
#endif
111
111
112
112
public func requestPermission( with permissionType: LocationPermission ) async -> CLAuthorizationStatus {
113
113
switch permissionType {
114
114
case . always:
115
115
#if APPCLIP
116
- return await requestAuthorizationWhenInUse ( )
116
+ return await locationPermissionWhenInUse ( )
117
117
#else
118
- return await requestAuthorizationAlways ( )
118
+ return await locationPermissionAlways ( )
119
119
#endif
120
120
case . whenInUsage:
121
- return await requestAuthorizationWhenInUse ( )
121
+ return await locationPermissionWhenInUse ( )
122
122
}
123
123
}
124
124
@@ -141,14 +141,14 @@ public final class AsyncLocationManager {
141
141
142
142
public func requestLocation( ) async throws -> LocationUpdateEvent ? {
143
143
let performer = SingleLocationUpdatePerformer ( )
144
- return try await withTaskCancellationHandler ( handler: {
145
- proxyDelegate. cancel ( for: performer. uniqueIdentifier)
146
- } , operation: {
144
+ return try await withTaskCancellationHandler ( operation: {
147
145
return try await withCheckedThrowingContinuation ( { continuation in
148
146
performer. linkContinuation ( continuation)
149
147
self . proxyDelegate. addPerformer ( performer)
150
148
self . locationManager. requestLocation ( )
151
149
} )
150
+ } , onCancel: {
151
+ proxyDelegate. cancel ( for: performer. uniqueIdentifier)
152
152
} )
153
153
}
154
154
@@ -228,3 +228,40 @@ public final class AsyncLocationManager {
228
228
locationManager. stopRangingBeacons ( satisfying: satisfying)
229
229
}
230
230
}
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