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

added support for MediaLibrary. #214

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions PermissionScope-example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppleMusicUsageDescription</key>
<string>Explanation why you need the media library</string>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
1 change: 1 addition & 0 deletions PermissionScope/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ enum Constants {
struct InfoPlistKeys {
static let locationWhenInUse = "NSLocationWhenInUseUsageDescription"
static let locationAlways = "NSLocationAlwaysUsageDescription"
static let mediaLibrary = "NSAppleMusicUsageDescription"
}
}
44 changes: 44 additions & 0 deletions PermissionScope/PermissionScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import EventKit
import CoreBluetooth
import CoreMotion
import Contacts
import MediaPlayer

public typealias statusRequestClosure = (_ status: PermissionStatus) -> Void
public typealias authClosureType = (_ finished: Bool, _ results: [PermissionResult]) -> Void
Expand Down Expand Up @@ -987,6 +988,47 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void

/// Returns whether PermissionScope is waiting for the user to enable/disable motion access or not.
fileprivate var waitingForMotion = false

// MARK: MediaLibrary

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

- returns: Permission status for the requested type.
*/
public func statusMediaLibrary() -> PermissionStatus {
guard #available(iOS 9.3, *) else { fatalError() }
let status = MPMediaLibrary.authorizationStatus()
switch status {
case .authorized:
return .authorized
case .restricted, .denied:
return .unauthorized
case .notDetermined:
return .unknown
}
}

/**
Requests access to the MediaLibrary, if necessary.
*/
public func requestMediaLibrary() {
guard #available(iOS 9.3, *) else { fatalError() }
let status = statusMediaLibrary()
switch status {
case .unknown:
let hasWhenInUseKey :Bool = !Bundle.main
.object(forInfoDictionaryKey: Constants.InfoPlistKeys.mediaLibrary).isNil
assert(hasWhenInUseKey, Constants.InfoPlistKeys.mediaLibrary + " not found in Info.plist.")
MPMediaLibrary.requestAuthorization { _ in
self.detectAndCallback()
}
case .unauthorized:
self.showDeniedAlert(.mediaLibrary)
default:
break
}
}

// MARK: - UI

Expand Down Expand Up @@ -1243,6 +1285,8 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
permissionStatus = statusBluetooth()
case .motion:
permissionStatus = statusMotion()
case .mediaLibrary:
permissionStatus = statusMediaLibrary()
}

// Perform completion
Expand Down
5 changes: 5 additions & 0 deletions PermissionScope/Permissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import CoreBluetooth
import CoreMotion
import CloudKit
import Accounts
import MediaPlayer

/**
* Protocol for permission configurations.
Expand Down Expand Up @@ -76,3 +77,7 @@ public typealias requestPermissionShowAlert = (PermissionType) -> Void
@objc public class MotionPermission: NSObject, Permission {
public let type: PermissionType = .motion
}

@objc public class MediaLibraryPermission: NSObject, Permission {
public let type: PermissionType = .mediaLibrary
}
5 changes: 3 additions & 2 deletions PermissionScope/Structs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

/// Permissions currently supportes by PermissionScope
@objc public enum PermissionType: Int, CustomStringConvertible {
case contacts, locationAlways, locationInUse, notifications, microphone, camera, photos, reminders, events, bluetooth, motion
case contacts, locationAlways, locationInUse, notifications, microphone, camera, photos, reminders, events, bluetooth, motion, mediaLibrary

public var prettyDescription: String {
switch self {
Expand All @@ -34,10 +34,11 @@ import Foundation
case .reminders: return "Reminders"
case .bluetooth: return "Bluetooth"
case .motion: return "Motion"
case .mediaLibrary: return "MediaLibrary"
}
}

static let allValues = [contacts, locationAlways, locationInUse, notifications, microphone, camera, photos, reminders, events, bluetooth, motion]
static let allValues = [contacts, locationAlways, locationInUse, notifications, microphone, camera, photos, reminders, events, bluetooth, motion, mediaLibrary]
}

/// Possible statuses for a permission.
Expand Down