@capgo/capacitor-permissions 0.0.0-stage → 8.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,222 @@
1
+ import Foundation
2
+ import AVFoundation
3
+ import Photos
4
+ import Contacts
5
+ import EventKit
6
+ import CoreLocation
7
+ import CoreBluetooth
8
+ import CoreMotion
9
+ import UserNotifications
10
+ import Speech
11
+ import AppTrackingTransparency
12
+
13
+ @objc public class Permissions: NSObject, CLLocationManagerDelegate {
14
+ private let locationManager = CLLocationManager()
15
+ private var locationContinuation: ((PermissionStatusValue) -> Void)?
16
+ private var preciseContinuation: ((PermissionStatusValue) -> Void)?
17
+
18
+ override public init() {
19
+ super.init()
20
+ locationManager.delegate = self
21
+ }
22
+
23
+ @objc public func getPluginVersion() -> String {
24
+ return "native"
25
+ }
26
+
27
+ public func check(_ name: String, completion: @escaping (PermissionStatusValue) -> Void) {
28
+ switch name {
29
+ case "camera":
30
+ completion(PermissionStatusMapper.fromAV(AVCaptureDevice.authorizationStatus(for: .video)))
31
+ case "microphone":
32
+ completion(PermissionStatusMapper.fromAV(AVCaptureDevice.authorizationStatus(for: .audio)))
33
+ case "photoLibrary":
34
+ completion(PermissionStatusMapper.fromPhoto(PHPhotoLibrary.authorizationStatus(for: .readWrite)))
35
+ case "photoLibraryAddOnly":
36
+ completion(PermissionStatusMapper.fromPhoto(PHPhotoLibrary.authorizationStatus(for: .addOnly)))
37
+ case "contacts":
38
+ completion(PermissionStatusMapper.fromContacts(CNContactStore.authorizationStatus(for: .contacts)))
39
+ case "calendar":
40
+ completion(PermissionStatusMapper.fromEventKit(EKEventStore.authorizationStatus(for: .event)))
41
+ case "reminders":
42
+ completion(PermissionStatusMapper.fromEventKit(EKEventStore.authorizationStatus(for: .reminder)))
43
+ case "locationWhenInUse", "locationAlways":
44
+ completion(checkLocation(name))
45
+ case "bluetooth":
46
+ completion(PermissionStatusMapper.fromBluetooth(CBManager.authorization))
47
+ case "motion":
48
+ completion(checkMotion())
49
+ case "notifications":
50
+ UNUserNotificationCenter.current().getNotificationSettings { settings in
51
+ completion(PermissionStatusMapper.fromNotifications(settings))
52
+ }
53
+ case "speechRecognition":
54
+ completion(PermissionStatusMapper.fromSpeech(SFSpeechRecognizer.authorizationStatus()))
55
+ case "appTrackingTransparency":
56
+ completion(PermissionStatusMapper.fromTracking(ATTrackingManager.trackingAuthorizationStatus))
57
+ default:
58
+ completion(.unavailable)
59
+ }
60
+ }
61
+
62
+ public func request(_ name: String, completion: @escaping (PermissionStatusValue) -> Void) {
63
+ switch name {
64
+ case "camera":
65
+ AVCaptureDevice.requestAccess(for: .video) { _ in self.check(name, completion: completion) }
66
+ case "microphone":
67
+ AVCaptureDevice.requestAccess(for: .audio) { _ in self.check(name, completion: completion) }
68
+ case "photoLibrary":
69
+ PHPhotoLibrary.requestAuthorization(for: .readWrite) { _ in
70
+ self.check(name, completion: completion)
71
+ }
72
+ case "photoLibraryAddOnly":
73
+ PHPhotoLibrary.requestAuthorization(for: .addOnly) { _ in
74
+ self.check(name, completion: completion)
75
+ }
76
+ case "contacts":
77
+ CNContactStore().requestAccess(for: .contacts) { _, _ in
78
+ self.check(name, completion: completion)
79
+ }
80
+ case "calendar":
81
+ requestCalendar(completion)
82
+ case "reminders":
83
+ requestReminders(completion)
84
+ case "locationWhenInUse":
85
+ requestLocation(always: false, completion: completion)
86
+ case "locationAlways":
87
+ requestLocation(always: true, completion: completion)
88
+ case "bluetooth":
89
+ requestBluetooth(completion)
90
+ case "motion":
91
+ requestMotion(completion)
92
+ case "notifications":
93
+ UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { _, _ in
94
+ self.check(name, completion: completion)
95
+ }
96
+ case "speechRecognition":
97
+ SFSpeechRecognizer.requestAuthorization { _ in self.check(name, completion: completion) }
98
+ case "appTrackingTransparency":
99
+ ATTrackingManager.requestTrackingAuthorization { _ in
100
+ self.check(name, completion: completion)
101
+ }
102
+ default:
103
+ check(name, completion: completion)
104
+ }
105
+ }
106
+
107
+ public func requestPreciseLocation(completion: @escaping (PermissionStatusValue) -> Void) {
108
+ let status = locationManager.authorizationStatus
109
+ guard status == .authorizedWhenInUse || status == .authorizedAlways else {
110
+ completion(PermissionStatusMapper.fromLocation(
111
+ status,
112
+ accuracy: locationManager.accuracyAuthorization
113
+ ))
114
+ return
115
+ }
116
+ if locationManager.accuracyAuthorization == .fullAccuracy {
117
+ completion(.granted)
118
+ return
119
+ }
120
+ preciseContinuation = completion
121
+ locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: "PreciseLocation") { _ in
122
+ let cont = self.preciseContinuation
123
+ self.preciseContinuation = nil
124
+ cont?(PermissionStatusMapper.fromLocation(
125
+ self.locationManager.authorizationStatus,
126
+ accuracy: self.locationManager.accuracyAuthorization
127
+ ))
128
+ }
129
+ }
130
+
131
+ public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
132
+ if let cont = locationContinuation {
133
+ locationContinuation = nil
134
+ let name = manager.authorizationStatus == .authorizedAlways
135
+ ? "locationAlways"
136
+ : "locationWhenInUse"
137
+ check(name, completion: cont)
138
+ }
139
+ }
140
+
141
+ private func checkLocation(_ name: String) -> PermissionStatusValue {
142
+ let status = locationManager.authorizationStatus
143
+ let accuracy = locationManager.accuracyAuthorization
144
+ if name == "locationAlways" {
145
+ if status == .authorizedAlways {
146
+ return accuracy == .reducedAccuracy ? .limited : .granted
147
+ }
148
+ if status == .authorizedWhenInUse {
149
+ return .denied
150
+ }
151
+ }
152
+ return PermissionStatusMapper.fromLocation(status, accuracy: accuracy)
153
+ }
154
+
155
+ private func checkMotion() -> PermissionStatusValue {
156
+ if CMMotionActivityManager.isActivityAvailable() {
157
+ return PermissionStatusMapper.fromMotion(CMMotionActivityManager.authorizationStatus())
158
+ }
159
+ if CMPedometer.isPedometerEventTrackingAvailable() {
160
+ return PermissionStatusMapper.fromMotion(CMPedometer.authorizationStatus())
161
+ }
162
+ return .unavailable
163
+ }
164
+
165
+ private func requestCalendar(_ completion: @escaping (PermissionStatusValue) -> Void) {
166
+ let store = EKEventStore()
167
+ if #available(iOS 17.0, *) {
168
+ store.requestFullAccessToEvents { _, _ in
169
+ self.check("calendar", completion: completion)
170
+ }
171
+ } else {
172
+ store.requestAccess(to: .event) { _, _ in
173
+ self.check("calendar", completion: completion)
174
+ }
175
+ }
176
+ }
177
+
178
+ private func requestReminders(_ completion: @escaping (PermissionStatusValue) -> Void) {
179
+ let store = EKEventStore()
180
+ if #available(iOS 17.0, *) {
181
+ store.requestFullAccessToReminders { _, _ in
182
+ self.check("reminders", completion: completion)
183
+ }
184
+ } else {
185
+ store.requestAccess(to: .reminder) { _, _ in
186
+ self.check("reminders", completion: completion)
187
+ }
188
+ }
189
+ }
190
+
191
+ private func requestLocation(always: Bool, completion: @escaping (PermissionStatusValue) -> Void) {
192
+ locationContinuation = completion
193
+ if always {
194
+ locationManager.requestAlwaysAuthorization()
195
+ } else {
196
+ locationManager.requestWhenInUseAuthorization()
197
+ }
198
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
199
+ if let cont = self.locationContinuation {
200
+ self.locationContinuation = nil
201
+ self.check(always ? "locationAlways" : "locationWhenInUse", completion: cont)
202
+ }
203
+ }
204
+ }
205
+
206
+ private func requestBluetooth(_ completion: @escaping (PermissionStatusValue) -> Void) {
207
+ _ = CBCentralManager(delegate: nil, queue: nil, options: [
208
+ CBCentralManagerOptionShowPowerAlertKey: false
209
+ ])
210
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
211
+ self.check("bluetooth", completion: completion)
212
+ }
213
+ }
214
+
215
+ private func requestMotion(_ completion: @escaping (PermissionStatusValue) -> Void) {
216
+ let manager = CMMotionActivityManager()
217
+ let now = Date()
218
+ manager.queryActivityStarting(from: now, to: now, to: .main) { _, _ in
219
+ self.check("motion", completion: completion)
220
+ }
221
+ }
222
+ }
@@ -0,0 +1,123 @@
1
+ import Foundation
2
+ import Capacitor
3
+ import UIKit
4
+
5
+ @objc(PermissionsPlugin)
6
+ public class PermissionsPlugin: CAPPlugin, CAPBridgedPlugin {
7
+ public let identifier = "PermissionsPlugin"
8
+ public let jsName = "Permissions"
9
+ public let pluginMethods: [CAPPluginMethod] = [
10
+ CAPPluginMethod(name: "check", returnType: CAPPluginReturnPromise),
11
+ CAPPluginMethod(name: "request", returnType: CAPPluginReturnPromise),
12
+ CAPPluginMethod(name: "checkMultiple", returnType: CAPPluginReturnPromise),
13
+ CAPPluginMethod(name: "requestMultiple", returnType: CAPPluginReturnPromise),
14
+ CAPPluginMethod(name: "shouldShowRationale", returnType: CAPPluginReturnPromise),
15
+ CAPPluginMethod(name: "openSettings", returnType: CAPPluginReturnPromise),
16
+ CAPPluginMethod(name: "requestPreciseLocation", returnType: CAPPluginReturnPromise),
17
+ CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise)
18
+ ]
19
+
20
+ private let implementation = Permissions()
21
+
22
+ @objc func check(_ call: CAPPluginCall) {
23
+ guard let permission = call.getString("permission") else {
24
+ call.reject("permission is required")
25
+ return
26
+ }
27
+ implementation.check(permission) { status in
28
+ call.resolve(["status": status.rawValue])
29
+ }
30
+ }
31
+
32
+ @objc func request(_ call: CAPPluginCall) {
33
+ guard let permission = call.getString("permission") else {
34
+ call.reject("permission is required")
35
+ return
36
+ }
37
+ implementation.request(permission) { status in
38
+ call.resolve(["status": status.rawValue])
39
+ }
40
+ }
41
+
42
+ @objc func checkMultiple(_ call: CAPPluginCall) {
43
+ guard let permissions = call.getArray("permissions", String.self), !permissions.isEmpty else {
44
+ call.reject("permissions is required")
45
+ return
46
+ }
47
+ var statuses: [String: String] = [:]
48
+ let group = DispatchGroup()
49
+ for permission in permissions {
50
+ group.enter()
51
+ implementation.check(permission) { status in
52
+ statuses[permission] = status.rawValue
53
+ group.leave()
54
+ }
55
+ }
56
+ group.notify(queue: .main) {
57
+ call.resolve(["statuses": statuses])
58
+ }
59
+ }
60
+
61
+ @objc func requestMultiple(_ call: CAPPluginCall) {
62
+ guard let permissions = call.getArray("permissions", String.self), !permissions.isEmpty else {
63
+ call.reject("permissions is required")
64
+ return
65
+ }
66
+ var statuses: [String: String] = [:]
67
+ func next(_ index: Int) {
68
+ if index >= permissions.count {
69
+ call.resolve(["statuses": statuses])
70
+ return
71
+ }
72
+ let permission = permissions[index]
73
+ implementation.request(permission) { status in
74
+ statuses[permission] = status.rawValue
75
+ next(index + 1)
76
+ }
77
+ }
78
+ next(0)
79
+ }
80
+
81
+ @objc func shouldShowRationale(_ call: CAPPluginCall) {
82
+ call.resolve(["shouldShow": false])
83
+ }
84
+
85
+ @objc func openSettings(_ call: CAPPluginCall) {
86
+ let type = call.getString("type") ?? "application"
87
+ DispatchQueue.main.async {
88
+ let url: URL?
89
+ if type == "notifications" {
90
+ if #available(iOS 16.0, *) {
91
+ url = URL(string: UIApplication.openNotificationSettingsURLString)
92
+ } else {
93
+ url = URL(string: UIApplication.openSettingsURLString)
94
+ }
95
+ } else {
96
+ url = URL(string: UIApplication.openSettingsURLString)
97
+ }
98
+ guard let url else {
99
+ call.reject("Unable to open settings")
100
+ return
101
+ }
102
+ UIApplication.shared.open(url, options: [:]) { success in
103
+ if success {
104
+ call.resolve()
105
+ } else {
106
+ call.reject("Unable to open settings")
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ @objc func requestPreciseLocation(_ call: CAPPluginCall) {
113
+ implementation.requestPreciseLocation { status in
114
+ call.resolve(["status": status.rawValue])
115
+ }
116
+ }
117
+
118
+ @objc func getPluginVersion(_ call: CAPPluginCall) {
119
+ call.resolve([
120
+ "version": implementation.getPluginVersion()
121
+ ])
122
+ }
123
+ }
@@ -0,0 +1,73 @@
1
+ import XCTest
2
+ @testable import PermissionsPlugin
3
+ import Photos
4
+ import AVFoundation
5
+ import Contacts
6
+ import Speech
7
+ import AppTrackingTransparency
8
+ import CoreBluetooth
9
+ import CoreLocation
10
+
11
+ class PermissionsTests: XCTestCase {
12
+ func testPhotoStatusMapping() {
13
+ XCTAssertEqual(PermissionStatusMapper.fromPhoto(.authorized), .granted)
14
+ XCTAssertEqual(PermissionStatusMapper.fromPhoto(.limited), .limited)
15
+ XCTAssertEqual(PermissionStatusMapper.fromPhoto(.denied), .denied)
16
+ XCTAssertEqual(PermissionStatusMapper.fromPhoto(.restricted), .blocked)
17
+ XCTAssertEqual(PermissionStatusMapper.fromPhoto(.notDetermined), .denied)
18
+ }
19
+
20
+ func testAVStatusMapping() {
21
+ XCTAssertEqual(PermissionStatusMapper.fromAV(.authorized), .granted)
22
+ XCTAssertEqual(PermissionStatusMapper.fromAV(.denied), .denied)
23
+ XCTAssertEqual(PermissionStatusMapper.fromAV(.restricted), .blocked)
24
+ XCTAssertEqual(PermissionStatusMapper.fromAV(.notDetermined), .denied)
25
+ }
26
+
27
+ func testContactsStatusMapping() {
28
+ XCTAssertEqual(PermissionStatusMapper.fromContacts(.authorized), .granted)
29
+ XCTAssertEqual(PermissionStatusMapper.fromContacts(.denied), .denied)
30
+ XCTAssertEqual(PermissionStatusMapper.fromContacts(.restricted), .blocked)
31
+ XCTAssertEqual(PermissionStatusMapper.fromContacts(.notDetermined), .denied)
32
+ }
33
+
34
+ func testSpeechStatusMapping() {
35
+ XCTAssertEqual(PermissionStatusMapper.fromSpeech(.authorized), .granted)
36
+ XCTAssertEqual(PermissionStatusMapper.fromSpeech(.denied), .denied)
37
+ XCTAssertEqual(PermissionStatusMapper.fromSpeech(.restricted), .blocked)
38
+ XCTAssertEqual(PermissionStatusMapper.fromSpeech(.notDetermined), .denied)
39
+ }
40
+
41
+ func testTrackingStatusMapping() {
42
+ XCTAssertEqual(PermissionStatusMapper.fromTracking(.authorized), .granted)
43
+ XCTAssertEqual(PermissionStatusMapper.fromTracking(.denied), .denied)
44
+ XCTAssertEqual(PermissionStatusMapper.fromTracking(.restricted), .blocked)
45
+ XCTAssertEqual(PermissionStatusMapper.fromTracking(.notDetermined), .denied)
46
+ }
47
+
48
+ func testBluetoothStatusMapping() {
49
+ XCTAssertEqual(PermissionStatusMapper.fromBluetooth(.allowedAlways), .granted)
50
+ XCTAssertEqual(PermissionStatusMapper.fromBluetooth(.denied), .denied)
51
+ XCTAssertEqual(PermissionStatusMapper.fromBluetooth(.restricted), .blocked)
52
+ XCTAssertEqual(PermissionStatusMapper.fromBluetooth(.notDetermined), .denied)
53
+ }
54
+
55
+ func testLocationStatusMapping() {
56
+ XCTAssertEqual(
57
+ PermissionStatusMapper.fromLocation(.authorizedWhenInUse, accuracy: .fullAccuracy),
58
+ .granted
59
+ )
60
+ XCTAssertEqual(
61
+ PermissionStatusMapper.fromLocation(.authorizedWhenInUse, accuracy: .reducedAccuracy),
62
+ .limited
63
+ )
64
+ XCTAssertEqual(PermissionStatusMapper.fromLocation(.denied, accuracy: nil), .denied)
65
+ XCTAssertEqual(PermissionStatusMapper.fromLocation(.restricted, accuracy: nil), .blocked)
66
+ XCTAssertEqual(PermissionStatusMapper.fromLocation(.notDetermined, accuracy: nil), .denied)
67
+ }
68
+
69
+ func testGetPluginVersion() {
70
+ let implementation = Permissions()
71
+ XCTAssertEqual("native", implementation.getPluginVersion())
72
+ }
73
+ }
package/package.json CHANGED
@@ -1,6 +1,95 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-permissions",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "8.2.1",
4
+ "description": "Capacitor plugin for checking and requesting app permissions.",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Sources",
14
+ "ios/Tests",
15
+ "Package.swift",
16
+ "CapgoCapacitorPermissions.podspec"
17
+ ],
18
+ "author": "Martin Donadieu <martin@capgo.app>",
19
+ "license": "MPL-2.0",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Cap-go/capacitor-permissions.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/Cap-go/capacitor-permissions/issues"
26
+ },
27
+ "homepage": "https://capgo.app/docs/plugins/permissions/",
28
+ "keywords": [
29
+ "capacitor",
30
+ "permissions",
31
+ "privacy",
32
+ "runtime-permissions",
33
+ "settings",
34
+ "plugin",
35
+ "native"
36
+ ],
37
+ "scripts": {
38
+ "verify": "bun run verify:ios && bun run verify:android && bun run verify:web",
39
+ "verify:ios": "xcodebuild -scheme CapgoCapacitorPermissions -destination generic/platform=iOS",
40
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
41
+ "verify:web": "bun run build && bun run test:web",
42
+ "test:web": "bun test",
43
+ "lint": "bun run eslint && bun run prettier -- --check && bun run swiftlint -- lint",
44
+ "fmt": "bun run eslint -- --fix && bun run prettier -- --write && bun run swiftlint -- --fix --format",
45
+ "eslint": "eslint \"**/*.ts\"",
46
+ "prettier": "prettier-pretty-check \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
47
+ "swiftlint": "node-swiftlint",
48
+ "docgen": "docgen --api PermissionsPlugin --output-readme README.md --output-json dist/docs.json",
49
+ "build": "bun run clean && bun run docgen && tsc && rollup -c rollup.config.mjs",
50
+ "clean": "rimraf ./dist",
51
+ "watch": "tsc --watch",
52
+ "prepublishOnly": "bun run build",
53
+ "check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs",
54
+ "check:cap9-deprecated": "node scripts/check-cap9-deprecated.mjs",
55
+ "example:install": "cd example-app && bun install --frozen-lockfile",
56
+ "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build",
57
+ "changelog:ai": "node scripts/generate-ai-changelog.mjs"
58
+ },
59
+ "devDependencies": {
60
+ "@capacitor/android": "^8.3.4",
61
+ "@capacitor/cli": "^8.3.4",
62
+ "@capacitor/core": "^8.3.4",
63
+ "@capacitor/docgen": "^0.3.1",
64
+ "@capacitor/ios": "^8.3.4",
65
+ "@eslint/js": "^10.0.1",
66
+ "@ionic/prettier-config": "^4.0.0",
67
+ "@ionic/swiftlint-config": "^2.0.0",
68
+ "@types/node": "^25.9.1",
69
+ "eslint": "^10.4.0",
70
+ "eslint-config-prettier": "^10.1.8",
71
+ "eslint-plugin-import": "^2.32.0",
72
+ "husky": "^9.1.7",
73
+ "prettier": "^3.8.3",
74
+ "prettier-plugin-java": "^2.9.2",
75
+ "prettier-pretty-check": "^0.2.0",
76
+ "rimraf": "^6.1.3",
77
+ "rollup": "^4.60.4",
78
+ "swiftlint": "^2.0.0",
79
+ "typescript": "^6.0.3",
80
+ "typescript-eslint": "^8.59.4"
81
+ },
82
+ "peerDependencies": {
83
+ "@capacitor/core": ">=8.0.0"
84
+ },
85
+ "prettier": "@ionic/prettier-config",
86
+ "swiftlint": "@ionic/swiftlint-config",
87
+ "capacitor": {
88
+ "ios": {
89
+ "src": "ios"
90
+ },
91
+ "android": {
92
+ "src": "android"
93
+ }
94
+ }
95
+ }