Cannot preview .xlsx file using UIDocumentInteractionController in swift 4 - ios

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.

Related

Swift UIDocumentInteractionController not launching for preview my file

My scenario, I am storing local some files and getting path to assigning UIDocumentInteractionController file path but UIDocumentInteractionController not opening.
My Code Below
func filepreview(filepath: String) {
let dc = UIDocumentInteractionController(url: URL(fileURLWithPath: filepath))
dc.delegate = self
dc.presentPreview(animated: true)
}
Warning: Attempt to present on
whose view is not in the window
hierarchy!

How to show particular file format from iclouds derive in ios swift

In my application upload the file from iclouds derive. want to upload xlsx, docx, txt.
want to upload the xlsx file means from iclouds derive want to display only xlsx another files should not display.[docx,txt]
same if the user want to display docx file means xlsx and txt ,xlsx file should not be display.
Here my code
#IBAction func moveiclouds_BtnClick(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String, "com.microsoft.word.doc", "com.microsoft.excel.xls", kUTTypeCompositeContent as String, kUTTypeJPEG as String], in: .import)
documentPicker.delegate = self
self.present(documentPicker, animated: true, completion: nil)
}
extension CreateFeesView: UIDocumentPickerDelegate{
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let cico = url as URL
cico_files = cico
filepath_fees = url.pathExtension
}
}
from iclouds derive all files are diplaying in recently added list. want to display only particular file in the list. how to achive help me. Thank advance
Just adjust the documentTypes: argument when creating your document picker.
documentTypes: ["org.openxmlformats.wordprocessingml.document"]
will let the user only select docx files.
documentTypes: ["org.openxmlformats.spreadsheetml.sheet"]
will let the user only select xlsx files.

How to open Acrobat Reader to read pdf file?

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

iOS Action Extension share from GarageBand

I am building an action extension for GarageBand on iOS which transforms and uploads audio but no matter what I try, I just could not get to the exported file.
Let’s consider the following code — it should:
find and load shared audio from extensionContext
initialise audio player
play the sound
It works if I run the extension in Voice Memos.app — the url to the file looks like this: file:///tmp/com.apple.VoiceMemos/New%20Recording%202.m4a
Now, If I run the code in GarageBand.app, the url points to (what I presume) is GarageBand’s app container, as the url looks something like /var/…/Containers/…/Project.band/audio/Project.m4a, and the audio will not be loaded and cannot therefore be manipulated in any way.
// edit: If I try to load contents of the audio file, it looks like the data only contains aac header (?) but the rest of the file is empty
What is interesting is this: The extension renders a react-native view and if I pass the view the fileUrl (/var/…Project.band/audio/Project.m4a) and then pass it down to XMLHTTPRequest, the file gets uploaded. So it looks like the file can be accessed in some way?
I’m new to Swift/iOS development so this is kind of frustrating for me, I feel like I tried just about everything.
The code:
override func viewDidLoad() {
super.viewDidLoad()
var audioFound :Bool = false
for inputItem: Any in self.extensionContext!.inputItems {
let extensionItem = inputItem as! NSExtensionItem
for attachment: Any in extensionItem.attachments! {
print("attachment = \(attachment)")
let itemProvider = attachment as! NSItemProvider
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeAudio as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeAudio as String,
options: nil, completionHandler: { (audioURL, error) in
OperationQueue.main.addOperation {
if let audioURL = audioURL as? NSURL {
print("audioUrl = \(audioURL)")
// in our sample code we just present and play the audio in our app extension
let theAVPlayer :AVPlayer = AVPlayer(url: audioURL as URL)
let theAVPlayerViewController :AVPlayerViewController = AVPlayerViewController()
theAVPlayerViewController.player = theAVPlayer
self.present(theAVPlayerViewController, animated: true) {
theAVPlayerViewController.player!.play()
}
}
}
})
audioFound = true
break
}
}
if (audioFound) {
break
}
}
}

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.

Resources