-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(iOS): Making permissions switch statements exhaustive & supporting new iOS 14 cases #3160
Changes from 2 commits
2f14c57
7479349
78fc230
ac4bea8
56ffb24
08310b9
4e1bd25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,14 +35,16 @@ public class CAPPermissionsPlugin: CAPPlugin { | |
func checkCamera(_ call: CAPPluginCall) { | ||
let authStatus = AVCaptureDevice.authorizationStatus(for: .video) | ||
|
||
var ret = "prompt" | ||
let ret: String | ||
switch (authStatus) { | ||
case .notDetermined: | ||
ret = "prompt" | ||
case .denied, .restricted: | ||
ret = "denied" | ||
case .authorized: | ||
ret = "granted" | ||
case .notDetermined: | ||
fallthrough | ||
@unknown default: | ||
ret = "prompt" | ||
} | ||
|
||
call.resolve([ | ||
|
@@ -52,31 +54,42 @@ public class CAPPermissionsPlugin: CAPPlugin { | |
|
||
func checkPhotos(_ call: CAPPluginCall) { | ||
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus() | ||
|
||
var ret = "prompt" | ||
let ret: String | ||
switch (photoAuthorizationStatus) { | ||
case .notDetermined: | ||
ret = "prompt" | ||
case .denied, .restricted: | ||
ret = "denied" | ||
case .authorized: | ||
ret = "granted" | ||
case .denied, .restricted: | ||
ret = "denied" | ||
case .authorized: | ||
ret = "granted" | ||
#if swift(>=5.3) | ||
case .limited: | ||
// TODO: address this new case properly | ||
#warning(".limited != .authorized, authorization status should be revisted for iOS 14") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to the new case,
ikeith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ret = "granted" | ||
#endif | ||
case .notDetermined: | ||
fallthrough | ||
@unknown default: | ||
ret = "prompt" | ||
} | ||
|
||
call.resolve([ | ||
"state": ret | ||
]) | ||
} | ||
|
||
func checkGeolocation(_ call: CAPPluginCall) { | ||
var ret = "prompt" | ||
let ret: String | ||
if CLLocationManager.locationServicesEnabled() { | ||
switch CLLocationManager.authorizationStatus() { | ||
case .notDetermined: | ||
ret = "prompt" | ||
case .denied, .restricted: | ||
ret = "denied" | ||
case .authorizedAlways, .authorizedWhenInUse: | ||
ret = "granted" | ||
case .notDetermined: | ||
fallthrough | ||
@unknown default: | ||
ret = "prompt" | ||
} | ||
} else { | ||
ret = "denied" | ||
|
@@ -89,13 +102,19 @@ public class CAPPermissionsPlugin: CAPPlugin { | |
|
||
func checkNotifications(_ call: CAPPluginCall) { | ||
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { settings in | ||
var ret = "prompt" | ||
let ret: String | ||
switch settings.authorizationStatus { | ||
case .authorized, .provisional: | ||
ret = "granted" | ||
#if swift(>=5.3) | ||
case .ephemeral: | ||
ret = "granted" | ||
#endif | ||
case .denied: | ||
ret = "denied" | ||
case .notDetermined: | ||
fallthrough | ||
@unknown default: | ||
ret = "prompt" | ||
} | ||
|
||
|
@@ -114,14 +133,16 @@ public class CAPPermissionsPlugin: CAPPlugin { | |
func checkMicrophone(_ call: CAPPluginCall) { | ||
let microStatus = AVCaptureDevice.authorizationStatus(for: .audio) | ||
|
||
var ret = "prompt" | ||
let ret: String | ||
switch (microStatus) { | ||
case .authorized: | ||
ret = "granted" | ||
case .denied, .restricted: | ||
ret = "denied" | ||
case .notDetermined: | ||
ret = "prompt" | ||
case .authorized: | ||
ret = "granted" | ||
case .denied, .restricted: | ||
ret = "denied" | ||
case .notDetermined: | ||
fallthrough | ||
@unknown default: | ||
ret = "prompt" | ||
} | ||
|
||
call.resolve([ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new enum cases are not bound to Swift 5.3. They are added in iOS 14 and the availability check should check for os version instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, they are only available on iOS 14 if you use SDK 14 to compile (Xcode 12), if you use SDK 13 (Xcode 11) then they won't be available because it works in a sort of "compatibility mode".
The
#if swift(>=5.3)
is only true on Xcode 12, it's false in Xcode 11 because it ships with swift 5.1 to 5.2.4.I agree that using
if #available(iOS 14, *)
would be more clear, but sadly, it can't be used inside the switch.Maybe is less confusing if we use
#if compiler(>=5.3)
instead of#if swift(>=5.3)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is true that language version is not synonymous with the SDK version but in practice it's not an issue. The only way it would be a problem is if you downloaded a newer version of the Swift compiler and replaced the toolchain in Xcode 11. If you did that, you should know what you're doing and can expect side effects in many places.
Not only is
#available
invalid inside a switch statement (so we'd need to have duplicated switches to use it), it doesn't actually solve our problem because that's a hook to insert a runtime check, meaning the new enum case would fail to compile under Xcode 11. There is no compiler flag that I'm aware of to strip code based on the SDK or OS version being targeted.At this time, we need to be able to build cleanly with Xcode 11 and 12. Ultimately, this is only temporary for some months until 12 becomes the default.