Skip to content

Commit 969a7c5

Browse files
committed
Update to 4.0
Add macros for check imported permissions.
1 parent a7444ab commit 969a7c5

14 files changed

+781
-356
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,18 @@ Swift `4.2` & `5.0`. Ready for use on iOS 10+
5050
[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate `SPPermission` into your Xcode project using CocoaPods, specify it in your `Podfile`:
5151

5252
```ruby
53-
pod 'SPPermission'
53+
pod 'SPPermission/Notification'
5454
```
5555

56+
Due to Apple's new policy regarding permission access you need to specifically define what kind of permissions you want to access using subspecs. For example if you want to access `Camera` and `Location` you define the following:
57+
58+
```ruby
59+
pod 'SPPermission/Camera'
60+
pod 'SPPermission/Location'
61+
```
62+
63+
See `Permission.podspec` for more information about which subspecs are available.
64+
5665
### Carthage
5766

5867
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate `SPPermission` into your Xcode project using Carthage, specify it in your `Cartfile`:
@@ -261,7 +270,7 @@ SPPermission.Dialog.request(
261270

262271
## Purpose String in Info.plist
263272

264-
`SPPermission` uses many permissions in one library; you need to add some strings to the `Info.plist` file with description. List of keys:
273+
You need to add some strings to the `Info.plist` file with description. List of keys:
265274

266275
- NSContactsUsageDescription
267276
- NSCalendarsUsageDescription
@@ -274,7 +283,6 @@ SPPermission.Dialog.request(
274283
- NSLocationAlwaysUsageDescription (iOS 10 and earlier)
275284

276285
Do not use the description as the name of the key - this causes errors in the latest version of the new Xcode. Specify `For SPPermission` in the description.
277-
If I forgot to mention some, please let me know and create [issue](https://github.com/ivanvorobei/SPPermission/issues) or [pull request](https://github.com/ivanvorobei/SPPermission/pulls).
278286

279287
## How I do UI
280288

SPPermission.podspec

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "SPPermission"
4-
s.version = "3.1.1"
4+
s.version = "4.0"
55
s.summary = "Request permissions with dialog. For customise implement DataSource protocol. Check permissions."
66
s.homepage = "https://github.com/IvanVorobei/RequestPermission"
77
s.source = { :git => "https://github.com/IvanVorobei/RequestPermission.git", :tag => s.version }
@@ -13,7 +13,67 @@ Pod::Spec.new do |s|
1313
s.ios.framework = 'UIKit'
1414
s.swift_version = ['4.2', '5.0']
1515
s.ios.deployment_target = "10.0"
16+
s.requires_arc = true
1617

17-
s.source_files = "Source/SPPermission/**/*.swift"
18+
s.default_subspec = 'Core'
19+
20+
s.subspec 'Core' do |co|
21+
co.source_files = "Source/SPPermission/**/*.swift"
22+
end
23+
24+
s.subspec 'Camera' do |cm|
25+
cm.dependency 'SPPermission/Core'
26+
cm.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_CAMERA" }
27+
end
28+
29+
s.subspec 'PhotoLibrary' do |pl|
30+
pl.dependency 'SPPermission/Core'
31+
pl.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_PHOTOLIBRARY" }
32+
end
33+
34+
s.subspec 'Notification' do |no|
35+
no.dependency 'SPPermission/Core'
36+
no.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_NOTIFICATION" }
37+
end
38+
39+
s.subspec 'Microphone' do |mi|
40+
mi.dependency 'SPPermission/Core'
41+
mi.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_MICROPHONE" }
42+
end
43+
44+
s.subspec 'Calendar' do |ca|
45+
ca.dependency 'SPPermission/Core'
46+
ca.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_CALENDAR" }
47+
end
48+
49+
s.subspec 'Contacts' do |cn|
50+
cn.dependency 'SPPermission/Core'
51+
cn.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_CONTACTS" }
52+
end
53+
54+
s.subspec 'Reminders' do |re|
55+
re.dependency 'SPPermission/Core'
56+
re.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_REMINDERS" }
57+
end
58+
59+
s.subspec 'SpeechRecognizer' do |sr|
60+
sr.dependency 'SPPermission/Core'
61+
sr.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_SPEECH" }
62+
end
63+
64+
s.subspec 'Location' do |lo|
65+
lo.dependency 'SPPermission/Core'
66+
lo.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_LOCATION" }
67+
end
68+
69+
s.subspec 'Motion' do |mo|
70+
mo.dependency 'SPPermission/Core'
71+
mo.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_MOTION" }
72+
end
73+
74+
s.subspec 'MediaLibrary' do |ml|
75+
ml.dependency 'SPPermission/Core'
76+
ml.pod_target_xcconfig = { "SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "SPPERMISSION_MEDIALIBRARY" }
77+
end
1878

1979
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2017 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
#if SPPERMISSION_CALENDAR
23+
24+
import UIKit
25+
import EventKit
26+
27+
extension SPPermission {
28+
29+
struct SPCalendarPermission: SPPermissionInterface {
30+
31+
var isAuthorized: Bool {
32+
return EKEventStore.authorizationStatus(for: EKEntityType.event) == .authorized
33+
}
34+
35+
var isDenied: Bool {
36+
return EKEventStore.authorizationStatus(for: EKEntityType.event) == .denied
37+
}
38+
39+
func request(withCompletionHandler сompletionHandler: @escaping ()->()?) {
40+
let eventStore = EKEventStore()
41+
eventStore.requestAccess(to: EKEntityType.event, completion: {
42+
(accessGranted: Bool, error: Error?) in
43+
DispatchQueue.main.async {
44+
сompletionHandler()
45+
}
46+
})
47+
}
48+
}
49+
}
50+
51+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2017 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
#if SPPERMISSION_CAMERA
23+
24+
import UIKit
25+
import AVFoundation
26+
27+
extension SPPermission {
28+
29+
struct SPCameraPermission: SPPermissionInterface {
30+
31+
var isAuthorized: Bool {
32+
return AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == AVAuthorizationStatus.authorized
33+
}
34+
35+
var isDenied: Bool {
36+
return AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == AVAuthorizationStatus.denied
37+
}
38+
39+
func request(withCompletionHandler сompletionHandler: @escaping ()->()?) {
40+
AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: {
41+
finished in
42+
DispatchQueue.main.async {
43+
сompletionHandler()
44+
}
45+
})
46+
}
47+
}
48+
}
49+
50+
#endif
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2017 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
#if SPPERMISSION_CONTACTS
23+
24+
import UIKit
25+
import Contacts
26+
import AddressBook
27+
28+
extension SPPermission {
29+
30+
struct SPContactsPermission: SPPermissionInterface {
31+
32+
var isAuthorized: Bool {
33+
if #available(iOS 9.0, *) {
34+
return CNContactStore.authorizationStatus(for: .contacts) == .authorized
35+
} else {
36+
return ABAddressBookGetAuthorizationStatus() == .authorized
37+
}
38+
}
39+
40+
var isDenied: Bool {
41+
if #available(iOS 9.0, *) {
42+
return CNContactStore.authorizationStatus(for: .contacts) == .denied
43+
} else {
44+
return ABAddressBookGetAuthorizationStatus() == .denied
45+
}
46+
}
47+
48+
func request(withCompletionHandler сompletionHandler: @escaping ()->()?) {
49+
if #available(iOS 9.0, *) {
50+
let store = CNContactStore()
51+
store.requestAccess(for: .contacts, completionHandler: { (granted, error) in
52+
DispatchQueue.main.async {
53+
сompletionHandler()
54+
}
55+
})
56+
} else {
57+
let addressBookRef: ABAddressBook = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
58+
ABAddressBookRequestAccessWithCompletion(addressBookRef) {
59+
(granted: Bool, error: CFError?) in
60+
DispatchQueue.main.async() {
61+
сompletionHandler()
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
#endif
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2017 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
#if SPPERMISSION_LOCATION
23+
24+
import UIKit
25+
import MapKit
26+
27+
extension SPPermission {
28+
29+
struct SPLocationPermission: SPPermissionInterface {
30+
31+
var type: SPLocationType
32+
33+
enum SPLocationType {
34+
case WhenInUse
35+
case AlwaysAndWhenInUse
36+
}
37+
38+
init(type: SPLocationType) {
39+
self.type = type
40+
}
41+
42+
var isAuthorized: Bool {
43+
44+
let status = CLLocationManager.authorizationStatus()
45+
46+
if status == .authorizedAlways {
47+
return true
48+
} else {
49+
if type == .WhenInUse {
50+
return status == .authorizedWhenInUse
51+
} else {
52+
return false
53+
}
54+
}
55+
}
56+
57+
var isDenied: Bool {
58+
return CLLocationManager.authorizationStatus() == .denied
59+
}
60+
61+
func request(withCompletionHandler сompletionHandler: @escaping ()->()?) {
62+
63+
switch self.type {
64+
case .AlwaysAndWhenInUse:
65+
if SPPermissionAlwaysAuthorizationLocationHandler.shared == nil {
66+
SPPermissionAlwaysAuthorizationLocationHandler.shared = SPPermissionAlwaysAuthorizationLocationHandler()
67+
}
68+
69+
SPPermissionAlwaysAuthorizationLocationHandler.shared!.requestPermission { (authorized) in
70+
DispatchQueue.main.async {
71+
сompletionHandler()
72+
SPPermissionAlwaysAuthorizationLocationHandler.shared = nil
73+
}
74+
}
75+
break
76+
case .WhenInUse:
77+
if SPPermissionWhenInUseAuthorizationLocationHandler.shared == nil {
78+
SPPermissionWhenInUseAuthorizationLocationHandler.shared = SPPermissionWhenInUseAuthorizationLocationHandler()
79+
}
80+
81+
SPPermissionWhenInUseAuthorizationLocationHandler.shared!.requestPermission { (authorized) in
82+
DispatchQueue.main.async {
83+
сompletionHandler()
84+
SPPermissionWhenInUseAuthorizationLocationHandler.shared = nil
85+
}
86+
}
87+
break
88+
}
89+
}
90+
}
91+
}
92+
93+
#endif

0 commit comments

Comments
 (0)