Last active
March 8, 2018 09:31
-
-
Save madcato/c8b9c3b7f3266191c109f1f04c8e3776 to your computer and use it in GitHub Desktop.
This class allows to copy bundle files to a destination path (documents directory)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// InstallManager.swift | |
// OSFramework | |
// | |
// Created by Daniel Vela on 22/01/2018. | |
// Copyright © 2018 Daniel Vela. All rights reserved. | |
// | |
import Foundation | |
/// This class manages the installation of files from a bundle | |
public class InstallManager { | |
public init() { | |
} | |
public func destinationFileURL(_ fileName: String) -> URL { | |
return getDocumentsDirectory().appendingPathComponent(fileName) | |
} | |
public func getDocumentsDirectory() -> URL { | |
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) | |
return paths[0] | |
} | |
public func saveFileToDocumentsDirectory(fileName: String) { | |
let toUrl = destinationFileURL(fileName) | |
guard let fromUrl = Bundle.main.url(forResource: fileName, withExtension: nil) else { | |
NSLog("File %@ doesn't exists", fileName) | |
return | |
} | |
let fm = FileManager() | |
try? fm.copyItem(at: fromUrl, to: toUrl) | |
} | |
// if let image = UIImage(named: "example.png") { | |
// if let data = UIImageJPEGRepresentation(image, 0.8) { | |
// let filename = getDocumentsDirectory().appendingPathComponent("copy.png") | |
// try? data.write(to: filename) | |
// } | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment