Skip to content

Commit

Permalink
Merge pull request #285 from tilltue/ios14
Browse files Browse the repository at this point in the history
IOS 14 Handle the Limited Photos Library
  • Loading branch information
tilltue authored Sep 20, 2020
2 parents 4f082d4 + 7dfcbaa commit dfba254
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 38 deletions.
2 changes: 2 additions & 0 deletions Example/TLPhotoPicker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<string>You need Usage Microphone Description</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>You need Usage Photo Description</string>
<key>PHPhotoLibraryPreventAutomaticLimitedAccessAlert</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ github "tilltue/TLPhotoPicker"
> Don't forget the Privacy Description in `info.plist`.
<img src="./Images/Privacy.png">
> iOS 14
> You can suppress the automatic prompting from the system by setting this key to yes in your apps info plist.
> PHPhotoLibraryPreventAutomaticLimitedAccessAlert = YES
https://developer.apple.com/videos/play/wwdc2020/10641/

## Usage

**use delegate**
Expand Down Expand Up @@ -293,6 +298,7 @@ public struct TLPhotosPickerConfigure {
public var maxVideoDuration:TimeInterval? = nil //for camera : max video recording duration
public var autoPlay = true
public var muteAudio = true
public var preventAutomaticLimitedAccessAlert = true // newest iOS 14
public var mediaType: PHAssetMediaType? = nil
public var numberOfColumn = 3
public var singleSelectedMode = false
Expand Down
2 changes: 1 addition & 1 deletion TLPhotoPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'TLPhotoPicker'
s.version = '2.0.11'
s.version = '2.0.12'
s.summary = 'multiple phassets picker for iOS lib. like facebook'

# This description is used to generate tags and improve search results.
Expand Down
11 changes: 6 additions & 5 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TLPhotoLibrary {
lazy var imageManager: PHCachingImageManager = {
return PHCachingImageManager()
}()
internal var limitMode: Bool = false
internal var assetCollections: [PHFetchResult<PHAssetCollection>] = []
internal var albums: PHFetchResult<PHCollection>? = nil

Expand Down Expand Up @@ -240,11 +241,11 @@ extension TLPhotoLibrary {
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
var assetCollections = [TLAssetsCollection]()

//Camera Roll
let camerarollCollection = getSmartAlbum(subType: .smartAlbumUserLibrary,
useCameraButton: useCameraButton,
result: &assetCollections)
if var cameraRoll = camerarollCollection {
//Recents
let recentsCollection = getSmartAlbum(subType: .smartAlbumUserLibrary,
useCameraButton: useCameraButton,
result: &assetCollections)
if var cameraRoll = recentsCollection {
cameraRoll.title = configure.customLocalizedTitle[cameraRoll.title] ?? cameraRoll.title
cameraRoll.useCameraButton = useCameraButton
assetCollections[0] = cameraRoll
Expand Down
88 changes: 61 additions & 27 deletions TLPhotoPicker/Classes/TLPhotosPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public struct TLPhotosPickerConfigure {
public var maxVideoDuration:TimeInterval? = nil
public var autoPlay = true
public var muteAudio = true
public var preventAutomaticLimitedAccessAlert = true
public var mediaType: PHAssetMediaType? = nil
public var numberOfColumn = 3
public var singleSelectedMode = false
Expand Down Expand Up @@ -144,6 +145,7 @@ open class TLPhotosPickerViewController: UIViewController {
@IBOutlet open var emptyView: UIView!
@IBOutlet open var emptyImageView: UIImageView!
@IBOutlet open var emptyMessageLabel: UILabel!
@IBOutlet open var photosButton: UIBarButtonItem!

public weak var delegate: TLPhotosPickerViewControllerDelegate? = nil
public weak var logDelegate: TLPhotosPickerLogDelegate? = nil
Expand Down Expand Up @@ -253,27 +255,49 @@ open class TLPhotosPickerViewController: UIViewController {
self.stopPlay()
}

func checkAuthorization() {
switch PHPhotoLibrary.authorizationStatus() {
private func loadPhotos(limitMode: Bool) {
self.photoLibrary.limitMode = limitMode
self.photoLibrary.delegate = self
self.photoLibrary.fetchCollection(configure: self.configure)
}

private func processAuthorization(status: PHAuthorizationStatus) {
switch status {
case .notDetermined:
PHPhotoLibrary.requestAuthorization { [weak self] status in
switch status {
case .authorized:
self?.initPhotoLibrary()
default:
self?.handleDeniedAlbumsAuthorization()
}
}
requestAuthorization()
case .limited:
loadPhotos(limitMode: true)
case .authorized:
self.initPhotoLibrary()
case .restricted: fallthrough
case .denied:
loadPhotos(limitMode: false)
case .restricted, .denied:
handleDeniedAlbumsAuthorization()
@unknown default:
break
}
}

private func requestAuthorization() {
if #available(iOS 14.0, *) {
PHPhotoLibrary.requestAuthorization(for: .readWrite) { [weak self] status in
self?.processAuthorization(status: status)
}
} else {
PHPhotoLibrary.requestAuthorization { [weak self] status in
self?.processAuthorization(status: status)
}
}
}

private func checkAuthorization() {
if #available(iOS 14.0, *) {
let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
processAuthorization(status: status)
} else {
let status = PHPhotoLibrary.authorizationStatus()
processAuthorization(status: status)
}
}

override open func viewDidLoad() {
super.viewDidLoad()
makeUI()
Expand All @@ -294,7 +318,7 @@ open class TLPhotosPickerViewController: UIViewController {
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.photoLibrary.delegate == nil {
initPhotoLibrary()
checkAuthorization()
}
}

Expand Down Expand Up @@ -404,9 +428,18 @@ extension TLPhotosPickerViewController {
self.customDataSouces?.registerSupplementView(collectionView: self.collectionView)
}

private func updatePresentLimitedLibraryButton() {
if #available(iOS 14.0, *), self.photoLibrary.limitMode && self.configure.preventAutomaticLimitedAccessAlert {
self.customNavItem.rightBarButtonItems = [self.doneButton, self.photosButton]
} else {
self.customNavItem.rightBarButtonItems = [self.doneButton]
}
}

private func updateTitle() {
guard self.focusedCollection != nil else { return }
self.titleLabel.text = self.focusedCollection?.title
updatePresentLimitedLibraryButton()
}

private func reloadCollectionView() {
Expand Down Expand Up @@ -438,15 +471,6 @@ extension TLPhotosPickerViewController {
self.albumPopView.setupPopupFrame()
}

private func initPhotoLibrary() {
if PHPhotoLibrary.authorizationStatus() == .authorized {
self.photoLibrary.delegate = self
self.photoLibrary.fetchCollection(configure: self.configure)
}else{
//self.dismiss(animated: true, completion: nil)
}
}

private func registerChangeObserver() {
PHPhotoLibrary.shared().register(self)
}
Expand Down Expand Up @@ -503,6 +527,12 @@ extension TLPhotosPickerViewController {
self.dismiss(done: true)
}

@IBAction open func limitButtonTap() {
if #available(iOS 14.0, *) {
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}
}

private func dismiss(done: Bool) {
var shouldDismiss = true
if done {
Expand Down Expand Up @@ -620,13 +650,17 @@ extension TLPhotosPickerViewController: UIImagePickerControllerDelegate, UINavig
}

private func handleDeniedAlbumsAuthorization() {
self.delegate?.handleNoAlbumPermissions(picker: self)
self.handleNoAlbumPermissions?(self)
DispatchQueue.main.async {
self.delegate?.handleNoAlbumPermissions(picker: self)
self.handleNoAlbumPermissions?(self)
}
}

private func handleDeniedCameraAuthorization() {
self.delegate?.handleNoCameraPermissions(picker: self)
self.handleNoCameraPermissions?(self)
DispatchQueue.main.async {
self.delegate?.handleNoCameraPermissions(picker: self)
self.handleNoCameraPermissions?(self)
}
}

open func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
Expand Down
21 changes: 16 additions & 5 deletions TLPhotoPicker/Classes/TLPhotosPickerViewController.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<capability name="Image references" minToolsVersion="12.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -21,6 +22,7 @@
<outlet property="indicator" destination="AEv-G6-dRI" id="coA-3n-07e"/>
<outlet property="navigationBar" destination="X8O-Gg-slz" id="Sp9-g1-r4y"/>
<outlet property="navigationBarTopConstraint" destination="IKp-hS-tTy" id="MFH-eP-0tb"/>
<outlet property="photosButton" destination="spQ-SO-lth" id="Vy3-rM-vY9"/>
<outlet property="popArrowImageView" destination="5zn-je-qLx" id="6k9-cH-vcU"/>
<outlet property="subTitleArrowImageView" destination="b7w-7R-rco" id="IjY-7S-Zz1"/>
<outlet property="subTitleLabel" destination="DON-iU-Cox" id="ZyB-O9-EcR"/>
Expand All @@ -30,6 +32,12 @@
<outlet property="view" destination="Zyk-dI-msE" id="dxK-gh-unF"/>
</connections>
</placeholder>
<barButtonItem title="photo" id="spQ-SO-lth">
<imageReference key="image" image="photo" catalog="system" symbolScale="small"/>
<connections>
<action selector="limitButtonTap" destination="-1" id="VRo-CG-wO6"/>
</connections>
</barButtonItem>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="Zyk-dI-msE">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
Expand All @@ -46,7 +54,7 @@
</connections>
</barButtonItem>
<view key="titleView" contentMode="scaleToFill" id="VAz-Py-dsa">
<rect key="frame" x="87.5" y="0.0" width="200" height="44"/>
<rect key="frame" x="100.5" y="0.0" width="174.5" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="Ql8-7f-9Uk">
Expand Down Expand Up @@ -123,7 +131,7 @@
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7qj-q4-rHC">
<rect key="frame" x="29" y="105" width="42" height="20.5"/>
<rect key="frame" x="29.5" y="105" width="41.5" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -192,6 +200,7 @@
</connections>
</view>
</subviews>
<viewLayoutGuide key="safeArea" id="HLR-WT-D3I"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="HPm-Vc-F86" firstAttribute="bottom" secondItem="HLR-WT-D3I" secondAttribute="bottom" id="0AS-35-SWm"/>
Expand All @@ -212,8 +221,10 @@
<constraint firstItem="4gR-Bn-quP" firstAttribute="bottom" secondItem="HLR-WT-D3I" secondAttribute="bottom" id="qqE-w2-Tsc"/>
<constraint firstAttribute="trailing" secondItem="X8O-Gg-slz" secondAttribute="trailing" id="yWV-L2-0f4"/>
</constraints>
<viewLayoutGuide key="safeArea" id="HLR-WT-D3I"/>
<point key="canvasLocation" x="33.5" y="53.5"/>
</view>
</objects>
<resources>
<image name="photo" catalog="system" width="128" height="93"/>
</resources>
</document>

0 comments on commit dfba254

Please sign in to comment.