Skip to content
This repository has been archived by the owner on Feb 27, 2019. It is now read-only.

Factored permissions out into subspecs #216

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions PermissionScope-example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ViewController: UIViewController {

// an example of how to use the unified permissions API
func checkContacts() {
switch PermissionScope().statusContacts() {
/* switch PermissionScope().statusContacts() {
case .unknown:
// ask
PermissionScope().requestContacts()
Expand All @@ -52,7 +52,7 @@ class ViewController: UIViewController {
case .authorized:
// thanks!
return
}
}*/
}

@IBAction func singlePerm() {
Expand All @@ -78,7 +78,7 @@ class ViewController: UIViewController {
}

@IBAction func noUIPerm() {
noUIPscope.requestNotifications()
//noUIPscope.requestNotifications()
}
}

51 changes: 49 additions & 2 deletions PermissionScope.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,54 @@ Pod::Spec.new do |s|

s.ios.deployment_target = '8.0'

s.source_files = 'PermissionScope/*.swift'

s.requires_arc = true

s.default_subspec = 'Core'

s.subspec 'Core' do |sub|
sub.source_files = 'PermissionScope/*.swift'
end

s.subspec 'Bluetooth' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Bluetooth.swift'
end

s.subspec 'Camera' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Camera.swift'
end

s.subspec 'Contacts' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Contacts.swift'
end

s.subspec 'Events' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Events.swift'
end

s.subspec 'Location' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Location.swift'
end

s.subspec 'Microphone' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Microphone.swift'
end

s.subspec 'Motion' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Motion.swift'
end

s.subspec 'Notifications' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Notifications.swift'
end

s.subspec 'Photos' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Photos.swift'
end

s.subspec 'Reminders' do |sub|
sub.source_files = 'PermissionScope/Extensions/PermissionScope+Reminders.swift'
end

end


52 changes: 52 additions & 0 deletions PermissionScope.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
objects = {

/* Begin PBXBuildFile section */
0CB286531E26037B00116E09 /* PermissionScope+Bluetooth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB286481E26037B00116E09 /* PermissionScope+Bluetooth.swift */; };
0CB286541E26037B00116E09 /* PermissionScope+Camera.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB286491E26037B00116E09 /* PermissionScope+Camera.swift */; };
0CB286551E26037B00116E09 /* PermissionScope+Contacts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB2864A1E26037B00116E09 /* PermissionScope+Contacts.swift */; };
0CB286561E26037B00116E09 /* PermissionScope+Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB2864B1E26037B00116E09 /* PermissionScope+Events.swift */; };
0CB286591E26037B00116E09 /* PermissionScope+Microphone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB2864E1E26037B00116E09 /* PermissionScope+Microphone.swift */; };
0CB2865A1E26037B00116E09 /* PermissionScope+Motion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB2864F1E26037B00116E09 /* PermissionScope+Motion.swift */; };
0CB2865B1E26037B00116E09 /* PermissionScope+Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB286501E26037B00116E09 /* PermissionScope+Notifications.swift */; };
0CB2865C1E26037B00116E09 /* PermissionScope+Photos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB286511E26037B00116E09 /* PermissionScope+Photos.swift */; };
0CB2865D1E26037B00116E09 /* PermissionScope+Reminders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB286521E26037B00116E09 /* PermissionScope+Reminders.swift */; };
0CB2865F1E260F9F00116E09 /* PermissionScope+Location.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB2865E1E260F9F00116E09 /* PermissionScope+Location.swift */; };
244B5BA81B8D5E7900FFA019 /* Permissions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 244B5BA71B8D5E7900FFA019 /* Permissions.swift */; };
2487CEE11B87B01E00766A1A /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2487CEE01B87B01E00766A1A /* Extensions.swift */; };
2487CEE31B87B05200766A1A /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2487CEE21B87B05200766A1A /* Constants.swift */; };
Expand Down Expand Up @@ -74,6 +84,16 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0CB286481E26037B00116E09 /* PermissionScope+Bluetooth.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Bluetooth.swift"; path = "Extensions/PermissionScope+Bluetooth.swift"; sourceTree = "<group>"; };
0CB286491E26037B00116E09 /* PermissionScope+Camera.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Camera.swift"; path = "Extensions/PermissionScope+Camera.swift"; sourceTree = "<group>"; };
0CB2864A1E26037B00116E09 /* PermissionScope+Contacts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Contacts.swift"; path = "Extensions/PermissionScope+Contacts.swift"; sourceTree = "<group>"; };
0CB2864B1E26037B00116E09 /* PermissionScope+Events.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Events.swift"; path = "Extensions/PermissionScope+Events.swift"; sourceTree = "<group>"; };
0CB2864E1E26037B00116E09 /* PermissionScope+Microphone.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Microphone.swift"; path = "Extensions/PermissionScope+Microphone.swift"; sourceTree = "<group>"; };
0CB2864F1E26037B00116E09 /* PermissionScope+Motion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Motion.swift"; path = "Extensions/PermissionScope+Motion.swift"; sourceTree = "<group>"; };
0CB286501E26037B00116E09 /* PermissionScope+Notifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Notifications.swift"; path = "Extensions/PermissionScope+Notifications.swift"; sourceTree = "<group>"; };
0CB286511E26037B00116E09 /* PermissionScope+Photos.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Photos.swift"; path = "Extensions/PermissionScope+Photos.swift"; sourceTree = "<group>"; };
0CB286521E26037B00116E09 /* PermissionScope+Reminders.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Reminders.swift"; path = "Extensions/PermissionScope+Reminders.swift"; sourceTree = "<group>"; };
0CB2865E1E260F9F00116E09 /* PermissionScope+Location.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PermissionScope+Location.swift"; path = "Extensions/PermissionScope+Location.swift"; sourceTree = "<group>"; };
244B5BA71B8D5E7900FFA019 /* Permissions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Permissions.swift; sourceTree = "<group>"; };
2487CEE01B87B01E00766A1A /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
2487CEE21B87B05200766A1A /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -130,6 +150,23 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0CB286441E26033D00116E09 /* Extensions */ = {
isa = PBXGroup;
children = (
0CB286481E26037B00116E09 /* PermissionScope+Bluetooth.swift */,
0CB286491E26037B00116E09 /* PermissionScope+Camera.swift */,
0CB2864A1E26037B00116E09 /* PermissionScope+Contacts.swift */,
0CB2864B1E26037B00116E09 /* PermissionScope+Events.swift */,
0CB2864E1E26037B00116E09 /* PermissionScope+Microphone.swift */,
0CB2864F1E26037B00116E09 /* PermissionScope+Motion.swift */,
0CB286501E26037B00116E09 /* PermissionScope+Notifications.swift */,
0CB286511E26037B00116E09 /* PermissionScope+Photos.swift */,
0CB286521E26037B00116E09 /* PermissionScope+Reminders.swift */,
0CB2865E1E260F9F00116E09 /* PermissionScope+Location.swift */,
);
name = Extensions;
sourceTree = "<group>";
};
D05F103F1AD1EBF6000F8267 /* PermissionScope-example */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -176,6 +213,7 @@
D092BA351AD1E5A300BFC163 /* PermissionScope */ = {
isa = PBXGroup;
children = (
0CB286441E26033D00116E09 /* Extensions */,
D092BA381AD1E5A300BFC163 /* PermissionScope.h */,
D092BA4F1AD1E67300BFC163 /* PermissionScope.swift */,
2487CEE41B87B61E00766A1A /* Structs.swift */,
Expand Down Expand Up @@ -384,11 +422,21 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0CB286541E26037B00116E09 /* PermissionScope+Camera.swift in Sources */,
0CB2865D1E26037B00116E09 /* PermissionScope+Reminders.swift in Sources */,
0CB2865A1E26037B00116E09 /* PermissionScope+Motion.swift in Sources */,
2487CEE51B87B61E00766A1A /* Structs.swift in Sources */,
D092BA501AD1E67300BFC163 /* PermissionScope.swift in Sources */,
0CB2865B1E26037B00116E09 /* PermissionScope+Notifications.swift in Sources */,
0CB2865F1E260F9F00116E09 /* PermissionScope+Location.swift in Sources */,
2487CEE11B87B01E00766A1A /* Extensions.swift in Sources */,
244B5BA81B8D5E7900FFA019 /* Permissions.swift in Sources */,
0CB286561E26037B00116E09 /* PermissionScope+Events.swift in Sources */,
0CB286531E26037B00116E09 /* PermissionScope+Bluetooth.swift in Sources */,
0CB286551E26037B00116E09 /* PermissionScope+Contacts.swift in Sources */,
0CB286591E26037B00116E09 /* PermissionScope+Microphone.swift in Sources */,
2487CEE31B87B05200766A1A /* Constants.swift in Sources */,
0CB2865C1E26037B00116E09 /* PermissionScope+Photos.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -464,6 +512,7 @@
INFOPLIST_FILE = "PermissionScope-example/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "com.thatthinginswift.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "PermissionScope-example";
PROVISIONING_PROFILE = "";
Expand All @@ -478,6 +527,7 @@
INFOPLIST_FILE = "PermissionScope-example/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "com.thatthinginswift.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "PermissionScope-example";
PROVISIONING_PROFILE = "";
Expand Down Expand Up @@ -592,6 +642,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "com.thatthinginswift.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = PermissionScope;
SKIP_INSTALL = YES;
Expand All @@ -613,6 +664,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "com.thatthinginswift.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = PermissionScope;
SKIP_INSTALL = YES;
Expand Down
89 changes: 89 additions & 0 deletions PermissionScope/Extensions/PermissionScope+Bluetooth.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// PermissionScope+Bluetooth.swift
// PermissionScope
//
// Created by Timothy Costa on 1/11/17.
// Copyright © 2017 That Thing in Swift. All rights reserved.
//

import Foundation
import CoreBluetooth

private var bluetoothManagerKey: UInt8 = 0

@objc public class BluetoothPermission: NSObject, Permission {
public let type: PermissionType = .bluetooth
}

extension PermissionScope: CBPeripheralManagerDelegate {
var bluetoothManager:CBPeripheralManager {
get {
if let man = objc_getAssociatedObject(self, &bluetoothManagerKey) as? CBPeripheralManager {
return man
}
let man = CBPeripheralManager(delegate: self, queue: nil, options:[CBPeripheralManagerOptionShowPowerAlertKey: false])
man.delegate = self
objc_setAssociatedObject(self, &bluetoothManagerKey, man, .OBJC_ASSOCIATION_RETAIN)
return man
}
}

/**
Requests access to Bluetooth, if necessary.
*/
public func requestBluetooth() {
let status = statusBluetooth()
switch status {
case .disabled:
showDisabledAlert(.bluetooth)
case .unauthorized:
showDeniedAlert(.bluetooth)
case .unknown:
triggerBluetoothStatusUpdate()
default:
break
}

}

/**
Returns the current permission status for accessing Bluetooth.

- returns: Permission status for the requested type.
*/
public func statusBluetooth() -> PermissionStatus {
// if already asked for bluetooth before, do a request to get status, else wait for user to request
if askedBluetooth{
triggerBluetoothStatusUpdate()
} else {
return .unknown
}

let state = (bluetoothManager.state, CBPeripheralManager.authorizationStatus())
switch state {
case (.unsupported, _), (.poweredOff, _), (_, .restricted):
return .disabled
case (.unauthorized, _), (_, .denied):
return .unauthorized
case (.poweredOn, .authorized):
return .authorized
default:
return .unknown
}
}

func triggerBluetoothStatusUpdate() {
if !waitingForBluetooth && bluetoothManager.state == .unknown {
bluetoothManager.startAdvertising(nil)
bluetoothManager.stopAdvertising()
askedBluetooth = true
waitingForBluetooth = true
}
}

public func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
waitingForBluetooth = false
detectAndCallback()
}

}
56 changes: 56 additions & 0 deletions PermissionScope/Extensions/PermissionScope+Camera.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// PermissionScope+Camera.swift
// PermissionScope
//
// Created by Timothy Costa on 1/11/17.
// Copyright © 2017 That Thing in Swift. All rights reserved.
//

import Foundation
import AVFoundation

@objc public class CameraPermission: NSObject, Permission {
public let type: PermissionType = .camera
}


extension PermissionScope {
/**
Requests access to the Camera, if necessary.
*/
public func requestCamera() {
let status = statusCamera()
switch status {
case .unknown:
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo,
completionHandler: { granted in
self.detectAndCallback()
})
case .unauthorized:
showDeniedAlert(.camera)
case .disabled:
showDisabledAlert(.camera)
case .authorized:
break
}
}

/**
Returns the current permission status for accessing the Camera.

- returns: Permission status for the requested type.
*/
public func statusCamera() -> PermissionStatus {
let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch status {
case .authorized:
return .authorized
case .restricted, .denied:
return .unauthorized
case .notDetermined:
return .unknown
}
}


}
Loading