This code will use for Swift 5 And iOS 12. If we use this code we can get Documents and Images File type
import UIKit
import Photos
class ViewController {
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
checkPermission()
}
}
extension ViewController : UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIDocumentPickerDelegate {
func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
accessDocsAndImages()
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ newStatus in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
print("success")
}
})
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
@unknown default:
print("Defults")
}
}
func accessDocsAndImages() {
imagePicker.allowsEditing = true
imagePicker.delegate = self
let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .alert)
let openCameraAction = UIAlertAction(title: "Camera", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
return
})
let openGalleryAction = UIAlertAction(title: "Open Gallery", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.imagePicker.sourceType = .photoLibrary
self.present(self.imagePicker, animated: true, completion: nil)
print("Opened gallery")
})
let openDocAction = UIAlertAction(title: "Open Documents", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
print("Opened gallery")
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.content", "public.item", "public.composite-content", "public.data", "public.database", "public.calendar-event", "public.message", "public.presentation", "public.contact", "public.archive", "public.disk-image", "public.text", "public.plain-text", "public.utf8-plain-text", "public.utf16-external-plain-text", "public.utf16-plain-text", "com.apple.traditional-mac-plain-text", "public.rtf","public.pdf", "public.movie", "public.audiovisual-content", "public.video", "public.audio"], in: .import)
documentPicker.delegate = self
//documentPicker.modalPresentationStyle = .formSheet
documentPicker.allowsMultipleSelection = true
self.present(documentPicker, animated: true, completion: nil)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("cancelled")
})
optionMenu.addAction(openGalleryAction)
optionMenu.addAction(openCameraAction)
optionMenu.addAction(openDocAction)
optionMenu.addAction(cancelAction)
self.present(optionMenu, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
if (assetResources.first!.originalFilename.hasSuffix("JPG")) {
print("jpg")
} else if (assetResources.first!.originalFilename.hasSuffix("JPEG")) {
print("jpeg")
} else if (assetResources.first!.originalFilename.hasSuffix("PNG")) {
print("png")
} else if (assetResources.first!.originalFilename.hasSuffix("GIF")) {
print("gif")
} else if (assetResources.first!.originalFilename.hasSuffix("TIFF")) {
print("tiff")
} else if (assetResources.first!.originalFilename.hasSuffix("WEBP")) {
print("webp")
} else if (assetResources.first!.originalFilename.hasSuffix("HEIC")) {
print("heic")
} else if (assetResources.first!.originalFilename.hasSuffix("HEIX")) {
print("heix")
} else if (assetResources.first!.originalFilename.hasSuffix("HEVC")) {
print("hevc")
} else if (assetResources.first!.originalFilename.hasSuffix("HEVX")) {
print("hevx")
} else {
print("Unknown")
}
}
dismiss(animated: true, completion: nil)
}
private func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print(url)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let cico = urls[0] as URL
print(cico)
print(urls[0])
print(urls[0].lastPathComponent)
print(urls[0].pathExtension)
let urlWithoutFileExtension: URL = urls[0].deletingPathExtension()
let fileNameWithoutExtension: String = urlWithoutFileExtension.lastPathComponent
print(fileNameWithoutExtension)
dismiss(animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print(" cancelled by user")
}
}