How to open Acrobat Reader to read pdf file? - ios

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 ?

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>

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) {
}

Related

File sharing using UIActivityViewController/ UIDocumentInteractionController not working - [can't share via AirDrop, Whatapp, iMessage, Mail, etc.]

While trying to share an '.mp3' file that I have downloaded onto my app using the device's share-sheet feature, I'm able to fetch the file from the documents directory but unable to share it.
When I try to share it via Whatsapp, I get an error saying:
This item cannot be shared. Please select a different item.
If I share via AirDrop, it just says "Failed" (in red) and sharing via other options leads to a blank message (e.g. a new email with no file attached or a new msg with nothing in it)
Here are the code options I've tried so far:
Option 1 - Using UIActivityViewController
let fileURL = NSURL(fileURLWithPath: FileURLFromDocumentsDirectory)
var filesToShare = [Any]()
filesToShare.append(fileURL)
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Option 2 - Using UIDocumentInteractionController
let docController : UIDocumentInteractionController?
docController = UIDocumentInteractionController(url: URL(string: fileToSharePath)!)
docController?.delegate = self
docController?.presentOptionsMenu(from: self.view.frame, in:self.view, animated:true)
In both options, I do get the file but can't share it. The phone does recognize the share as an audio file but I can't do anything with it.
Sample URL:
file:///Users/UserName/Library/Developer/CoreSimulator/Devices/C8B09E62-091F-4E61-BA6C-3D9EC23LKC01/data/Containers/Data/Application/EF2CKKJF-AB35-55G5-B778-439812EFGGG5/Documents/audioFile.mp3
The sharing works for text but not for the audio files.
Do I also need to enable some features or add some code to the AppDelegate?
Appreciate any help. Thanks.
Update #1: Code with Suggestions from #dfd [still doesn't work]
let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, completionHandler) in
let fileURL = NSURL(fileURLWithPath: (downloadedFile?.documentDirectoryURL)!)
let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
Also, for all these options, I get the audio file (mp3) as an 'Audio Recording' in the Share Sheet.
Instead of sharing the original file, can you try to create a copy of this in a temporary directory and share that copy. Once done with sharing you can delete or leave it to the system to delete it.
Something like this (You can add more check about file and directory existance):
let fileManager = FileManager.default
let tempDirectory = fileManager.temporaryDirectory
let tempPath = tempDirectory.path + "/" + fileToSharePath.lastPathComponent
let tempURL = URL(fileURLWithPath: tempPath)
try? fileManager.copyItem(atPath:fileToSharePath.path , toPath: tempPath)
let docController : UIDocumentInteractionController?
docController = UIDocumentInteractionController(url: URL(string: tempURL)!)
docController?.delegate = self
docController?.presentOptionsMenu(from: self.view.frame, in:self.view, animated:true)

Cannot preview .xlsx file using UIDocumentInteractionController in swift 4

I have an application which will download .xlsx, .pdf files from a given url. When downloaded will show a local notification. Upon clicking the notification , preview of the downloaded file should be shown. I can download and preview a pdf file, but when it comes to .xlsx file, I can't preview it. I used UIDocumentInteractionController and UIDocumentInteractionControllerDelegate to interact with the file's path to preview it.
func showFileWithPath(path: String)
{
print(path)
let isFileFound:Bool? = FileManager.default.fileExists(atPath: path)
if isFileFound == true
{
let viewer = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
viewer.delegate = self
viewer.presentPreview(animated: true)
}
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
{
return self.navigationController!
}
This showFileWithPath(path: String) method will be called in UserNotification delegate method and the path of the file is passed as the parameter. This code is working fine for pdf files. But when it comes to .xlsx files, the preview is not showing any contents, just showing loading. The same case when tried with QuickLook Framework.

iOS sharing document to WhatsApp using UIDocumentInteractionController

I'm trying to share a simple .txt file to WhatsApp using UIDocumentInteractionController on iOS.
I found this FAQ of WhatsApp explaining the Custom URL Scheme for WhatsApp, but it is not explaining how to share another document to WhatsApp. This should be possible with a WhatsApp update released about 2 months ago.
I also found this answer, but it does not work for other document types. Can anyone help me?
My code, that does not work, because the URL Scheme is wrong:
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL) {
activityVc = UIDocumentInteractionController(url: URL(fileURLWithPath: txtPath))
activityVc.uti = "net.whatsapp.document"
activityVc.presentOptionsMenu(from: CGRect.zero, in: self.view, animated: true)
}
}

PDF file not open in UIDocumentInteractionController

I am working on the simulator any there are no apps like adobe to open the pdf files....i am using UIDocumentInteractionController to open the pdf file like below.
let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")
print("--------------------------------- str is \(url)")
if (url != nil) {
// Initialize Document Interaction Controller
self.documentInteractionController = UIDocumentInteractionController(url: url)
// Configure Document Interaction Controller
self.documentInteractionController?.delegate = self
// Preview PDF
self.documentInteractionController?.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
My screen is getting displayed like below
No pdf is being displayed here...is it because there is no app in my simulator which could open pdf files?
Not sure if any Simulator apps will handle pdf documents - maybe News? But you can just add a webView to a view controller and use that to view your pdf.
#IBOutlet weak var webView: UIWebView!
Do the usual safety checks on your url and then:
webView.loadRequest(NSURLRequest(url: url as URL) as URLRequest)
I think there is a path issue.
Replace
let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")
With
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("samplepdf.pdf")
for Swift 3/Xcode 8
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("samplepdf.pdf") as NSURL
Hope that will help.

Sharing images to Whatsapp using UIDocumentIntractionController in ios

Is it possible to share Image and Text from an iOS app to WhatsApp using UIDocumentIntractionController or API in iPhone application development?
Please look at this link from the FAQ:
http://www.whatsapp.com/faq/en/iphone/23559013
I have found that If your application creates photos, videos or audio notes and you’d like your users to share these media using WhatsApp, you can use the Document Interaction API to send your media to your WhatsApp contacts and groups.
You can refer this link : http://www.whatsapp.com/faq/en/iphone/23559013
In Swift 3 use this code
#IBAction func whatsappShareWithImages(_ sender: AnyObject)
{
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
if let image = UIImage(named: "whatsappIcon") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
self.documentInteractionController.uti = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
}
Add this code in your app "plist"
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
You can also refer to small app for reference : https://github.com/nithinbemitk/iOS-Whatsapp-Share

Resources