因為各種需求,需要擷取的路徑不一致,故寫下這文章,以作備忘。
- (NSString *)getDocumentPath{ NSString *path = nil; // Method 1 NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); path = [pathsArray objectAtIndex:0]; // Method 2// path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; return path;}- (NSString *)getAppPath{ NSString *path = nil; path = NSHomeDirectory(); return path;}
// tmp 目錄我們可以在裡面寫入一些程式運行時需要用得資料,裡面寫入得資料在程式退出後會沒有。- (NSString *)getTmpPath{ NSString *path = nil; path = NSTemporaryDirectory(); return path;}- (NSString *)getAppPackagePath{ NSString *path = nil; // Method 1 path = [[NSBundle mainBundle] resourcePath]; // Method 2 return path;}- (NSString *)getFilePath{ NSString *path = nil; NSFileManager *fileManager = [NSFileManager defaultManager];NSURL *pathURL = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL]; path = [pathURL path]; return path;}