pix*_*abs 55 iphone xcode objective-c ios afnetworking
我已设置以下代码将文件保存到文档目录:
NSLog(@"Saving File...");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.reddexuk.com/logo.png"]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"logo.png"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
Run Code Online (Sandbox Code Playgroud)
但是,我希望在成功保存后将每个文件添加到UITableView.当点击UITableView中的文件时,我想要一个UIWebView导航到该文件(所有离线).
另外 - 我怎样才能获得文件名和结尾,例如"logo.png"而不是http://www.reddexuk.com/logo.png?
我怎样才能做到这一点?
syc*_*efx 143
这是我用来获取目录内容的方法.
-(NSArray *)listFileAtPath:(NSString *)path
{
//-----> LIST ALL FILES <-----//
NSLog(@"LISTING ALL FILES FOUND");
int count;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for (count = 0; count < (int)[directoryContent count]; count++)
{
NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
}
return directoryContent;
}
Run Code Online (Sandbox Code Playgroud)
小智 21
-(NSArray *)findFiles:(NSString *)extension{
NSMutableArray *matches = [[NSMutableArray alloc]init];
NSFileManager *fManager = [NSFileManager defaultManager];
NSString *item;
NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil];
// >>> this section here adds all files with the chosen extension to an array
for (item in contents){
if ([[item pathExtension] isEqualToString:extension]) {
[matches addObject:item];
}
}
return matches; }
Run Code Online (Sandbox Code Playgroud)
上面的例子非常明显.我希望它能回答你的第二个问题.
con*_*are 11
获取目录的内容
- (NSArray *)ls {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
NSLog(@"%@", documentsDirectory);
return directoryContent;
}
Run Code Online (Sandbox Code Playgroud)
要获取最后一个路径组件,
[[path pathComponents] lastObject]
Run Code Online (Sandbox Code Playgroud)
谢谢Alex,
这是Swift版本
func listFilesOnDevice() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = paths[0] as! String
let manager = NSFileManager.defaultManager()
if let allItems = manager.contentsOfDirectoryAtPath(documentDirectory, error: nil) {
println(allItems)
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个古老的问题,但这是一个好问题,并且在iOS沙盒发布后情况有所变化。
应用程序中所有可读/可写文件夹的路径现在将包含一个哈希,Apple保留随时更改该路径的权利。当然,它将在每次启动应用程序时更改。
您需要获取所需文件夹的路径,并且无法像过去那样对它进行硬编码。
您需要文件目录,并在返回数组中,它位于位置0。然后从那里,使用该值提供给NSFileManager以获取目录内容。
下面的代码在iOS 7和8下工作,以返回documents目录中的内容数组。您可能要根据自己的喜好对其进行排序。
+ (NSArray *)dumpDocumentsDirectoryContents {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSError *error;
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
NSLog(@"%@", directoryContents);
return directoryContents;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
68918 次 |
最近记录: |