2

I have many files in .pdf format. I'm display the names of the files in a collection view. Now in the didSelectItemAtIndex method, I want to open Acrobat Reader to open the pdf files. How do I implement this ? Also does Acrobat Reader have an URL Scheme so that I can detect whether the app is on the phone ?

Akshay Yerneni
  • 103
  • 2
  • 11

2 Answers2

3

This is the app schema for Adobe reader :

com.adobe.Adobe-Reader

In Swift, you can do the following :

           var docController: UIDocumentInteractionController?
    if let path = Bundle.main.path(forResource: "PDF-NAME", ofType: "pdf") {
            let targetURL = NSURL.fileURL(withPath: path)
            docController = UIDocumentInteractionController(url: targetURL)
            let url = NSURL(string:"com.adobe.Adobe-Reader:");
            if UIApplication.shared.canOpenURL(url! as URL) {
                docController!.presentOpenInMenu(from: .zero, in: self.view, animated: true)
                print("Adobe-Reader")
            }else{
                print("Adobe-Reader is not installed")
            }
    }

And add below app schemas in plist.

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>com.adobe.Adobe-Reader</string>
</array>
Mushrankhan
  • 749
  • 9
  • 12
Fares Benhamouda
  • 589
  • 8
  • 21
  • The document is opening and everything is working fine, but every time the document opens a new file is created inside acrobat reader. Is there any way to check this redundancy ? – Akshay Yerneni Jul 13 '17 at 11:15
  • That's how adobe reader process the file. You cannot change it unfortunalty . Once you delegate the pdf reader mission to adobe, you can do nothing. – Fares Benhamouda Jul 13 '17 at 12:16
  • Yeah that's what I figured. Because if I opened the same thing in iBooks no copies were created there. So yeah....thanks for the help! Cheers! – Akshay Yerneni Jul 14 '17 at 04:44
1

First get the path for your pdf file. They call this to open pdf through acrobat reader

UIApplication.shared.open(URL(string: "com.adobe.adobe-reader://PDFFilePath")!)

But before that, there is no harm to confirm if the reader is already installed or not

if UIApplication.shared.canOpenURL(theURL! as URL) {

}
Fangming
  • 24,551
  • 6
  • 100
  • 90
  • @AkshayYerneni Is your PDF in main bundle or document folder? How are you getting the path for your PDF file? – Fangming Jul 13 '17 at 10:44
  • I managed to open the file in acrobat reader. I got another issue. When I open the a single document multiple times, many copies of the document are created in the acrobat reader menu. For e.g. - I have a doc named Book1.pdf. When I open the doc the second time, another copy of the doc, named Book1(2).pdf is created and so on. Is there any way to check this redundancy ? – Akshay Yerneni Jul 13 '17 at 11:31
  • @AkshayYerneni Sadly, no. You can hand over the pdf file to their reader but there is no way you can change the way they process the file. So it's actually up to them to handle this duplicated file issue, not you. – Fangming Jul 13 '17 at 11:41
  • Ah thanks ! I wasted a lot of time to figure out if this issue was coming from my end of the code ! – Akshay Yerneni Jul 14 '17 at 04:43
  • What is my path is storage from phone ie ,file:///var/mobile/Containers/Data/Application/0A08AE0B-E6D7-4F6E-81BF-CEB4A05248C2/Documents/0sFOIb.pdf , – Rishabh Dugar Oct 11 '17 at 13:59
  • @RishabhDugar Please see this post to get path on your iphone for document directory, the only place you can put your app related files https://stackoverflow.com/a/45085638/5838902 – Fangming Oct 11 '17 at 14:08
  • @FangmingNing, thanks for reply, though this was dead post, actually i already have a file url , my use case is -> I save file from some Firebase Url to local and i want to display it in reader now, already opening in internal PDFView kit ,, but want external ,, , this is one of my URL "file:///var/mobile/Containers/Data/Application/672F544B-E817-4D1B-A0C9-2A71BCAAC2EB/Documents/WIcH8J.pdf" ,, i tried many things like "com.adobe.adobe-reader:///var/mobil.../Documents/WIcH8J.pdf"" , removing "/" etc , any indicator would help, thanks – Rishabh Dugar Oct 11 '17 at 14:29
  • or it is possible to give direct web url to adobe reader , but i read some post adobe reader does not yet support that, something like "com.adobe.adobe-reader:http://something.pdf" – Rishabh Dugar Oct 11 '17 at 14:33
  • @RishabhDugar You need to understand this. Each app on iOS runs in it's own sandbox and has access to only it's own document folder. You have to either put the pdf in your main bundle (project file list), or the document folder. Then you can have a valid path for pfd reader. The path you provided does not seem like a valid document path. Here is a valid path for my project. /Users/fangmingning/Library/Developer/CoreSimulator/Devices/FC4CF4F3-455B-4299-9D81-0FF3BFE2B31D/data/Containers/Data/Application/CE2FCB04-D01A-48DE-9AA3-B1EE3F418727/Documents – Fangming Oct 11 '17 at 14:40
  • Oh I think I got your point there, i should try to place my downloaded file in space that each app has for proper storage. Thank you so much for sharing this knowledge. Cheers , Could you help with some sample Document path and how i would use that in your solution? – Rishabh Dugar Oct 11 '17 at 14:47
  • @RishabhDugar Yup and more accurately, you are ONLY allowed to put files to the app's own document folder, no matter where you get it from. So go to the link I mentioned and find your document folder for you iphone simulator. Place pdf file there and have a try. If you run on simulator you will get a path on your laptop. If you run it on your phone, it will give you the path of the document folder on your phone. Happy coding https://stackoverflow.com/a/45085638/5838902 – Fangming Oct 11 '17 at 14:51
  • Looks Like this is what i have been doing , let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL! let destinationFileUrl = documentsUrl.appendingPathComponent(self.finalFileName+".pdf") print("destinationFileUrl") destinationfile url give me URL like i mentioned earlier. :) sorry for bugging you :P – Rishabh Dugar Oct 11 '17 at 14:54
  • I'm using the above code and I'm able to open Adobe app but I'm unable to open the PDF which is in main bundle.Can u pls help me @Fangming – Catherine Dec 14 '18 at 08:47
  • @Suganya have you looked at the comments above? There are some good information there. Try to run your app in the simulator, print the pdf path and go to that path in your laptop. Maybe you are not getting the path correctly – Fangming Dec 14 '18 at 15:08