Iphone file I/O operation and file structure
I want to write a iphone SDK program t开发者_高级运维o download and upload files from server. Server application is written in php. But I m confusing following points in client application.
1) How can I download the file from server 2) How can I store it in iphone (accessing iphone file system) 3) How can I display all file in a directory to the users.
Can anybody help me ?
Create an NSURL
NSURL * theURL = [NSURL URLWithString: @"http://foo.bar/whatever.php"];
Fetch the data with NSData
NSData * theData = [NSData dataWithContentsOfURL: theURL];
Save it to a file with the same NSData object
NSString * theFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString * theFileName = [theFolder stringByAppendingPathComponent:@"MyFile.txt"]
[theData writeToFile:theFileName atomically:true];
Use NSFileManager to list the files in the folder
NSError *error = nil;
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray * filesInFolderStringArray = [fileManager contentsOfDirectoryAtPath:theFolder error:error];
This will give you an array of NSStrings with the names of the files in the folder.
精彩评论