//
// DirectoryViewController.swift
// Cuanbo
// Created by 开发 on 17/6/8.
// Copyright © 2017年 huangtao. All rights reserved.
//
import UIKit
class DirectoryViewController:UIViewController {
@IBOutlet weak var msg: UITextView!
let appDir = AppDirectory();
override func viewDidLoad() {
super.viewDidLoad()
// 显示目录信息
msg.text =appDir.getAppDirectoryState();
print(msg.text);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// DiretoryHandle.swift
//
import Foundation
class AppDirectory {
func getAppDirectoryState() ->String{
var info = "";
info = info.appendStr("configDir:\(configDir)").appendStr("目录存在:\(FileHandle.isFile(configDir))").appendEnter();
info = info.appendStr("getAppDirectroy:\(getAppDirectroy())").appendStr("目录存在:\(FileHandle.isFile(getAppDirectroy()))").appendEnter();
info = info.appendStr("getHomeDirectory:\(getHomeDirectory())").appendStr("目录存在:\(FileHandle.isFile(getHomeDirectory()))").appendEnter();
info = info.appendStr("getTempDirectroy:\(getTempDirectroy())").appendStr("目录存在:\(FileHandle.isFile(getTempDirectroy()))").appendEnter();
info = info.appendStr("getDocumentsDirectroy:\(getDocumentsDirectroy())").appendStr("目录存在:\(FileHandle.isFile(getDocumentsDirectroy()))").appendEnter();
info = info.appendStr("getDocumentsDirectroy2:\(getDocumentsDirectroy2())").appendStr("目录存在:\(FileHandle.isFile(getDocumentsDirectroy2()))").appendEnter();
info = info.appendStr("getLibraryDirectory:\(getLibraryDirectory())").appendStr("目录存在:\(FileHandle.isFile(getLibraryDirectory()))").appendEnter();
info = info.appendStr("getCachesDirectroy:\(getCachesDirectroy())").appendStr("目录存在:\(FileHandle.isFile(getCachesDirectroy()))").appendEnter();
info = info.appendStr("getPreferencesDirectroy:\(getPreferencesDirectroy())").appendStr("目录存在:\(FileHandle.isFile(getPreferencesDirectroy()))").appendEnter();
return info;
}
/**
HomeDirectory
- returns: 获取沙盒主目录路径
*/
func getHomeDirectory() ->String {
return NSHomeDirectory().appendStr("/");
}
/**
AppDirectroy
- returns:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。
*/
func getAppDirectroy() ->String {
returnNSBundle.mainBundle().bundlePath;
}
/**
TempDirectroy
returns:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息
*/
func getTempDirectroy() ->String {
returnNSTemporaryDirectory().appendStr("/");
}
/**
- DocumentsDirectroy
returns:您应该将所有的应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该定期备份的信息。
*/
func getDocumentsDirectroy() ->String {
return NSHomeDirectory() +"/Documents/";
}
/**
DocumentsDirectroy2
*/
func getDocumentsDirectroy2() ->String {
let paths =NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true);
return paths[0].appendStr("/");
}
/**
LibraryDirectory
- returns: 应用程序专用目录
*/
func getLibraryDirectory() ->String {
let paths =NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory,true);
return paths[0].appendStr("/");
}
/**
Library下的Caches
returns:用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。
*/
func getCachesDirectroy() ->String {
let paths =NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory,0)"> Library下的Preferences
returns:包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好.
*/
func getPreferencesDirectroy() ->String {
let paths =NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.PreferencePanesDirectory,true);
return paths[0].appendStr("/");
}
}
效果图: