var domainListPath: String?
var signaturePath: String?
var publicKeyPath: String?
1、从documentDirectory获取文件
let sandBoxPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)
domainListPath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("1.list").path
signaturePath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("2").path
publicKeyPath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("3.key").path
2、如果没有就从bundle把文件复制到documents里面
letfileMgr =FileManager.default
fileMgr.delegate=self
if(!fileMgr.fileExists(atPath:domainListPath!) ||
!fileMgr.fileExists(atPath:signaturePath!) ||
!fileMgr.fileExists(atPath:publicKeyPath!)) {
do{
tryfileMgr.copyItem(
atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/domainip.list").path,
toPath:domainListPath!)
tryfileMgr.copyItem(
atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/signature").path,
toPath:signaturePath!)
tryfileMgr.copyItem(
atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/public.key").path,
toPath:publicKeyPath!)
}catch{
}
}
//MARK FileManagerDelegate methods
funcfileManager(_fileManager:FileManager,shouldCopyItemAtPathsrcPath:String,toPathdstPath:String) ->Bool{
try? fileManager.removeItem(atPath: srcPath)
if!FileManager.default.fileExists(atPath: srcPathasString) {
do{
tryFileManager.default.copyItem(atPath: srcPathasString,toPath: dstPathasString)
}catch {
Log.Debug("func fileManager(_ fileManager: FileManager, shouldCopyItemAtPath srcPath: String, toPath dstPath: String) -> Bool")
}
}
return true
}
//mark DCheckerManager delegate methods
func overwriteConfigurations() {
letfileMgr =FileManager.default
fileMgr.delegate=self
do{
tryfileMgr.copyItem(
atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/domainip.list").path,
toPath:domainListPath!)
tryfileMgr.copyItem(
atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/signature").path,
toPath:signaturePath!)
tryfileMgr.copyItem(
atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/public.key").path,
toPath:publicKeyPath!)
}catch{
}
}
欢迎大家加入QQ群一起交流学习:
464406500