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.imageUTType.textUTType.plainTextUTType.utf8PlainText,    UTType.utf16ExternalPlainTextUTType.utf16PlainText,    UTType.delimitedTextUTType.commaSeparatedText,    UTType.tabSeparatedTextUTType.utf8TabSeparatedTextUTType.rtf,    UTType.pdfUTType.webArchiveUTType.imageUTType.jpeg,    UTType.tiffUTType.gifUTType.pngUTType.bmpUTType.ico,    UTType.rawImageUTType.svgUTType.livePhotoUTType.movie,    UTType.videoUTType.audioUTType.quickTimeMovieUTType.mpeg,    UTType.mpeg2VideoUTType.mpeg2TransportStreamUTType.mp3,    UTType.mpeg4MovieUTType.mpeg4AudioUTType.aviUTType.aiff,    UTType.wavUTType.midiUTType.archiveUTType.gzipUTType.bz2,    UTType.zipUTType.appleArchiveUTType.spreadsheetUTType.epub

]


        let documentPickerController = UIDocumentPickerViewController(

                forOpeningContentTypes: supportedTypes)

        documentPickerController.delegate = self

        self.present(documentPickerController, animatedtruecompletionnil)

    }



Add the extension as like below and get back the pdf URL from the path inside the finishpicking function


extension YourviewController:UIDocumentPickerDelegate {

    public func documentPicker(_ controller: UIDocumentPickerViewControllerdidPickDocumentsAt urls: [URL]) {

        guard let myURL = urls.first else {

            return

        }

        let path = myURL.absoluteString

        print("Url  : \(myURL)")

    }

          


    public func documentMenu(_ documentMenu:UIDocumentMenuViewControllerdidPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

        documentPicker.delegate = self

        present(documentPicker, animatedtruecompletionnil)

    }



    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

        print("view was cancelled")

        dismiss(animatedtruecompletionnil)

    }

}



Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift