NSFileManager can only handle local paths. It won't do anything useful if you give it a URL.
copyItemAtPath:toPath:error:
takes an error parameter. Use it, like this:
NSError *error;
if (![fileManager copyItemAtPath:urlText toPath:filePath error:&error]) {
NSLog(@"Error %@", error);
}
You would then get this error:
Error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be
completed. (Cocoa error 260.)" UserInfo=0x9a83c00 {NSFilePath=http://www.abc.com/text.txt,
NSUnderlyingError=0x9a83b80 "The operation couldn’t be completed.
No such file or directory"}
It can't read the file at http://www.abc.com/text.txt
, because it is not a valid path.
as Sunny Shah stated without explanation you have to fetch the object at the URL first:
NSString *urlText = @"http://www.abc.com/text.txt";
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSURL *url = [NSURL URLWithString:urlText];
NSError *error;
NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];
if (!data) { // check if download has failed
NSLog(@"Error fetching file %@", error);
}
else {
// successful download
if (![data writeToFile:filePath options:NSDataWritingAtomic error:&error]) { // check if writing failed
NSLog(@"Error writing file %@", error);
}
else {
NSLog(@"File saved.");
}
}
}
Always check for errors!