Save file to custom folder - swift

In AppDelegate I create hidden folder in .documents if it doesn't exist:
let fileManager = FileManager.default
let path = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let audioKitFilesFolder = path.appendingPathComponent(".AudioKitFilesFolder")
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: audioKitFilesFolder.absoluteString, isDirectory:&isDir) {
if isDir.boolValue {
print("file exists and is a directory")
} else {
print("file exists and is not a directory")
}
} else {
do {
try fileManager.createDirectory(at: audioKitFilesFolder, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Can't Create Folder \(error)")
}
}
In my Networking API I have func that save file from web to .documents. But I need save this file to the my hidden Folder. How I can get path for this folder for my copyItem method?
Newtwork API func:
func downloadFile(id: Int, url: URL, fileName: String) {
var request = URLRequest(url: url)
URLSession.shared.downloadTask(with: url, completionHandler: { location, response, error in
guard let location = location, error == nil else { return }
do {
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent(fileName)
try fileManager.copyItem(at: location, to: fileURL)
try self.router.configureParameters(bodyParameters: ["uuid": UserDefaultsHelper.uuid], urlParameters: nil, request: &request)
} catch {
print(error)
}
}).resume()
URLSession.shared.dataTask(with: request) { data, response, error in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
}

What happens if you change
do {
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent(fileName)
try fileManager.copyItem(at: location, to: fileURL)
try self.router.configureParameters(bodyParameters: ["uuid": UserDefaultsHelper.uuid], urlParameters: nil, request: &request)
} catch {
to
do {
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let audioKitFilesFolder = documentsURL.appendingPathComponent(".AudioKitFilesFolder")
let fileURL = audioKitFilesFolder.appendingPathComponent(fileName)
try fileManager.copyItem(at: location, to: fileURL)
try self.router.configureParameters(bodyParameters: ["uuid": UserDefaultsHelper.uuid], urlParameters: nil, request: &request)
} catch {
and perhaps remove the . from . AudioKitFilesFolder in all places

Related

Cocoa: How do I change the download destination? Swift

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?

how to write data in the local json file iOS Swift?

if let path = Bundle.main.path(forResource: "domaines", ofType: "json") {
if JSONSerialization.isValidJSONObject(dict){
do{
let rawData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
try rawData.write(to: URL(fileURLWithPath: path))
}catch{
}
}else{
}
}else{
print("file not present")
}
This is the code used by me but I'm not able to save the data to the local JSON file.
well, I created a class by which you can create a JSON file then add data to bypassing an array to it and you can update it by replacing data with the new data. check out my code and comment if you don’t understand anything.
it's easy to understand,
it's easy to implement.
// offlineJsonFileManager.swift
// BuzCard
//
// Created by ap00724 on 06/02/20.
// Copyright © 2020 ap00724. All rights reserved.
//
import Foundation
import UIKit
class offlineJsonFileManager: NSObject {
static let sharedManager = offlineJsonFileManager()
func saveToJsonFile(fileName:String,dict:[[String:Any]]) {
guard let documentDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileUrl = documentDirectoryUrl.appendingPathComponent("\(fileName).json")
let personArray = dict
// Transform array into data and save it into file
do {
let data = try JSONSerialization.data(withJSONObject: personArray, options: [])
try data.write(to: fileUrl, options: [])
} catch {
print(error)
}
}
func retrieveFromJsonFile(fileName:String,completion:(Bool,[[String:Any]])->()) {
// Get the url of Persons.json in document directory
guard let documentsDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
completion(false,[["error":"file does not exist."]]);return }
let fileUrl = documentsDirectoryUrl.appendingPathComponent("\(fileName).json")
// Read data from .json file and transform data into an array
do {
let data = try Data(contentsOf: fileUrl, options: [])
guard let personArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:Any]] else { return }
completion(true,personArray)
} catch {
print(error)
completion(false,[["error":"\(error)"]])
}
}
}
private func saveDomaines(json: [[String: Any]]) {
if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
let fileURL = documentsDirectory.appendingPathComponent("domaines.json")
print("File exists")
writeFile(fileURL: fileURL, json: json)
} else {
print("Shouldn't reach here")
}
}
private func writeFile(fileURL: URL, json: [[String: Any]]) {
do {
if let jsonData = try JSONSerialization.data(withJSONObject: json, options: .init(rawValue: 0)) as? Data {
try jsonData.write(to: fileURL)
}
} catch {
print(error.localizedDescription)
}
}

How can i find filepath of a file that i downloaded with Alamofire from Firebase Storage

I have some e-pubs on firebase storage.I am downloading specific e-pub with Alamofire downloadURL function but i need e-pubs filePath to open it with FolioReader.
I already tried destinationURL.path but it didn't work with FolioReader
downloadBookRef.downloadURL { (url, error) in
//Done
let destination: DownloadRequest.DownloadFileDestination = {_, _ in
let documentsURL: NSURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
print("documentURL", documentsURL)
let fileURL = documentsURL.appendingPathComponent("kitap\(item).epub")
print("fileURL", fileURL ?? "")
return (fileURL!, [.removePreviousFile, .createIntermediateDirectories])
}
download(url!, to: destination).response { response in
if response.error == nil, let filePath = response.destinationURL?.path {
print("filePath", filePath)
}
}
}

Swift FileManager - no such file

I am trying to upload videos. I get true for fileExists but in the logs I see -
Body file is unreachable:
/private/var/mobile/Containers/Data/Application/BE71B534-5051-4552-8491-30E1FE34E128/Documents/upload/306A4291-5E5A-467E-B2F7-8FFCA5BFFC1D1520419887-676644831.mp4
Error Domain=NSCocoaErrorDomain Code=260 "The file
“306A4291-5E5A-467E-B2F7-8FFCA5BFFC1D1520419887-676644831.mp4”
couldn’t be opened because there is no such file."
let dir = "/upload/"
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let path = documentsUrl.appendingPathComponent(dir)
do {
if let urlArray = try? FileManager.default.contentsOfDirectory(at: path,
includingPropertiesForKeys: [.contentModificationDateKey],
options:.skipsHiddenFiles) {
let mp4Files = urlArray.filter{ $0.pathExtension == "mp4" }
for videoFile in urlArray {
let fileExists = FileManager().fileExists(atPath: videoFile.path)
if fileExists {
let url = URL.init(fileURLWithPath: videoFile.path)
self.uploadVideo(url: url)
}
}
}
} catch {
print(error.localizedDescription)
}
To save the video I use -
let urlData = NSData(contentsOf: videoUrl)
if((urlData) != nil) {
DispatchQueue.main.async(execute: { () -> Void in
urlData?.write(toFile: path, atomically: true)
})
}
Try this one,
create Directory using this code
var nextItemDirUrl : URL!
func createDownloadSongDirIfNotForNextItem() {
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let logsPath = documentsDirectoryURL.appendingPathComponent("upload")
nextItemDirUrl = logsPath
do {
try FileManager.default.createDirectory(atPath: logsPath.path, withIntermediateDirectories: true, attributes: nil)
print("Directory created at:",logsPath)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
}
After save your file using nextItemDirUrl in DocumentDirectory
let destinationUrl = nextItemDirUrl.appendingPathComponent("yourView.mp4")
yourVideoData.write(to: destinationUrl, atomically: true)
I managed to fix this by doing -
let urlData = NSData(contentsOf: videoFile)
if((urlData) != nil) {
let lastPath = "/videos/" + "temp.mp4"
let path = documentDirectory.appending(lastPath)
urlData?.write(toFile: path, atomically: true)
let fileURL = URL.init(fileURLWithPath: path)
self.uploadVideo(url: url)
}

Trying to clear document folder Swift

I use following method to add file (download) to document directory:
static func downloadFileWithLink(linkString : String){
// Create destination URL
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile")
//Create URL to the source file you want to download
let fileURL = URL(string: linkString)
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.copyItem(at: tempLocalUrl, to: destinationFileUrl)
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
} else {
print("Error took place while downloading a file. Error description: %#", error?.localizedDescription);
}
}
task.resume()
}
It simply download file to document folder in sandbox. I looked for method that delete files in documents folder, and tried following:
static func deleteFiledInDocDirectory(){
let fileManager = FileManager.default
let tempFolderPath = NSTemporaryDirectory()
do {
let filePaths = try fileManager.contentsOfDirectory(atPath: tempFolderPath)
for filePath in filePaths {
try fileManager.removeItem(atPath: tempFolderPath + filePath)
}
} catch {
print("Could not clear temp folder: \(error)")
}
}
However, when i inspect sandbox, downloaded file is still here. How to delete it?
In deleteFiledInDocDirectory() you're using NSTemporaryDirectory instead of the documents directory that you originally saved the file to.
Change tempFolderPath to be set using following:
guard let tempFolderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
return // documents directory not found for some reason
}
The full method:
static func deleteFiledInDocDirectory(){
guard let tempFolderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString else {
return // documents directory not found for some reason
}
let fileManager = FileManager.default
do {
let filePaths = try fileManager.contentsOfDirectory(atPath: tempFolderPath)
for filePath in filePaths {
try fileManager.removeItem(atPath: tempFolderPath + filePath)
}
} catch {
print("Could not clear temp folder: \(error)")
}
}