Select PDF file and upload to server in iOS swift || PDF picker in iOS
Solution:
In iOS we can now upload pdf files from your local device to server.Follow the below steps to achieve..
First you must have the deployment target above 14 in your app.
Before you have to import the below frameworks in your project
import MobileCoreServices
import UniformTypeIdentifiers
Then use the below function
// MARK: - File selection
@available(iOS 14.0, *)
func selectFiles() {
let supportedTypes = [UTType.image, UTType.text, UTType.plainText, UTType.utf8PlainText, UTType.utf16ExternalPlainText, UTType.utf16PlainText, UTType.delimitedText, UTType.commaSeparatedText, UTType.tabSeparatedText, UTType.utf8TabSeparatedText, UTType.rtf, UTType.pdf, UTType.webArchive, UTType.image, UTType.jpeg, UTType.tiff, UTType.gif, UTType.png, UTType.bmp, UTType.ico, UTType.rawImage, UTType.svg, UTType.livePhoto, UTType.movie, UTType.video, UTType.audio, UTType.quickTimeMovie, UTType.mpeg, UTType.mpeg2Video, UTType.mpeg2TransportStream, UTType.mp3, UTType.mpeg4Movie, UTType.mpeg4Audio, UTType.avi, UTType.aiff, UTType.wav, UTType.midi, UTType.archive, UTType.gzip, UTType.bz2, UTType.zip, UTType.appleArchive, UTType.spreadsheet, UTType.epub
]
let documentPickerController = UIDocumentPickerViewController
forOpeningContentTypes: supportedTypes)
documentPickerController.deleg
self.present(
}
Add the extension as like below and get back the pdf URL from the path inside the finishpicking function
extension YourviewController:U
public func documentPicker(_ controller: UIDocumentPickerViewController
guard let myURL = urls.first else {
return
}
let path = myURL.absoluteString
print("Url : \(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuVie
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
}
Comments
Post a Comment