From 2f14c57a000787539ec705d3b402a7475b7925c5 Mon Sep 17 00:00:00 2001 From: Ian Keith Date: Thu, 25 Jun 2020 13:12:51 -0400 Subject: [PATCH 1/4] Cleaned up permissions enum handling. Added unknown default handlers and new iOS 14 enum cases. --- .../Capacitor/Plugins/Permissions.swift | 63 ++++++++++++------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Permissions.swift b/ios/Capacitor/Capacitor/Plugins/Permissions.swift index 97b3ef419f..14e65d7599 100644 --- a/ios/Capacitor/Capacitor/Plugins/Permissions.swift +++ b/ios/Capacitor/Capacitor/Plugins/Permissions.swift @@ -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,16 +54,25 @@ 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") + ret = "granted" + #endif + case .notDetermined: + fallthrough + @unknown default: + ret = "prompt" } + call.resolve([ "state": ret ]) @@ -71,12 +82,14 @@ public class CAPPermissionsPlugin: CAPPlugin { var ret = "prompt" 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([ From 7479349eccb46637e644c61afb1a644953e19d4b Mon Sep 17 00:00:00 2001 From: Ian Keith Date: Thu, 25 Jun 2020 15:58:50 -0400 Subject: [PATCH 2/4] Missed a type declaration --- ios/Capacitor/Capacitor/Plugins/Permissions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Permissions.swift b/ios/Capacitor/Capacitor/Plugins/Permissions.swift index 14e65d7599..3b0366f15e 100644 --- a/ios/Capacitor/Capacitor/Plugins/Permissions.swift +++ b/ios/Capacitor/Capacitor/Plugins/Permissions.swift @@ -79,7 +79,7 @@ public class CAPPermissionsPlugin: CAPPlugin { } func checkGeolocation(_ call: CAPPluginCall) { - var ret = "prompt" + let ret: String if CLLocationManager.locationServicesEnabled() { switch CLLocationManager.authorizationStatus() { case .denied, .restricted: From 78fc230ed01a447ce5b5b7396574fff6dceca26d Mon Sep 17 00:00:00 2001 From: Ian Keith Date: Thu, 25 Jun 2020 16:27:31 -0400 Subject: [PATCH 3/4] Update ios/Capacitor/Capacitor/Plugins/Permissions.swift Co-authored-by: Dan Imhoff --- ios/Capacitor/Capacitor/Plugins/Permissions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Permissions.swift b/ios/Capacitor/Capacitor/Plugins/Permissions.swift index 3b0366f15e..9305332b9b 100644 --- a/ios/Capacitor/Capacitor/Plugins/Permissions.swift +++ b/ios/Capacitor/Capacitor/Plugins/Permissions.swift @@ -64,7 +64,7 @@ public class CAPPermissionsPlugin: CAPPlugin { #if swift(>=5.3) case .limited: // TODO: address this new case properly - #warning(".limited != .authorized, authorization status should be revisted for iOS 14") + #warning(".limited != .authorized, authorization status should be revisited for iOS 14") ret = "granted" #endif case .notDetermined: From ac4bea895ae154931af4d7be32695f2bada6621a Mon Sep 17 00:00:00 2001 From: Ian Keith Date: Tue, 30 Jun 2020 10:42:38 -0400 Subject: [PATCH 4/4] Changing @unknown default to be a failure case. --- .../Capacitor/Plugins/Permissions.swift | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Permissions.swift b/ios/Capacitor/Capacitor/Plugins/Permissions.swift index 3b0366f15e..22f26376ff 100644 --- a/ios/Capacitor/Capacitor/Plugins/Permissions.swift +++ b/ios/Capacitor/Capacitor/Plugins/Permissions.swift @@ -7,7 +7,8 @@ import UserNotifications */ @objc(CAPPermissionsPlugin) public class CAPPermissionsPlugin: CAPPlugin { - + fileprivate static let uknownPermissionValue = "Unknown permission value" + @objc func query(_ call: CAPPluginCall) { guard let name = call.getString("name") else { call.reject("Must provide a permission to check") @@ -42,9 +43,10 @@ public class CAPPermissionsPlugin: CAPPlugin { case .authorized: ret = "granted" case .notDetermined: - fallthrough - @unknown default: ret = "prompt" + @unknown default: + call.reject(CAPPermissionsPlugin.uknownPermissionValue) + return } call.resolve([ @@ -68,9 +70,10 @@ public class CAPPermissionsPlugin: CAPPlugin { ret = "granted" #endif case .notDetermined: - fallthrough - @unknown default: ret = "prompt" + @unknown default: + call.reject(CAPPermissionsPlugin.uknownPermissionValue) + return } call.resolve([ @@ -87,9 +90,10 @@ public class CAPPermissionsPlugin: CAPPlugin { case .authorizedAlways, .authorizedWhenInUse: ret = "granted" case .notDetermined: - fallthrough - @unknown default: ret = "prompt" + @unknown default: + call.reject(CAPPermissionsPlugin.uknownPermissionValue) + return } } else { ret = "denied" @@ -113,9 +117,10 @@ public class CAPPermissionsPlugin: CAPPlugin { case .denied: ret = "denied" case .notDetermined: - fallthrough - @unknown default: ret = "prompt" + @unknown default: + call.reject(CAPPermissionsPlugin.uknownPermissionValue) + return } call.resolve([ @@ -140,9 +145,10 @@ public class CAPPermissionsPlugin: CAPPlugin { case .denied, .restricted: ret = "denied" case .notDetermined: - fallthrough - @unknown default: ret = "prompt" + @unknown default: + call.reject(CAPPermissionsPlugin.uknownPermissionValue) + return } call.resolve([