From 851f8f2a991c11b6ecdf799714c603bbe3334934 Mon Sep 17 00:00:00 2001 From: mark Date: Tue, 30 Mar 2021 21:24:09 +0800 Subject: [PATCH 1/4] Reset with first album when there're no album after user granted some of them --- .../ImagePickerController+Albums.swift | 1 + .../Controller/ImagePickerController.swift | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Sources/Controller/ImagePickerController+Albums.swift b/Sources/Controller/ImagePickerController+Albums.swift index a5020cb1..5a75f8db 100644 --- a/Sources/Controller/ImagePickerController+Albums.swift +++ b/Sources/Controller/ImagePickerController+Albums.swift @@ -34,6 +34,7 @@ extension ImagePickerController: AlbumsViewControllerDelegate { } func select(album: PHAssetCollection) { + self.currentAlbum = album assetsViewController.showAssets(in: album) albumButton.setTitle((album.localizedTitle ?? "") + " ", for: .normal) albumButton.sizeToFit() diff --git a/Sources/Controller/ImagePickerController.swift b/Sources/Controller/ImagePickerController.swift index 85b086de..98fdc6af 100644 --- a/Sources/Controller/ImagePickerController.swift +++ b/Sources/Controller/ImagePickerController.swift @@ -56,7 +56,7 @@ import Photos let dropdownTransitionDelegate = DropdownTransitionDelegate() let zoomTransitionDelegate = ZoomTransitionDelegate() - lazy var albums: [PHAssetCollection] = { + private func loadAlbums() -> [PHAssetCollection] { // We don't want collections without assets. // I would like to do that with PHFetchOptions: fetchOptions.predicate = NSPredicate(format: "estimatedAssetCount > 0") // But that doesn't work... @@ -74,6 +74,11 @@ import Photos let assetsFetchResult = PHAsset.fetchAssets(in: $0, options: fetchOptions) return assetsFetchResult.count > 0 } + } + var currentAlbum: PHAssetCollection? + + lazy var albums: [PHAssetCollection] = { + self.loadAlbums() }() public init(selectedAssets: [PHAsset] = []) { @@ -86,6 +91,16 @@ import Photos fatalError("init(coder:) has not been implemented") } + open override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + PHPhotoLibrary.shared().register(self) + } + + open override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + PHPhotoLibrary.shared().unregisterChangeObserver(self) + } + public override func viewDidLoad() { super.viewDidLoad() @@ -161,3 +176,19 @@ import Photos albumButton.isHidden = albums.count < 2 } } + +extension ImagePickerController: PHPhotoLibraryChangeObserver { + public func photoLibraryDidChange(_ changeInstance: PHChange) { + if let currentAlbum = currentAlbum, + changeInstance.changeDetails(for: currentAlbum) != nil { + return + } + DispatchQueue.main.async { + self.albums = self.loadAlbums() + self.updateAlbumButton() + if let firstAlbum = self.albums.first { + self.select(album: firstAlbum) + } + } + } +} From 25833a5f40f8c4aa83db93600ff987974f3abdc5 Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 31 Mar 2021 12:55:39 +0800 Subject: [PATCH 2/4] Fix request photo auth method for iOS 14 --- .../ImagePickerController+Closure.swift | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Sources/Controller/ImagePickerController+Closure.swift b/Sources/Controller/ImagePickerController+Closure.swift index 2b98e6e1..05a9a639 100644 --- a/Sources/Controller/ImagePickerController+Closure.swift +++ b/Sources/Controller/ImagePickerController+Closure.swift @@ -54,12 +54,23 @@ import Photos } private func authorize(_ authorized: @escaping () -> Void) { - PHPhotoLibrary.requestAuthorization { (status) in - switch status { - case .authorized: - DispatchQueue.main.async(execute: authorized) - default: - break + if #available(iOS 14, *) { + PHPhotoLibrary.requestAuthorization(for: .readWrite) { (status) in + switch status { + case .authorized: + DispatchQueue.main.async(execute: authorized) + default: + break + } + } + } else { + PHPhotoLibrary.requestAuthorization { (status) in + switch status { + case .authorized: + DispatchQueue.main.async(execute: authorized) + default: + break + } } } } From 8d4ce61772d216e8e4482d4af208775485ad83b3 Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 31 Mar 2021 13:01:22 +0800 Subject: [PATCH 3/4] Fix allowed access photo permission status on iOS 14 --- Sources/Controller/ImagePickerController+Closure.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Controller/ImagePickerController+Closure.swift b/Sources/Controller/ImagePickerController+Closure.swift index 05a9a639..f5d21186 100644 --- a/Sources/Controller/ImagePickerController+Closure.swift +++ b/Sources/Controller/ImagePickerController+Closure.swift @@ -57,7 +57,8 @@ import Photos if #available(iOS 14, *) { PHPhotoLibrary.requestAuthorization(for: .readWrite) { (status) in switch status { - case .authorized: + case .authorized, + .limited: DispatchQueue.main.async(execute: authorized) default: break From dc7ef14daceb5165d35a67a63ff1445f7237ac27 Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 31 Mar 2021 17:47:25 +0800 Subject: [PATCH 4/4] Fix photo permission request not response once user cancel it --- .../ImagePickerController+Closure.swift | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/Sources/Controller/ImagePickerController+Closure.swift b/Sources/Controller/ImagePickerController+Closure.swift index f5d21186..857931aa 100644 --- a/Sources/Controller/ImagePickerController+Closure.swift +++ b/Sources/Controller/ImagePickerController+Closure.swift @@ -55,22 +55,35 @@ import Photos private func authorize(_ authorized: @escaping () -> Void) { if #available(iOS 14, *) { - PHPhotoLibrary.requestAuthorization(for: .readWrite) { (status) in - switch status { - case .authorized, - .limited: - DispatchQueue.main.async(execute: authorized) - default: - break + let immediateStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite) + switch immediateStatus { + case .authorized, + .limited: + DispatchQueue.main.async(execute: authorized) + default: + PHPhotoLibrary.requestAuthorization(for: .readWrite) { (status) in + switch status { + case .authorized, + .limited: + DispatchQueue.main.async(execute: authorized) + default: + break + } } } } else { - PHPhotoLibrary.requestAuthorization { (status) in - switch status { - case .authorized: - DispatchQueue.main.async(execute: authorized) - default: - break + let immediateStatus = PHPhotoLibrary.authorizationStatus() + switch immediateStatus { + case .authorized: + DispatchQueue.main.async(execute: authorized) + default: + PHPhotoLibrary.requestAuthorization { (status) in + switch status { + case .authorized: + DispatchQueue.main.async(execute: authorized) + default: + break + } } } }