[Info.plist] 추가
- ApplicationSupports iTunes file sharing : YES
- Supports opening documents in place : YES
import UIKit
import UniformTypeIdentifiers
import MobileCoreServices
class PDFDocsAttach: NSObject {
static let shared = PDFDocsAttach()
var pdfDataClosur: ((_ pdfDatas: [Data]) -> Void)?
override init() {
super.init()
}
class func show() {
guard let topVC = UIApplication.currentTopViewController() else {
return
}
var documentPicker: UIDocumentPickerViewController!
if #available(iOS 14, *) {
documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.pdf])
} else {
documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
}
documentPicker.delegate = shared
documentPicker.allowsMultipleSelection = true
documentPicker.modalPresentationStyle = .overFullScreen//.formSheet
topVC.present(documentPicker, animated: true)
}
}
extension PDFDocsAttach: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
var pdfDatas: [Data] = []
for url in urls {
// Make sure you release the security-scoped resource when you finish.
defer {
url.stopAccessingSecurityScopedResource()
}
// Start accessing a security-scoped resource.
guard url.startAccessingSecurityScopedResource() else {
// Handle the failure here.
return
}
do {
let data = try Data.init(contentsOf: url)
pdfDatas.append(data)
// You will have data of the selected file
}
catch let error {
log(direction: .ERROR, ofType: self, datas: error.localizedDescription)
}
}
PDFDocsAttach.shared.pdfDataClosur?(pdfDatas)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
[사용방법]
PDFDocsAttach.show()
PDFDocsAttach.shared.pdfDataClosur = { pdfs in
for i in 0..<pdfs.count {
FileSaveManager.fileSave(fileName: "\(i+1).pdf", data: pdfs[i])
}
}
'Swift > 기타' 카테고리의 다른 글
[SWIFT]Data to UIActivityViewController 파일앱 공유하기 다운로드 (0) | 2023.10.26 |
---|---|
[SWIFT] 생체인증 등록 및 앱 권한 설정 이동 (0) | 2023.10.19 |
[IOS]App 네트워크 패킷 캡쳐 (0) | 2023.09.06 |
LLDB 메모리 덤프 저장 (0) | 2023.09.06 |
[SWIFT]Gif 이미지를 이용한 로딩 UI (0) | 2023.07.19 |