Last active
April 20, 2016 07:25
-
-
Save ProgramAlgo/58178481786309bf9047e861479c211e to your computer and use it in GitHub Desktop.
swift copy file from BundleDirectory to DocumentDirectory
This file contains 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
func copyFile() { | |
let manager = NSFileManager.defaultManager() | |
let bundlepath = NSBundle.mainBundle().resourcePath! + "/temp" | |
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true) | |
let docsDir: AnyObject = dirPaths[0] | |
do { | |
let jsDirPath = docsDir.stringByAppendingPathComponent("js") | |
try NSFileManager.defaultManager().createDirectoryAtPath(jsDirPath, withIntermediateDirectories: true, attributes: nil) | |
let cssDirPath = docsDir.stringByAppendingPathComponent("css") | |
try NSFileManager.defaultManager().createDirectoryAtPath(cssDirPath, withIntermediateDirectories: true, attributes: nil) | |
let imagesDirPath = docsDir.stringByAppendingPathComponent("images") | |
try NSFileManager.defaultManager().createDirectoryAtPath(imagesDirPath, withIntermediateDirectories: true, attributes: nil) | |
let videoDirPath = docsDir.stringByAppendingPathComponent("video") | |
try NSFileManager.defaultManager().createDirectoryAtPath(videoDirPath, withIntermediateDirectories: true, attributes: nil) | |
} catch let error as NSError { | |
dPrint(error.localizedDescription); | |
} catch { | |
} | |
dPrint(dirPaths) | |
if let paths = manager.enumeratorAtPath(bundlepath) { | |
while let file = paths.nextObject() as? String { | |
print(file) | |
// パスの実体が存在するか確認 | |
var isDir : ObjCBool = false | |
if manager.fileExistsAtPath(bundlepath + "/" + file, isDirectory: &isDir) { | |
// ディレクトリかどうか確認して | |
if isDir { | |
print("\(file) is Directory.") | |
} else { | |
print("\(file) is exist.") | |
let dstPath = docsDir.stringByAppendingPathComponent(file) | |
do { | |
try manager.removeItemAtPath(dstPath) | |
try manager.copyItemAtPath(bundlepath + "/" + file, toPath: dstPath ) | |
dPrint("success") | |
} catch let error as NSError { | |
dPrint("Cannot copy file: \(error.localizedDescription)") | |
} catch { | |
} | |
} | |
} else { | |
print("\(file) is not exist.") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment