(this is my first post here, I am sorry for any mistakes i have made)
i have code that works perfectly on the iOS simulator but once i run it on a real device it shows online a white screen and this error: com.apple.WebKit.WebContent: 113: Could not find specified service
this is my code for downloading a pdf file from firebase to the device:
#IBAction func downloadButtonPressed (sender: UIButton) {
print("zzz download button pressed")
let directoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) //FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
if directoryPath.count > 0 {
let documentsDirectory = directoryPath[0]
let pathURL = URL(fileURLWithPath: documentsDirectory)
let fileURL = pathURL.appendingPathComponent("\(fixture.id).pdf")
let httpsReference = FIRStorage.storage().reference(forURL: fixture.onlineManual)
print("zzz urlManual: \(fixture.onlineManual)")
let downloadTask = httpsReference.write(toFile: fileURL) {(URL, error) in
if error != nil {
print("downloadError with pdf")
print(error?.localizedDescription)
} else {
print("zzz donwload successful: \(URL!)")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newManual = NSEntityDescription.insertNewObject(forEntityName: "OfflineManuals", into: context)
newManual.setValue(self.fixture.id, forKey: "id")
newManual.setValue("\(URL!)", forKey: "manual")
do {
try context.save()
print("zzz manual saved to device")
} catch {
print("zzz error saving manual to core data")
}
}
}
}
}
this is the code that loads the file from the device and displays it in a WKWebView
func loadRequest() {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
if documentsPath.count > 0 {
let documentsDirectory = documentsPath[0]
let restorePath = documentsDirectory + "/\(manualURL!).pdf"
let url = URL(fileURLWithPath: restorePath)
print("zzz url for local file: \(url)")
let req = URLRequest(url: url)
webView.load(req)
}
}
thank you for reading!
I finally found a way to do this. I still have no clou why my code in the question does not work on real devices but this one does and since i found a lot of people with similar problems desperatly looking for an answer use this:
SWIFT3 XCODE 8 IOS 10
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
var webView: WKWebView!
let sema = DispatchSemaphore( value: 0 )
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
myView.addSubview(webView)
savePDFtoDevice()
loadPDF()
}
the LayoutSubviews is not needed but i had problems without it...
override func viewDidLayoutSubviews() {
print("zzz width: \(self.myView.frame.size.width), height: \(myView.frame.size.height)")
print("zzz width screen: \(self.view.frame.width)")
let frame = CGRect(x: 30.0, y: 30.0, width: (self.myView.frame.width - 60), height: 300)
webView.frame = frame
}
func savePDFtoDevice(){
let myURL = URL(string: "http://www.claypaky.it/media/documents/HR_Mythos_Manual_05.2016_EN.pdf")
let req = URLRequest(url: myURL!)
let session = URLSession(configuration: .default)
session.dataTask(with: req) { (data, response, error) in
if error != nil {
print("zzz error got no session")
} else {
if let pdfData = data {
var url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
url = url.appendingPathComponent("file.pdf")
print("zzz url to save: \(url)")
print("zzz got data: \(pdfData.description)")
try? data?.write(to: url)
print("zzz wrote to file")
self.sema.signal()
}
}
}.resume()
sema.wait()
}
func loadPDF() {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
print("zzz url to load from: \(url)")
do {
let content = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)[0]
print("zzz file: \(content)")
webView.loadFileURL(content, allowingReadAccessTo: content)
print("zzz webView loaded")
} catch {
print("zzz error with getting the contents...")
}
}
}
I tried it in my app with changing URLs and filenames etc. and it does the trick.
Finally!!!!
Related
Given the code below I am successfully downloading my tar.gz file but when I try unzip it I get
Error creating a file file:///Users/xxxxx/Library/Developer/CoreSimulator/Devices/346A7980-8EFB-4ACE-88FC-617C9533E893/data/Containers/Data/Application/22F22B8E-2C15-4C17-B6CD-3777B66D2AAE/Documents/ : unzipFail
Can anyone tell me what I am doing wrong?
import UIKit
import WebKit
import Zip
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create destination URL
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationFileUrl = documentsUrl.appendingPathComponent("data.tar.gz")
//Create URL to the source file you want to download
let fileURL = URL(string: "https:/www.myurl.com/data.tar.gz")
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
do {
try? FileManager.default.removeItem(at: destinationFileUrl)
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
let unzipDirectory = try Zip.quickUnzipFile(destinationFileUrl)
} catch (let writeError) {
print("Error creating a file \(documentsUrl) : \(writeError)")
}
}
} else {
print("Error took place while downloading a file. Error description: %#", error?.localizedDescription);
}
}
task.resume()
}
The problem was that the tar.gz is not supported. I used an alternative library
How to load a usdz model and textures from a remote server using ARKit?
As in the example below:
let myURL = NSURL(string: "https://mywebsite.com/vase.scn")
guard let scene = try? SCNScene(url: myURL! as URL, options: nil) else {
return
}
let node = scene.rootNode.childNode(withName: "vase", recursively: true)
let transform = queryResult.worldTransform
let thirdColumn = transform.columns.3
node!.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)
self.sceneView.scene.rootNode.addChildNode(node!)
I found solution as in the below:
Download model and save
func downloadModel() {
guard let url = URL(url: originalURL) else { return }
let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
extension ARViewController: URLSessionDownloadDelegate {
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { print("locationUrl:", location.path)
// create destination URL with the original file name
guard let url = downloadTask.originalRequest?.url else { return }
let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
// delete original copy
try? FileManager.default.removeItem(at: destinationURL)
// copy from temp to Document
do {
try FileManager.default.copyItem(at: location, to: destinationURL)
self.originalURL = destinationURL
print("originalURL:", originalURL!.path)
} catch let error {
print("Copy Error: \(error.localizedDescription)")
}
}
}
Load usdz model and textures
func addItem(queryResult: ARRaycastResult) {
let url = URL(string: originalURL!.path)
let mdlAsset = MDLAsset(url: url!)
mdlAsset.loadTextures()
let scene = SCNScene(mdlAsset: mdlAsset)
let node = scene.rootNode.childNode(withName: "model", recursively: true)
let transform = queryResult.worldTransform
let thirdColumn = transform.columns.3
node!.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)
self.sceneView.scene.rootNode.addChildNode(node!)
}
I’m trying to download a file from a URL, I managed to do that however, the it will download to ~Libray directory. How do I change the directory to the downloads folder? or out of the library directory.
Here is my file downloader…
import Foundation
class FileDownloader {
static func loadFileSync(url: URL, completion: #escaping (String?, Error?) -> Void)
{
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).first!
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
if FileManager().fileExists(atPath: destinationUrl.path)
{
print("File already exists [\(destinationUrl.path)]")
completion(destinationUrl.path, nil)
}
else if let dataFromURL = NSData(contentsOf: url)
{
if dataFromURL.write(to: destinationUrl, atomically: true)
{
print("file saved [\(destinationUrl.path)]")
completion(destinationUrl.path, nil)
}
else
{
print("error saving file")
let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
completion(destinationUrl.path, error)
}
}
else
{
let error = NSError(domain:"Error downloading file", code:1002, userInfo:nil)
completion(destinationUrl.path, error)
}
}
static func loadFileAsync(url: URL, completion: #escaping (String?, Error?) -> Void)
{
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
if FileManager().fileExists(atPath: destinationUrl.path)
{
print("File already exists [\(destinationUrl.path)]")
completion(destinationUrl.path, nil)
}
else
{
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
var request = URLRequest(url: url)
request.httpMethod = "GET"
let task = session.dataTask(with: request, completionHandler:
{
data, response, error in
if error == nil
{
if let response = response as? HTTPURLResponse
{
if response.statusCode == 200
{
if let data = data
{
if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic)
{
completion(destinationUrl.path, error)
}
else
{
completion(destinationUrl.path, error)
}
}
else
{
completion(destinationUrl.path, error)
}
}
}
}
else
{
completion(destinationUrl.path, error)
}
})
task.resume()
}
}
}
I took a look at the code and I think it has to do something with this
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).first!
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
I have tried chinging the code from documentDirectory to desktopDirectory but that still puts it in the library directory. How do I change it from the library directory to the downloads directory?
You are running a sandboxed app. The Appname/Data/... directories are aliases of the real directories.
Also, you should use userDomainMask instead of allDomainsMask to search for the path in user's home directory. The correct directory you are looking for is downloadsDirectory.
let downloadsUrl = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
let destinationUrl = downloadsUrl.appendingPathComponent(url.lastPathComponent)
There is another SO thread about sandboxed directories:
how to get /Users/username/Downloads path in a sandboxed app?
I think something changed within Swift that disabled me from previewing my files. It worked fine previously. If I click on say a PDF file in my app, I see the title of the PDF, but the content of PDF (preview) area does not show.
Below is my code & logs & also the screenshot. If anyone has an idea of where I can fix the issue, any help would be greatly appreciated.
// When file is clicked this method is called
#objc private func handleTapped() {
guard let url = self.file.fileUrl else { return }
if self.file.isDownloaded {
self.showDocumentController(url: self.file.urlInDocumentsDirectory! as NSURL)
return
}
SVProgressHUD.showProgress(0)
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("pig.png")
return (documentsURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(url, to: destination)
.downloadProgress { (download) in
DispatchQueue.main.async() {
SVProgressHUD.showProgress(Float(download.fractionCompleted))
}
}.validate(statusCode: 200..<300)
.response { (response) in
SVProgressHUD.dismiss()
guard response.response?.statusCode == 200 else { return }
let directoryURL = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let pathURL = URL(fileURLWithPath: directoryURL, isDirectory: true)
//pathURL: file:///var/mobile/Containers/Data/Application/6DDCCC30-107C-4613-B63D-18962C3D06D3/Documents/
guard let fileName = response.response?.suggestedFilename else { return }
//fileName: 05_기조강연_RobertMankin_BETTER+OFFICES+GREATER+INNOVATION.pdf
let fileURL = pathURL.appendingPathComponent(fileName)
//fileURL: file:///var/mobile/Containers/Data/Application/6DDCCC30-107C-4613-B63D-18962C3D06D3/Documents/05_%E1%84%80%E1%85%B5%E1%84%8C%E1%85%A9%E1%84%80%E1%85%A1%E1%86%BC%E1%84%8B%E1%85%A7%E1%86%AB_RobertMankin_BETTER+OFFICES+GREATER+INNOVATION.pdf
self.saveFileURL(url: fileURL as NSURL)
self.showDocumentController(url: fileURL as NSURL)
}
}
private func saveFileURL(url: NSURL) {
self.file.urlInDocumentsDirectory = url as URL
let realm = RealmService.defaultRealm
try! realm?.write {
realm?.add(self.file, update: true)
}
self.file = self.file.copyFromRealm()
}
private func showDocumentController(url: NSURL) {
let docController = UIDocumentInteractionController(url: url as URL)
docController.delegate = self
docController.presentPreview(animated: true)
}
// MARK: UIDocumentInteractionControllerDelegate methods
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
if let controller = UIApplication.shared.keyWindow?.topMostViewController() {
return controller
}
return UIViewController()
}
this is how the preview shows
Here Is The Code
import UIKit
import Alamofire
class ViewController: UIViewController, UIWebViewDelegate
{
#IBOutlet weak var WebView: UIWebView!
var NewsURL: String = ""
override func viewDidLoad()
{
super.viewDidLoad()
Self.LoadPdf()
}
func LoadPdf()
{
let url = NSURL (string: "\(http://)") //Your Pdf URL Here
let requestObj = NSURLRequest(URL: url!);
WebView.loadRequest(requestObj)
}
}
I'm new to SWIFT/Programming &
I couldn't find an answer on my question, that's why I'm gonna give it a try here:
HOW Do I download a video (mp4) from an URL and store it within the app**
HOW Do I display the video then in a container**
I've already found this topic:
Swift - Downloading video with downloadTaskWithURL
But in my case, I wouldn't want the video to be safed in the camera-roll. Just within the app.
Thanks for any kind of help/hint !
You can use URLSession's dataTask or downloadTask to download any file from url(if it's downloadble)
Here's the way to use dataTask for downloading:
let videoUrl = "Some video url"
let docsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationUrl = docsUrl.appendingPathComponent("MyFileSaveName.mp4")
if(FileManager().fileExists(atPath: destinationUrl.path)){
print("\n\nfile already exists\n\n")
}
else{
//DispatchQueue.global(qos: .background).async {
var request = URLRequest(url: URL(string: videoUrl)!)
request.httpMethod = "GET"
_ = session.dataTask(with: request, completionHandler: { (data, response, error) in
if(error != nil){
print("\n\nsome error occured\n\n")
return
}
if let response = response as? HTTPURLResponse{
if response.statusCode == 200{
DispatchQueue.main.async {
if let data = data{
if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic){
print("\n\nurl data written\n\n")
}
else{
print("\n\nerror again\n\n")
}
}//end if let data
}//end dispatch main
}//end if let response.status
}
}).resume()
//}//end dispatch global
}//end outer else
Now to play the saved file:
class MyViewController: UIViewController {
override func viewDidLoad() {
let baseUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let assetUrl = baseUrl.appendingPathComponent("MyFileSaveName.mp4")
let url = assetUrl
print(url)
let avAssest = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: avAssest)
let player = AVPlayer(playerItem: playerItem)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true, completion: {
player.play()
})
}
}
However, most sites do not provide a direct dwonloadable link for video. You can get that link by playing the video in a UIWebView and register this following observer to get that link:
NotificationCenter.default.addObserver(self, selector: #selector(videoPlayedInWebView), name: NSNotification.Name(rawValue: "AVPlayerItemBecameCurrentNotification"), object: nil)
#objc func videoPlayedInWebView(aNotification: NSNotification) {
if let playerItem: AVPlayerItem = aNotification.object as? AVPlayerItem{
let asset: AVURLAsset = playerItem.asset as! AVURLAsset
var downloadebaleVideoUrl = asset.url
print(downloadebaleVideoUrl)
}
}
Here "downloadebaleVideoUrl" is the link that will be generated once the video plays in the webview.
If you have any questions, feel free to ask.
Note: This is will work only for sites that have mp4 files. 'HLS' streams won't be downloaded with this method. For that you can refer to this following answer:
https://stackoverflow.com/a/54493233/10758374
Edit: this works only with UIWebView and it won't work with WKWebView.
You need to create a local url, that will be a path in your app's file system and write the video's data into it.
func writeToFile(urlString: String) {
guard let videoUrl = URL(string: urlString) else {
return
}
do {
let videoData = try Data(contentsOf: videoUrl)
let fm = FileManager.default
guard let docUrl = fm.urls(for: .documentDirectory, in: .userDomainMask).first else {
print("Unable to reach the documents folder")
return false
}
let localUrl = docUrl.appendingPathComponent("test.mp4")
try videoData.write(to: localUrl)
} catch {
print("could not save data")
}
}
Keep in mind to always call this function in the background thread.