I have managed to download pdf file to the device.
I get this file:///var/mobile/Containers/Data/Application/DBC9AFAB-3FD4-4D06-82F5-0577251001A7/Library/Caches/RewardMe-Presentation-at-NVIDIA-Auditorium.pdf path
So I tried to open it on an UIWebView like this
let downloadedFilePath = "file:///var/mobile/Containers/Data/Application/DBC9AFAB-3FD4-4D06-82F5-0577251001A7/Library/Caches/RewardMe-Presentation-at-NVIDIA-Auditorium.pdf"
let filePathURL = NSURL(fileURLWithPath: downloadedFilePath);
webView.loadRequest(NSURLRequest(URL: url));
But the WebView fails to load the pdf. It only shows a blank webview
Any ideas why this does not work?
You code looks ok but the path is not
let downloadedFilePath = "///var/mobile/Containers/Data/Application/DBC9AFAB-3FD4-4D06-82F5-0577251001A7/Library/Caches/RewardMe-Presentation-at-NVIDIA-Auditorium.pdf"
let filePathURL = NSURL(fileURLWithPath: downloadedFilePath);
webView.loadRequest(NSURLRequest(URL: url));
Try this hope it works.
For checking that the file exist Open a new finder and press Command+Shift+G after that copy and paste the file path and press enter
If the file load then the file exist. File path in your case is " ///var/mobile/Containers/Data/Application/DBC9AFAB-3FD4-4D06-82F5-0577251001A7/Library/Caches/RewardMe-Presentation-at-NVIDIA-Auditorium.pdf "
Related
Inside my app I have a html credits file which I want to load into a webView. The code I'm using works perfectly fine for my main target. However later on I created a second target: the pro-version of the app but for this one the code doesn't work because url is nil.
Both targets are selected on Target Membership for credits.html
I have tried changing the Location of the file in the identity inspector to all possibilities with no success.
I guess the file is somehow saved with a path only the main target can access.
if let urlPath = Bundle.main.path(forResource: "credits", ofType: "html") {
if let url = URL(string: urlPath) {
webView.loadRequest(URLRequest(url: url))
}
}
You need to use Bundle(identifier:) to get a bundle for a specific target. You can learn the Bundle identifier for each specific target by going to the Build settings of your target and looking at the Product Bundle Identifier variable.
if let targetBundle = Bundle(identifier: "yourBundleId"), let filePath = targetBundle.url(forResource: "credits", withExtension: "html") {
webView.loadRequest(URLRequest(url: url))
}
As I understand from Apple documentation, that there is sandbox for each app , so there local storage to the app can save data and files inside it and the other apps can not access its data and files, but I tried to create file by app and retrieved file URL from it and create another app and pass the file URL to it, but the new app open this file successfully, why that happen? the other app must can not be able to open the file according to Apple documentation
In first App:
I download file using Alamofire
let destination: DownloadRequest.DownloadFileDestination =
{
(temporaryURL, response)in
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0]
let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
let tmpURL = documentsURL.appendingPathComponent("wordFile.docx")
return (tmpURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(urlString, to:
destination).responseData { response in
(response.destinationURL?.path)!
//The "response.destinationURL?.path" is: "/Users/user/Library/Developer/CoreSimulator/Devices/A0427AC0-6D5E-4734-A550-73A8224BD54D/data/Containers/Data/Application/4126243E-E166-4311-A950-D8FF49A07991/Documents/wordFile.docx"
})
In second App:
I got the file path from previous App (response.destinationURL?.path)!
and pass it to webView , but the webView show file the successfully
I ran into this bug that prevents my app from displaying PDF using UIDocumentInteractionController or QLPreviewController: https://forums.developer.apple.com/thread/91835
According to the suggestions, the solution is to copy files to documents or tmp folders and load files from there.
However, this does not work for me. Loading the files from .documentDirectory or NSTemporaryDirectory() produces the same error, but now not only on device, but also in simulator.
Edit:
The following code solved the problem for me:
func copyFiles(fileName: String) -> URL {
let filemgr = FileManager.default
filemgr.delegate = self
let tempDocsFolder = URL.init(fileURLWithPath: NSTemporaryDirectory()).path
// my fileName is in format "file.pdf"
let fileSplit = fileName.components(separatedBy: ".")
let filePath = Bundle.main.path(forResource: fileSplit[0], ofType: fileSplit[1])
let destPath = "\(tempDocsFolder)/\(fileName)"
do {
try? filemgr.copyItem(atPath: filePath!, toPath: destPath)
}
return URL.init(fileURLWithPath: destPath)
}
Then returned URL is then feeded to the UIDocumentInteractionController. The reason it didn't work for me before was because I tried to copy my files to /tmp/documents/, but the files must be copied to the root of the tmp folder: /tmp/ (I have no idea why).
Check for case-sensitivity of resources(File names).
Add any screen shots of the code.
Surprisingly i have not been able to find an answer to this despite many searching.
I have a file that has been cached, i can retrieve the cache directory and file name which i believe to be the url?
this is code to get the file and then play it
let cachedFilePath = getCachedFilePath.result as! String
audioFileURL = cachedFilePath
self.audioPlayerItem = AVPlayerItem(url: NSURL(string: audioFileURL!) as! URL)
This is what is returned if i print audioFileURL:
/Users/Genie/Library/Developer/CoreSimulator/Devices/A2FB00CE-B018-4FDF-9635-35FD6678DF8D/data/Containers/Data/Application/E18B5E89-B973-4277-AA5C-1378C69D80CD/Library/Caches/Parse/PFFileCache/7e4a8fc0eb37b655b859e4959f986da2_The%20Name%20of%20the%20Wind%2017-92.mp3
The player loads but it never plays, It just sits there spinning. so i am wondering if i am passing the url to the player correctly?
i read on one thread to use file:// before it but that didn't work.
In the place where you're creating an NSURL, you have the replace the argument name string to fileURLWithPath
Change
NSURL(string: audioFileURL!)
to
NSURL(fileURLWithPath: audioFileURL!)
Also, why are you casting NSURL to URL ? Why not use URL directly?
try like this,i hope it will work for you.
self.audioPlayerItem = AVPlayerItem(url:NSURL(fileURLWithPath: audioFileURL!) as! URL)
AVPlayerItem(url:URL(fileURLWithPath: Bundle.main.path(forResource: "filename", ofType: "mp4")!))
And make sure your video is added to the bundle:
project root > your Target > Build Phases > Copy Bundle Resources.
I previously used below code to open up a dummy PDF, just for testing purpose, and now for the actual project i have to load a pdf based on the table cell clicked that will give the exact path of the pdf at some local path.
self.termsPath = NSBundle.mainBundle().pathForResource("DemoForm", ofType: "pdf")!
let url = NSURL(fileURLWithPath: termsPath!)
let pdfRequest = NSURLRequest(URL: url!)
pdfWebView.loadRequest(pdfRequest)
pdfWebView.scalesPageToFit = true
Now i have thisvariable docPath that will be passed from the prepareforSegue when table cell is clicked, i tried to use this variable in place of DemoForm but it gives below error
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0
Where i'm i going wrong here? Can anyone help me.Thanks
EDIT: By the way my docPath refers to something like ".../.../.../.../Referral.pdf"