-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Permission handling in image picker #334
base: master
Are you sure you want to change the base?
Changes from 2 commits
fcdc803
ecca308
fa56af0
5887d42
994b816
8cacfff
01220e0
dff1e7b
8e9eb1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import Photos | |
@objcMembers open class ImagePickerController: UINavigationController { | ||
// MARK: Public properties | ||
public weak var imagePickerDelegate: ImagePickerControllerDelegate? | ||
|
||
public var settings: Settings = Settings() | ||
public var doneButton: UIBarButtonItem = UIBarButtonItem(title: "", style: .done, target: nil, action: nil) | ||
public var cancelButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: nil, action: nil) | ||
|
@@ -47,6 +48,7 @@ import Photos | |
var onDeselection: ((_ asset: PHAsset) -> Void)? | ||
var onCancel: ((_ assets: [PHAsset]) -> Void)? | ||
var onFinish: ((_ assets: [PHAsset]) -> Void)? | ||
var onPermissionChange: ((_ status: PHAuthorizationStatus) -> Void)? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not used anywhere There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
|
||
let assetsViewController: AssetsViewController | ||
let albumsViewController = AlbumsViewController() | ||
|
@@ -93,6 +95,7 @@ import Photos | |
// Setup view controllers | ||
albumsViewController.delegate = self | ||
assetsViewController.delegate = self | ||
|
||
|
||
viewControllers = [assetsViewController] | ||
view.backgroundColor = settings.theme.backgroundColor | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2021 Mithilesh Parmar | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import UIKit | ||
import Photos | ||
|
||
extension AssetsViewController { | ||
internal func handleDeniedAccess(){ | ||
let alert = UIAlertController(title: "Allow access to your photos", | ||
message: "This lets you share from your camera roll and enables other features for photos. Go to your settings and tap \"Photos\".", | ||
preferredStyle: .alert) | ||
|
||
let notNowAction = UIAlertAction(title: "Not Now", | ||
style: .cancel, | ||
handler: nil) | ||
alert.addAction(notNowAction) | ||
|
||
let openSettingsAction = UIAlertAction(title: "Open Settings", | ||
style: .default) { [unowned self] (_) in | ||
// Open app privacy settings | ||
gotoAppPrivacySettings() | ||
} | ||
alert.addAction(openSettingsAction) | ||
present(alert, animated: true, completion: nil) | ||
} | ||
|
||
internal func handleLimitedAccess(){ | ||
let actionSheet = UIAlertController(title: "", | ||
message: "Select more photos or go to Settings to allow access to all photos.", | ||
preferredStyle: .actionSheet) | ||
|
||
let selectPhotosAction = UIAlertAction(title: "Select more photos", | ||
style: .default) { [unowned self] (_) in | ||
// Show limited library picker | ||
if #available(iOS 14, *) { | ||
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self) | ||
} | ||
} | ||
actionSheet.addAction(selectPhotosAction) | ||
|
||
let allowFullAccessAction = UIAlertAction(title: "Allow access to all photos", | ||
style: .default) { [unowned self] (_) in | ||
// Open app privacy settings | ||
self.gotoAppPrivacySettings() | ||
} | ||
actionSheet.addAction(allowFullAccessAction) | ||
|
||
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) | ||
actionSheet.addAction(cancelAction) | ||
|
||
present(actionSheet, animated: true, completion: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get crash on iPad because the popoverPresentationController?.sourceView is not set. Send here the "manage" button so sheet is displayed properly |
||
|
||
} | ||
|
||
internal func gotoAppPrivacySettings() { | ||
guard let url = URL(string: UIApplication.openSettingsURLString), | ||
UIApplication.shared.canOpenURL(url) else { | ||
assertionFailure("Not able to open App privacy settings") | ||
return | ||
} | ||
|
||
UIApplication.shared.open(url, options: [:], completionHandler: nil) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2021 Mithilesh Parmar | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import Foundation | ||
|
||
|
||
@propertyWrapper | ||
public struct UsesAutoLayout<T: UIView> { | ||
public var wrappedValue: T { | ||
didSet { | ||
wrappedValue.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
} | ||
|
||
public init(wrappedValue: T) { | ||
self.wrappedValue = wrappedValue | ||
wrappedValue.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
} | ||
|
||
|
||
@propertyWrapper | ||
public struct HasUserInteraction<T: UIView> { | ||
public var wrappedValue: T { | ||
didSet { | ||
wrappedValue.isUserInteractionEnabled = true | ||
} | ||
} | ||
|
||
public init(wrappedValue: T) { | ||
self.wrappedValue = wrappedValue | ||
wrappedValue.isUserInteractionEnabled = true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2021 Mithilesh Parmar | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import UIKit | ||
import Photos | ||
|
||
|
||
extension AssetsViewController { | ||
|
||
internal func checkAuthorizationStatus(){ | ||
|
||
if #available(iOS 14, *) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this method |
||
PHPhotoLibrary.requestAuthorization(for: .readWrite) { [unowned self] (status) in | ||
DispatchQueue.main.async { | ||
self.dataSource.setStatus(status) | ||
self.collectionView.reloadData() | ||
} | ||
} | ||
}else{ | ||
PHPhotoLibrary.requestAuthorization { (status) in | ||
DispatchQueue.main.async { | ||
self.dataSource.setStatus(status) | ||
self.collectionView.reloadData() | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should not be present in the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced with original