From c476309e4335b2fd14c3d902f2e2076a62981e9b Mon Sep 17 00:00:00 2001 From: Ivan Vorobei Date: Mon, 2 Oct 2023 22:48:23 +0300 Subject: [PATCH] Merge location permissions to one permission with access. --- Package.swift | 32 ++----- PermissionsKit.podspec | 22 +---- README.md | 3 +- .../Handlers}/LocationAlwaysHandler.swift | 3 +- .../Handlers}/LocationWhenInUseHandler.swift | 3 +- .../LocationAccuracy.swift | 0 .../LocationPermission.swift} | 63 ++++++++++--- .../LocationWhenInUsePermission.swift | 89 ------------------- Sources/PermissionsKit/Data/Text.swift | 4 +- Sources/PermissionsKit/Permission.swift | 20 +++-- 10 files changed, 76 insertions(+), 163 deletions(-) rename Sources/{LocationAlwaysPermission => LocationPermission/Handlers}/LocationAlwaysHandler.swift (97%) rename Sources/{LocationWhenInUsePermission => LocationPermission/Handlers}/LocationWhenInUseHandler.swift (97%) rename Sources/{LocationExtension => LocationPermission}/LocationAccuracy.swift (100%) rename Sources/{LocationAlwaysPermission/LocationAlwaysPermission.swift => LocationPermission/LocationPermission.swift} (60%) delete mode 100644 Sources/LocationWhenInUsePermission/LocationWhenInUsePermission.swift diff --git a/Package.swift b/Package.swift index b9f2f9bb..280518bd 100644 --- a/Package.swift +++ b/Package.swift @@ -44,12 +44,8 @@ let package = Package( targets: ["SpeechRecognizerPermission"] ), .library( - name: "LocationWhenInUsePermission", - targets: ["LocationWhenInUsePermission"] - ), - .library( - name: "LocationAlwaysPermission", - targets: ["LocationAlwaysPermission"] + name: "LocationPermission", + targets: ["LocationPermission"] ), .library( name: "MotionPermission", @@ -91,12 +87,6 @@ let package = Package( .define("PERMISSIONSKIT_SPM") ] ), - .target( - name: "LocationExtension", - swiftSettings: [ - .define("PERMISSIONSKIT_SPM") - ] - ), .target( name: "CameraPermission", dependencies: [.target(name: "PermissionsKit")], @@ -162,24 +152,12 @@ let package = Package( ] ), .target( - name: "LocationWhenInUsePermission", - dependencies: [ - .target(name: "PermissionsKit"), - .target(name: "LocationExtension") - ], - swiftSettings: [ - .define("PERMISSIONSKIT_LOCATION_WHENINUSE"), - .define("PERMISSIONSKIT_SPM") - ] - ), - .target( - name: "LocationAlwaysPermission", + name: "LocationPermission", dependencies: [ - .target(name: "PermissionsKit"), - .target(name: "LocationExtension") + .target(name: "PermissionsKit") ], swiftSettings: [ - .define("PERMISSIONSKIT_LOCATION_ALWAYS"), + .define("PERMISSIONSKIT_LOCATION"), .define("PERMISSIONSKIT_SPM") ] ), diff --git a/PermissionsKit.podspec b/PermissionsKit.podspec index 93f1ecc2..62b03c3c 100644 --- a/PermissionsKit.podspec +++ b/PermissionsKit.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "PermissionsKit" - s.version = "9.2.2" + s.version = "10.0.0" s.summary = "Ask permissions with ready-use interface. You can check status permission and if it has been requested before. Support SwiftUI." s.homepage = "https://github.com/sparrowcode/PermissionsKit" s.source = { :git => "https://github.com/sparrowcode/PermissionsKit.git", :tag => s.version } @@ -92,26 +92,12 @@ Pod::Spec.new do |s| "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "PERMISSIONSKIT_SPEECH PERMISSIONSKIT_COCOAPODS" } end - - s.subspec 'LocationExtension' do |subspec| - subspec.source_files = "Sources/LocationExtension/**/*.swift" - end - s.subspec 'LocationWhenInUsePermission' do |subspec| - subspec.dependency 'PermissionsKit/Core' - subspec.dependency 'PermissionsKit/LocationExtension' - subspec.source_files = "Sources/LocationWhenInUsePermission/**/*.swift" - subspec.pod_target_xcconfig = { - "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "PERMISSIONSKIT_LOCATION_WHENINUSE PERMISSIONSKIT_COCOAPODS" - } - end - - s.subspec 'LocationAlwaysPermission' do |subspec| + s.subspec 'LocationPermission' do |subspec| subspec.dependency 'PermissionsKit/Core' - subspec.dependency 'PermissionsKit/LocationExtension' - subspec.source_files = "Sources/LocationAlwaysPermission/**/*.swift" + subspec.source_files = "Sources/LocationPermission/**/*.swift" subspec.pod_target_xcconfig = { - "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "PERMISSIONSKIT_LOCATION_ALWAYS PERMISSIONSKIT_COCOAPODS" + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "PERMISSIONSKIT_LOCATION PERMISSIONSKIT_COCOAPODS" } end diff --git a/README.md b/README.md index dc6d2ca3..8495a5f9 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,7 @@ Universal API for request permission and get its statuses. Available `.authorize | | Contacts | NSContactsUsageDescription | ✅ | ✅ | | | FaceID | NSFaceIDUsageDescription | ✅ | ✅ | | | Health | NSHealthUpdateUsageDescription, NSHealthShareUsageDescription | ✅ | ✅ | -| | Location Always | NSLocationAlwaysAndWhenInUseUsageDescription | ✅ | ✅ | -| | Location When In Use | NSLocationWhenInUseUsageDescription | ✅ | ✅ | +| | Location | NSLocationAlwaysAndWhenInUseUsageDescription NSLocationWhenInUseUsageDescription | ✅ | ✅ | | | Media Library | NSAppleMusicUsageDescription | ✅ | ✅ | | | Microphone | NSMicrophoneUsageDescription | ✅ | ✅ | | | Motion | NSMotionUsageDescription | ✅ | ✅ | diff --git a/Sources/LocationAlwaysPermission/LocationAlwaysHandler.swift b/Sources/LocationPermission/Handlers/LocationAlwaysHandler.swift similarity index 97% rename from Sources/LocationAlwaysPermission/LocationAlwaysHandler.swift rename to Sources/LocationPermission/Handlers/LocationAlwaysHandler.swift index 0b9a59a7..b0342fdc 100644 --- a/Sources/LocationAlwaysPermission/LocationAlwaysHandler.swift +++ b/Sources/LocationPermission/Handlers/LocationAlwaysHandler.swift @@ -21,10 +21,9 @@ #if PERMISSIONSKIT_SPM import PermissionsKit -import LocationExtension #endif -#if os(iOS) && PERMISSIONSKIT_LOCATION_ALWAYS +#if os(iOS) && PERMISSIONSKIT_LOCATION import Foundation import MapKit diff --git a/Sources/LocationWhenInUsePermission/LocationWhenInUseHandler.swift b/Sources/LocationPermission/Handlers/LocationWhenInUseHandler.swift similarity index 97% rename from Sources/LocationWhenInUsePermission/LocationWhenInUseHandler.swift rename to Sources/LocationPermission/Handlers/LocationWhenInUseHandler.swift index 029b0dde..f96fb7a8 100644 --- a/Sources/LocationWhenInUsePermission/LocationWhenInUseHandler.swift +++ b/Sources/LocationPermission/Handlers/LocationWhenInUseHandler.swift @@ -21,10 +21,9 @@ #if PERMISSIONSKIT_SPM import PermissionsKit -import LocationExtension #endif -#if PERMISSIONSKIT_LOCATION_WHENINUSE +#if PERMISSIONSKIT_LOCATION import Foundation import MapKit diff --git a/Sources/LocationExtension/LocationAccuracy.swift b/Sources/LocationPermission/LocationAccuracy.swift similarity index 100% rename from Sources/LocationExtension/LocationAccuracy.swift rename to Sources/LocationPermission/LocationAccuracy.swift diff --git a/Sources/LocationAlwaysPermission/LocationAlwaysPermission.swift b/Sources/LocationPermission/LocationPermission.swift similarity index 60% rename from Sources/LocationAlwaysPermission/LocationAlwaysPermission.swift rename to Sources/LocationPermission/LocationPermission.swift index 7c244a25..f318c6d6 100644 --- a/Sources/LocationAlwaysPermission/LocationAlwaysPermission.swift +++ b/Sources/LocationPermission/LocationPermission.swift @@ -21,24 +21,43 @@ #if PERMISSIONSKIT_SPM import PermissionsKit -import LocationExtension #endif -#if os(iOS) && PERMISSIONSKIT_LOCATION_ALWAYS +#if os(iOS) && PERMISSIONSKIT_LOCATION import Foundation -import MapKit +import EventKit public extension Permission { - - static var locationAlways: LocationAlwaysPermission { - return LocationAlwaysPermission() + + static func location(access: LocationAccess) -> LocationPermission { + LocationPermission(kind: .location(access: access)) } } -public class LocationAlwaysPermission: Permission { +public class LocationPermission: Permission { + + private var _kind: Permission.Kind - open override var kind: Permission.Kind { .locationAlways } - open var usageDescriptionKey: String? { "NSLocationAlwaysAndWhenInUseUsageDescription" } + // MARK: - Init + + init(kind: Permission.Kind) { + self._kind = kind + } + + open override var kind: Permission.Kind { self._kind } + open var usageDescriptionKey: String? { + switch _kind { + case .location(let access): + switch access { + case .whenInUse: + return "NSLocationWhenInUseUsageDescription" + case .always: + return "NSLocationAlwaysAndWhenInUseUsageDescription" + } + default: + fatalError() + } + } public override var status: Permission.Status { let authorizationStatus: CLAuthorizationStatus = { @@ -77,12 +96,28 @@ public class LocationAlwaysPermission: Permission { } public override func request(completion: @escaping () -> Void) { - LocationAlwaysHandler.shared = LocationAlwaysHandler() - LocationAlwaysHandler.shared?.requestPermission() { - DispatchQueue.main.async { - completion() - LocationAlwaysHandler.shared = nil + switch _kind { + case .location(let access): + switch access { + case .whenInUse: + LocationWhenInUseHandler.shared = LocationWhenInUseHandler() + LocationWhenInUseHandler.shared?.requestPermission() { + DispatchQueue.main.async { + completion() + LocationWhenInUseHandler.shared = nil + } + } + case .always: + LocationAlwaysHandler.shared = LocationAlwaysHandler() + LocationAlwaysHandler.shared?.requestPermission() { + DispatchQueue.main.async { + completion() + LocationAlwaysHandler.shared = nil + } + } } + default: + fatalError() } } } diff --git a/Sources/LocationWhenInUsePermission/LocationWhenInUsePermission.swift b/Sources/LocationWhenInUsePermission/LocationWhenInUsePermission.swift deleted file mode 100644 index effc7d51..00000000 --- a/Sources/LocationWhenInUsePermission/LocationWhenInUsePermission.swift +++ /dev/null @@ -1,89 +0,0 @@ -// The MIT License (MIT) -// Copyright © 2022 Sparrow Code LTD (https://sparrowcode.io, hello@sparrowcode.io) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#if PERMISSIONSKIT_SPM -import PermissionsKit -import LocationExtension -#endif - -#if PERMISSIONSKIT_LOCATION_WHENINUSE -import Foundation -import MapKit - -public extension Permission { - - static var locationWhenInUse: LocationWhenInUsePermission { - return LocationWhenInUsePermission() - } -} - -public class LocationWhenInUsePermission: Permission { - - open override var kind: Permission.Kind { .locationWhenInUse } - open var usageDescriptionKey: String? { "NSLocationAlwaysAndWhenInUseUsageDescription" } - - public override var status: Permission.Status { - let authorizationStatus: CLAuthorizationStatus = { - let locationManager = CLLocationManager() - if #available(iOS 14.0, tvOS 14.0, *) { - return locationManager.authorizationStatus - } else { - return CLLocationManager.authorizationStatus() - } - }() - - switch authorizationStatus { - #if os(iOS) - case .authorized: return .authorized - #endif - case .denied: return .denied - case .notDetermined: return .notDetermined - case .restricted: return .denied - case .authorizedAlways: return .authorized - case .authorizedWhenInUse: return .authorized - @unknown default: return .denied - } - } - - public var isPrecise: Bool { - #if os(iOS) - if #available(iOS 14.0, *) { - switch CLLocationManager().accuracyAuthorization { - case .fullAccuracy: return true - case .reducedAccuracy: return false - @unknown default: return false - } - } - #endif - return false - } - - public override func request(completion: @escaping () -> Void) { - LocationWhenInUseHandler.shared = LocationWhenInUseHandler() - LocationWhenInUseHandler.shared?.requestPermission() { - DispatchQueue.main.async { - completion() - LocationWhenInUseHandler.shared = nil - } - } - } -} -#endif diff --git a/Sources/PermissionsKit/Data/Text.swift b/Sources/PermissionsKit/Data/Text.swift index 53ba712a..b5e077c8 100644 --- a/Sources/PermissionsKit/Data/Text.swift +++ b/Sources/PermissionsKit/Data/Text.swift @@ -47,9 +47,9 @@ enum Texts { return NSLocalizedString("permission bluetooth name", bundle: bundle, comment: "") case .notification: return NSLocalizedString("permission notification name", bundle: bundle, comment: "") - case .locationWhenInUse: + case .location(access: .whenInUse): return NSLocalizedString("permission location when in use name", bundle: bundle, comment: "") - case .locationAlways: + case .location(access: .always): return NSLocalizedString("permission location always name", bundle: bundle, comment: "") case .tracking: return NSLocalizedString("permission tracking name", bundle: bundle, comment: "") diff --git a/Sources/PermissionsKit/Permission.swift b/Sources/PermissionsKit/Permission.swift index 851c67a1..753db022 100644 --- a/Sources/PermissionsKit/Permission.swift +++ b/Sources/PermissionsKit/Permission.swift @@ -109,9 +109,7 @@ open class Permission { case contacts case reminders case speech - // Upgrade location as calendar style - case locationWhenInUse - case locationAlways + case location(access: LocationAccess) case motion case mediaLibrary case bluetooth @@ -128,7 +126,9 @@ open class Permission { return "Photo Library" case .microphone: return "Microphone" - case .calendar(access: .write), .calendar(access: .full): + case .calendar(access: .write): + return "Calendar Only Write" + case .calendar(access: .full): return "Calendar" case .contacts: return "Contacts" @@ -136,8 +136,10 @@ open class Permission { return "Reminders" case .speech: return "Speech" - case .locationAlways: + case .location(access: .always): return "Location Always" + case .location(access: .whenInUse): + return "Location When Use" case .motion: return "Motion" case .mediaLibrary: @@ -146,8 +148,6 @@ open class Permission { return "Bluetooth" case .notification: return "Notification" - case .locationWhenInUse: - return "Location When Use" case .tracking: return "Tracking" case .faceID: @@ -165,4 +165,10 @@ open class Permission { case full case write } + + public enum LocationAccess { + + case whenInUse + case always + } }