Info.plist will need the following two lines
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
import UIKit | |
import PhotosUI | |
class ViewController: UIViewController { | |
@IBAction func presentPickerForImagesIncludingLivePhotos(_ sender: Any ) { | |
presentPicker( filter: PHPickerFilter.images ) | |
} | |
private func presentPicker( filter: PHPickerFilter ) { | |
let photoLibrary = PHPhotoLibrary.shared() // Retrieve PHAsset | |
var configuration = PHPickerConfiguration( photoLibrary: photoLibrary ) | |
configuration.filter = filter | |
configuration.selectionLimit = 0 | |
let picker = PHPickerViewController( configuration: configuration ) | |
picker.delegate = self | |
present( picker, animated: true ) | |
} | |
} | |
extension ViewController: PHPickerViewControllerDelegate { | |
func picker(_ picker: PHPickerViewController, didFinishPicking results: [ PHPickerResult ] ) { | |
dismiss( animated: true ) | |
let identifiers = results.compactMap( \.assetIdentifier ) | |
let fetchResult = PHAsset.fetchAssets( withLocalIdentifiers: identifiers, options: nil ) | |
fetchResult.enumerateObjects { asset, index, pointer in | |
print( "This asset was selected: \( asset.localIdentifier )" ) | |
} | |
} | |
} |