How to open the Document files e.g(.pdf,.doc,.docx) in ios mobile when a button action using swift3.0?












8















I need to open UIDocumentPickerViewController and It should allow user to select all type of files. ie.(.pdf,.doc)files I used UIDocumentPickerViewController method.
my Code:



UIDocumentPickerDelegate, UIDocumentMenuDelegate in my class declaration



//upload file



func OpenFile(){

let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .fullScreen
self.present(importMenu, animated: true, completion: nil)

}
@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let cico = url as URL
print("The Url is : (cico)")

do {
let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
print(weatherData)
let activityItems = [weatherData]
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
if UI_USER_INTERFACE_IDIOM() == .phone {
self.present(activityController, animated: true, completion: { _ in })
}
else {
let popup = UIPopoverController(contentViewController: activityController)
popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
}

} catch {
print(error)
}

//optional, case PDF -> render
//displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)


}

@available(iOS 8.0, *)
public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}


func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

print(" cancelled by user")

dismiss(animated: true, completion: nil)

}


In this code the app will crash. the reason is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot initialize a UIDocumentPickerViewController except by the initWithDocumentTypes:inMode: and initWithURL:inMode: initializers.



I don't know how to initialise the initWithDocumentTypes:inMode. I'm new to iOS any one help me???
can you please help me..










share|improve this question





























    8















    I need to open UIDocumentPickerViewController and It should allow user to select all type of files. ie.(.pdf,.doc)files I used UIDocumentPickerViewController method.
    my Code:



    UIDocumentPickerDelegate, UIDocumentMenuDelegate in my class declaration



    //upload file



    func OpenFile(){

    let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .fullScreen
    self.present(importMenu, animated: true, completion: nil)

    }
    @available(iOS 8.0, *)
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    let cico = url as URL
    print("The Url is : (cico)")

    do {
    let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
    print(weatherData)
    let activityItems = [weatherData]
    let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    if UI_USER_INTERFACE_IDIOM() == .phone {
    self.present(activityController, animated: true, completion: { _ in })
    }
    else {
    let popup = UIPopoverController(contentViewController: activityController)
    popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
    }

    } catch {
    print(error)
    }

    //optional, case PDF -> render
    //displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)


    }

    @available(iOS 8.0, *)
    public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)
    }


    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    print(" cancelled by user")

    dismiss(animated: true, completion: nil)

    }


    In this code the app will crash. the reason is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot initialize a UIDocumentPickerViewController except by the initWithDocumentTypes:inMode: and initWithURL:inMode: initializers.



    I don't know how to initialise the initWithDocumentTypes:inMode. I'm new to iOS any one help me???
    can you please help me..










    share|improve this question



























      8












      8








      8


      3






      I need to open UIDocumentPickerViewController and It should allow user to select all type of files. ie.(.pdf,.doc)files I used UIDocumentPickerViewController method.
      my Code:



      UIDocumentPickerDelegate, UIDocumentMenuDelegate in my class declaration



      //upload file



      func OpenFile(){

      let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import)
      importMenu.delegate = self
      importMenu.modalPresentationStyle = .fullScreen
      self.present(importMenu, animated: true, completion: nil)

      }
      @available(iOS 8.0, *)
      public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
      let cico = url as URL
      print("The Url is : (cico)")

      do {
      let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
      print(weatherData)
      let activityItems = [weatherData]
      let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
      if UI_USER_INTERFACE_IDIOM() == .phone {
      self.present(activityController, animated: true, completion: { _ in })
      }
      else {
      let popup = UIPopoverController(contentViewController: activityController)
      popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
      }

      } catch {
      print(error)
      }

      //optional, case PDF -> render
      //displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)


      }

      @available(iOS 8.0, *)
      public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

      documentPicker.delegate = self
      present(documentPicker, animated: true, completion: nil)
      }


      func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

      print(" cancelled by user")

      dismiss(animated: true, completion: nil)

      }


      In this code the app will crash. the reason is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot initialize a UIDocumentPickerViewController except by the initWithDocumentTypes:inMode: and initWithURL:inMode: initializers.



      I don't know how to initialise the initWithDocumentTypes:inMode. I'm new to iOS any one help me???
      can you please help me..










      share|improve this question
















      I need to open UIDocumentPickerViewController and It should allow user to select all type of files. ie.(.pdf,.doc)files I used UIDocumentPickerViewController method.
      my Code:



      UIDocumentPickerDelegate, UIDocumentMenuDelegate in my class declaration



      //upload file



      func OpenFile(){

      let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import)
      importMenu.delegate = self
      importMenu.modalPresentationStyle = .fullScreen
      self.present(importMenu, animated: true, completion: nil)

      }
      @available(iOS 8.0, *)
      public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
      let cico = url as URL
      print("The Url is : (cico)")

      do {
      let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
      print(weatherData)
      let activityItems = [weatherData]
      let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
      if UI_USER_INTERFACE_IDIOM() == .phone {
      self.present(activityController, animated: true, completion: { _ in })
      }
      else {
      let popup = UIPopoverController(contentViewController: activityController)
      popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
      }

      } catch {
      print(error)
      }

      //optional, case PDF -> render
      //displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)


      }

      @available(iOS 8.0, *)
      public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

      documentPicker.delegate = self
      present(documentPicker, animated: true, completion: nil)
      }


      func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

      print(" cancelled by user")

      dismiss(animated: true, completion: nil)

      }


      In this code the app will crash. the reason is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot initialize a UIDocumentPickerViewController except by the initWithDocumentTypes:inMode: and initWithURL:inMode: initializers.



      I don't know how to initialise the initWithDocumentTypes:inMode. I'm new to iOS any one help me???
      can you please help me..







      ios swift3 document uidocument uidocumentmenuvc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 18 '17 at 12:11









      MARTIN

      551517




      551517










      asked Dec 18 '17 at 9:44









      mounimouni

      54111




      54111
























          3 Answers
          3






          active

          oldest

          votes


















          9














          Swift 3*, 4*,



          To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in WKWebView,



             @IBAction func uploadNewResumeAction(_ sender: Any) {

          /* let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) */

          let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

          documentPicker.delegate = self
          present(documentPicker, animated: true, completion: nil)
          }

          extension YourViewController: UIDocumentPickerDelegate{


          func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

          let cico = url as URL
          print(cico)
          print(url)

          print(url.lastPathComponent)

          print(url.pathExtension)

          }
          }


          Note: If you intend to select all files the you have to use following code:



            let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) 


          In your action method.






          share|improve this answer


























          • Hi Abhishek then how to upload the document to the server.

            – mouni
            Dec 18 '17 at 10:00











          • @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

            – Abhishek Mitra
            Dec 18 '17 at 10:02













          • I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

            – mouni
            Dec 18 '17 at 10:10











          • @mouni when did you find the crash ? in which operation ?

            – Abhishek Mitra
            Dec 18 '17 at 10:13











          • I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

            – mouni
            Dec 18 '17 at 10:17



















          7














          Just posting this answer to help other user for this.



          To open the specific document files of (.pdf, .doc, .docx) using UIDocumentPickerViewController in Swift:
          documentTypes would be



          import import MobileCoreServices



          ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String]


          Example:



          let importMenu = UIDocumentPickerViewController(documentTypes: ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String], in: UIDocumentPickerMode.import) 
          importMenu.delegate = self
          self.present(importMenu, animated: true, completion: nil)





          share|improve this answer
























          • do you know how I could get it to pick Google docs?

            – ThE uSeFuL
            Oct 15 '18 at 17:19



















          4















          Actually, instead of WebView you could also use the QuickLook
          Framework, which is designed for this specific purpose. You just pass
          the location of the file and conform to the protocols. It will present
          a view controller with the document inside.




          It supports the following file:



          - iWork documents (Pages, Numbers and Keynote)



          - Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)



          - PDF files



          - Images



          - Text files



          - Rich-Text Format documents



          - Comma-Separated Value files (csv)



          Here is a tutorial by APPCODA, that describes the same.



          and



          Here is the tutorial provided by Apple OBJ-C version.






          share|improve this answer



















          • 2





            Saved my day! Awesome framework.

            – aqsa arshad
            May 2 '18 at 11:27











          • Cool! Glad it helped !

            – Umar Farooque
            May 2 '18 at 11:48






          • 1





            This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

            – Velociround
            Dec 3 '18 at 15:44






          • 1





            @Velociround can't agree more lol.

            – Umar Farooque
            Dec 3 '18 at 17:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47865646%2fhow-to-open-the-document-files-e-g-pdf-doc-docx-in-ios-mobile-when-a-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          Swift 3*, 4*,



          To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in WKWebView,



             @IBAction func uploadNewResumeAction(_ sender: Any) {

          /* let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) */

          let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

          documentPicker.delegate = self
          present(documentPicker, animated: true, completion: nil)
          }

          extension YourViewController: UIDocumentPickerDelegate{


          func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

          let cico = url as URL
          print(cico)
          print(url)

          print(url.lastPathComponent)

          print(url.pathExtension)

          }
          }


          Note: If you intend to select all files the you have to use following code:



            let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) 


          In your action method.






          share|improve this answer


























          • Hi Abhishek then how to upload the document to the server.

            – mouni
            Dec 18 '17 at 10:00











          • @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

            – Abhishek Mitra
            Dec 18 '17 at 10:02













          • I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

            – mouni
            Dec 18 '17 at 10:10











          • @mouni when did you find the crash ? in which operation ?

            – Abhishek Mitra
            Dec 18 '17 at 10:13











          • I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

            – mouni
            Dec 18 '17 at 10:17
















          9














          Swift 3*, 4*,



          To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in WKWebView,



             @IBAction func uploadNewResumeAction(_ sender: Any) {

          /* let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) */

          let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

          documentPicker.delegate = self
          present(documentPicker, animated: true, completion: nil)
          }

          extension YourViewController: UIDocumentPickerDelegate{


          func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

          let cico = url as URL
          print(cico)
          print(url)

          print(url.lastPathComponent)

          print(url.pathExtension)

          }
          }


          Note: If you intend to select all files the you have to use following code:



            let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) 


          In your action method.






          share|improve this answer


























          • Hi Abhishek then how to upload the document to the server.

            – mouni
            Dec 18 '17 at 10:00











          • @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

            – Abhishek Mitra
            Dec 18 '17 at 10:02













          • I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

            – mouni
            Dec 18 '17 at 10:10











          • @mouni when did you find the crash ? in which operation ?

            – Abhishek Mitra
            Dec 18 '17 at 10:13











          • I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

            – mouni
            Dec 18 '17 at 10:17














          9












          9








          9







          Swift 3*, 4*,



          To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in WKWebView,



             @IBAction func uploadNewResumeAction(_ sender: Any) {

          /* let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) */

          let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

          documentPicker.delegate = self
          present(documentPicker, animated: true, completion: nil)
          }

          extension YourViewController: UIDocumentPickerDelegate{


          func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

          let cico = url as URL
          print(cico)
          print(url)

          print(url.lastPathComponent)

          print(url.pathExtension)

          }
          }


          Note: If you intend to select all files the you have to use following code:



            let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) 


          In your action method.






          share|improve this answer















          Swift 3*, 4*,



          To open document and select any document, you are using UIDocumentPickerViewController then all documents presented in your iCloud, Files and in Google Drive will be shown if Google Drive is connected in user device. Then selected document need to download in your app and from there you can show it in WKWebView,



             @IBAction func uploadNewResumeAction(_ sender: Any) {

          /* let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) */

          let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

          documentPicker.delegate = self
          present(documentPicker, animated: true, completion: nil)
          }

          extension YourViewController: UIDocumentPickerDelegate{


          func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

          let cico = url as URL
          print(cico)
          print(url)

          print(url.lastPathComponent)

          print(url.pathExtension)

          }
          }


          Note: If you intend to select all files the you have to use following code:



            let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import) 


          In your action method.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 18 '17 at 10:05

























          answered Dec 18 '17 at 9:54









          Abhishek MitraAbhishek Mitra

          1,69021124




          1,69021124













          • Hi Abhishek then how to upload the document to the server.

            – mouni
            Dec 18 '17 at 10:00











          • @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

            – Abhishek Mitra
            Dec 18 '17 at 10:02













          • I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

            – mouni
            Dec 18 '17 at 10:10











          • @mouni when did you find the crash ? in which operation ?

            – Abhishek Mitra
            Dec 18 '17 at 10:13











          • I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

            – mouni
            Dec 18 '17 at 10:17



















          • Hi Abhishek then how to upload the document to the server.

            – mouni
            Dec 18 '17 at 10:00











          • @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

            – Abhishek Mitra
            Dec 18 '17 at 10:02













          • I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

            – mouni
            Dec 18 '17 at 10:10











          • @mouni when did you find the crash ? in which operation ?

            – Abhishek Mitra
            Dec 18 '17 at 10:13











          • I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

            – mouni
            Dec 18 '17 at 10:17

















          Hi Abhishek then how to upload the document to the server.

          – mouni
          Dec 18 '17 at 10:00





          Hi Abhishek then how to upload the document to the server.

          – mouni
          Dec 18 '17 at 10:00













          @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

          – Abhishek Mitra
          Dec 18 '17 at 10:02







          @mouni well, it is very easy, if you can select document from UIDocumentPickerViewController then you will get file path print(url) from above code of mine. and you need to look at this answer: stackoverflow.com/questions/47754252/… Then that file need to upload like any other like to upload images into the server.

          – Abhishek Mitra
          Dec 18 '17 at 10:02















          I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

          – mouni
          Dec 18 '17 at 10:10





          I have an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?'

          – mouni
          Dec 18 '17 at 10:10













          @mouni when did you find the crash ? in which operation ?

          – Abhishek Mitra
          Dec 18 '17 at 10:13





          @mouni when did you find the crash ? in which operation ?

          – Abhishek Mitra
          Dec 18 '17 at 10:13













          I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

          – mouni
          Dec 18 '17 at 10:17





          I use uiactionsheet. when I click file action it will be crash. I my fileAction i can use let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil)

          – mouni
          Dec 18 '17 at 10:17













          7














          Just posting this answer to help other user for this.



          To open the specific document files of (.pdf, .doc, .docx) using UIDocumentPickerViewController in Swift:
          documentTypes would be



          import import MobileCoreServices



          ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String]


          Example:



          let importMenu = UIDocumentPickerViewController(documentTypes: ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String], in: UIDocumentPickerMode.import) 
          importMenu.delegate = self
          self.present(importMenu, animated: true, completion: nil)





          share|improve this answer
























          • do you know how I could get it to pick Google docs?

            – ThE uSeFuL
            Oct 15 '18 at 17:19
















          7














          Just posting this answer to help other user for this.



          To open the specific document files of (.pdf, .doc, .docx) using UIDocumentPickerViewController in Swift:
          documentTypes would be



          import import MobileCoreServices



          ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String]


          Example:



          let importMenu = UIDocumentPickerViewController(documentTypes: ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String], in: UIDocumentPickerMode.import) 
          importMenu.delegate = self
          self.present(importMenu, animated: true, completion: nil)





          share|improve this answer
























          • do you know how I could get it to pick Google docs?

            – ThE uSeFuL
            Oct 15 '18 at 17:19














          7












          7








          7







          Just posting this answer to help other user for this.



          To open the specific document files of (.pdf, .doc, .docx) using UIDocumentPickerViewController in Swift:
          documentTypes would be



          import import MobileCoreServices



          ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String]


          Example:



          let importMenu = UIDocumentPickerViewController(documentTypes: ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String], in: UIDocumentPickerMode.import) 
          importMenu.delegate = self
          self.present(importMenu, animated: true, completion: nil)





          share|improve this answer













          Just posting this answer to help other user for this.



          To open the specific document files of (.pdf, .doc, .docx) using UIDocumentPickerViewController in Swift:
          documentTypes would be



          import import MobileCoreServices



          ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String]


          Example:



          let importMenu = UIDocumentPickerViewController(documentTypes: ["com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document", kUTTypePDF as String], in: UIDocumentPickerMode.import) 
          importMenu.delegate = self
          self.present(importMenu, animated: true, completion: nil)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 30 '18 at 11:16









          Himanshu padiaHimanshu padia

          4,4753238




          4,4753238













          • do you know how I could get it to pick Google docs?

            – ThE uSeFuL
            Oct 15 '18 at 17:19



















          • do you know how I could get it to pick Google docs?

            – ThE uSeFuL
            Oct 15 '18 at 17:19

















          do you know how I could get it to pick Google docs?

          – ThE uSeFuL
          Oct 15 '18 at 17:19





          do you know how I could get it to pick Google docs?

          – ThE uSeFuL
          Oct 15 '18 at 17:19











          4















          Actually, instead of WebView you could also use the QuickLook
          Framework, which is designed for this specific purpose. You just pass
          the location of the file and conform to the protocols. It will present
          a view controller with the document inside.




          It supports the following file:



          - iWork documents (Pages, Numbers and Keynote)



          - Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)



          - PDF files



          - Images



          - Text files



          - Rich-Text Format documents



          - Comma-Separated Value files (csv)



          Here is a tutorial by APPCODA, that describes the same.



          and



          Here is the tutorial provided by Apple OBJ-C version.






          share|improve this answer



















          • 2





            Saved my day! Awesome framework.

            – aqsa arshad
            May 2 '18 at 11:27











          • Cool! Glad it helped !

            – Umar Farooque
            May 2 '18 at 11:48






          • 1





            This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

            – Velociround
            Dec 3 '18 at 15:44






          • 1





            @Velociround can't agree more lol.

            – Umar Farooque
            Dec 3 '18 at 17:17
















          4















          Actually, instead of WebView you could also use the QuickLook
          Framework, which is designed for this specific purpose. You just pass
          the location of the file and conform to the protocols. It will present
          a view controller with the document inside.




          It supports the following file:



          - iWork documents (Pages, Numbers and Keynote)



          - Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)



          - PDF files



          - Images



          - Text files



          - Rich-Text Format documents



          - Comma-Separated Value files (csv)



          Here is a tutorial by APPCODA, that describes the same.



          and



          Here is the tutorial provided by Apple OBJ-C version.






          share|improve this answer



















          • 2





            Saved my day! Awesome framework.

            – aqsa arshad
            May 2 '18 at 11:27











          • Cool! Glad it helped !

            – Umar Farooque
            May 2 '18 at 11:48






          • 1





            This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

            – Velociround
            Dec 3 '18 at 15:44






          • 1





            @Velociround can't agree more lol.

            – Umar Farooque
            Dec 3 '18 at 17:17














          4












          4








          4








          Actually, instead of WebView you could also use the QuickLook
          Framework, which is designed for this specific purpose. You just pass
          the location of the file and conform to the protocols. It will present
          a view controller with the document inside.




          It supports the following file:



          - iWork documents (Pages, Numbers and Keynote)



          - Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)



          - PDF files



          - Images



          - Text files



          - Rich-Text Format documents



          - Comma-Separated Value files (csv)



          Here is a tutorial by APPCODA, that describes the same.



          and



          Here is the tutorial provided by Apple OBJ-C version.






          share|improve this answer














          Actually, instead of WebView you could also use the QuickLook
          Framework, which is designed for this specific purpose. You just pass
          the location of the file and conform to the protocols. It will present
          a view controller with the document inside.




          It supports the following file:



          - iWork documents (Pages, Numbers and Keynote)



          - Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)



          - PDF files



          - Images



          - Text files



          - Rich-Text Format documents



          - Comma-Separated Value files (csv)



          Here is a tutorial by APPCODA, that describes the same.



          and



          Here is the tutorial provided by Apple OBJ-C version.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 18 '17 at 10:32









          Umar FarooqueUmar Farooque

          1,6031424




          1,6031424








          • 2





            Saved my day! Awesome framework.

            – aqsa arshad
            May 2 '18 at 11:27











          • Cool! Glad it helped !

            – Umar Farooque
            May 2 '18 at 11:48






          • 1





            This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

            – Velociround
            Dec 3 '18 at 15:44






          • 1





            @Velociround can't agree more lol.

            – Umar Farooque
            Dec 3 '18 at 17:17














          • 2





            Saved my day! Awesome framework.

            – aqsa arshad
            May 2 '18 at 11:27











          • Cool! Glad it helped !

            – Umar Farooque
            May 2 '18 at 11:48






          • 1





            This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

            – Velociround
            Dec 3 '18 at 15:44






          • 1





            @Velociround can't agree more lol.

            – Umar Farooque
            Dec 3 '18 at 17:17








          2




          2





          Saved my day! Awesome framework.

          – aqsa arshad
          May 2 '18 at 11:27





          Saved my day! Awesome framework.

          – aqsa arshad
          May 2 '18 at 11:27













          Cool! Glad it helped !

          – Umar Farooque
          May 2 '18 at 11:48





          Cool! Glad it helped !

          – Umar Farooque
          May 2 '18 at 11:48




          1




          1





          This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

          – Velociround
          Dec 3 '18 at 15:44





          This should be the accepted answer. The QuickLook Framework is the best way to render documents, not using WebView.

          – Velociround
          Dec 3 '18 at 15:44




          1




          1





          @Velociround can't agree more lol.

          – Umar Farooque
          Dec 3 '18 at 17:17





          @Velociround can't agree more lol.

          – Umar Farooque
          Dec 3 '18 at 17:17


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47865646%2fhow-to-open-the-document-files-e-g-pdf-doc-docx-in-ios-mobile-when-a-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()