I wrote some code to download a URL to my disk, using an NSURLRequest and an NSURLDownload. The download starts fine (at least, it sends downloadDidBegin:(NSURLDownload *)download), but then it sends
-(void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
My implementation of the function is as follows:
{
[download release];
NSLog(#"Download failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
The logged error is
2011-07-17 01:48:43.194 URLDownloader[28031:903] Download failed! Error - The operation couldn’t be completed. (NSURLErrorDomain error -3001.) [the URL in the NSURLRequest]
From what I can tell, this is the NSURLErrorCannotOpenFile error. I cannot find any explanation of why this error occurs, other then "Returned when NSURLDownload was unable to open the downloaded file on disk." from the documentation. So, why is this happening?
Fact that might be helpful(?): NSURLErrorCannotOpenFile is enumerated as kCFURLErrorCannotOpenFile. I have no idea what this means either....
Alright, so I was just being foolish. I had created a path using NSURL's absoluteString method instead of the path method. This was just bad coding on my part...
Related
I'm creating a watch application which records an audio and play that audio. The recorded audio is saved in the Apps Groups and I used the following code for playing the audio.
- (void)playAudio
{
// Playing the audio from the url using the default controller
[self presentMediaPlayerControllerWithURL:self.audioFileURL options:#{WKMediaPlayerControllerOptionsAutoplayKey : #YES} completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * _Nullable error) {
NSLog(#"Error = %#",error);
}];
}
Unfortunately I am getting the following error and the player controller get dismissed immediately.
Error Domain=com.apple.watchkit.errors Code=4 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (1), NSUnderlyingError=0x16daa810 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}, NSLocalizedDescription=The operation could not be completed}.
Then I downloaded a sample project (WatchKitAudioRecorder Sample code) from Apple WatchOS Developer library but they are also having the same issue. I don't know why this is not working even in Apple Sample code :(
Finally I found that the data is not writing to the url path. Also should be written in the following path: Library/Caches/filename.extension
Here is a sample code, which may help someone.
- (void)writeAudioToAppGroupsWithData:(NSData *)audioData
{
// Writing the audio data to the App Groups
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];
containerURL = [containerURL URLByAppendingPathComponent:[NSString stringWithFormat:DIRECTORY_PATH]];
[audioData writeToURL:containerURL atomically:YES];
}
In this case make sure that your DIRECTORY_PATH is Library/Caches/filename.extension. Eg: Library/Caches/Audio.mp3
So I have a user drop some files into my application. Later I'm handling these files using NSFileHandle like this:
NSFileHandle *inputFile = [NSFileHandle fileHandleForReadingFromURL:path error:&error];
if(inputFile == nil) {
NSLog(#"Error (2): %#",error);
return;
}
On my Mac it works, but on other devices this error occurs:
Error Domain=NSCocoaErrorDomain Code=3 "The operation couldn't be completed. (Cocoa error 3.)" ...
I have looked into FoundationErrors.h but there is no error code 3 and i couldn't find anything useful using google.
What is causing this error (the description doesn't give much detail) and how can i prevent it?
I'm trying to download a file in my cocoa app as follows :
NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadURL]];
NSURLDownload *downloader = [[NSURLDownload alloc] initWithRequest:downloadRequest delegate:self];
where downloadURL is something like "https://www.abcd.com/path/to/file/abc.zip"
But I keep getting the following NSError in the (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error method :
Error Domain=NSURLErrorDomain Code=-3001 "The operation couldn’t be
completed. (NSURLErrorDomain error -3001.)" UserInfo=0x618000060a80
{NSErrorFailingURLStringKey=https://www.abcd.com/path/to/file/abc.zip,
NSUnderlyingError=0x618000242010 "The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error -3001.)",
NSErrorFailingURLKey=https://www.abcd.com/path/to/file/abc.zip}
What am I doing wrong?
Thanks for your help!
The error you are receiving is kCFURLErrorCannotOpenFile. You will probably need to capture and deal with the .zip file manually.
I solved the issue.
The NSURLDownload object's setDestination method needs the not just the directory path (/xx/yy/zz), but also the filename (/xx/yy/zz/abc.zip).
I get a weird issue while saving a file with writeToFile:options:error: In the first case I get the following issue:
writeToFile failed with error Error Domain=NSCocoaErrorDomain Code=4 "The file “preferences.plist” doesn’t exist." UserInfo=0xa12c30 {NSFilePath=file:/Users/patrick/Desktop/Untitled.fef/preferences.plist, NSUnderlyingError=0xa0d130 "The operation couldn’t be completed. No such file or directory"}
But the file actually exist in that location. The file is stored in the app document file package.
CODE: (Subclass of NSDocument)
NSString *prefFile = [[[self fileURL] absoluteString] stringByAppendingPathComponent:#"preferences.plist"];
NSError *error;
BOOL succes = [[NSKeyedArchiver archivedDataWithRootObject:documentPreferences] writeToFile:prefFile options:0 error:&error];
if (!succes) {
NSLog(#"writeToFile failed with error %#", error);
}
Make sure when using any of the ...toFilemethods of various classes that you use a real path and not by mistake a file URL. You can tell these apart by looking at the path. If the path starts with file://it can't be used. If it starts with a slash / it is a regular file path. Still you might use NSFileManager to check if the file exists.
As in your code instead of calling absoluteString on self.fileURL call path.
I am very new to the iPhone development and although I spent hours in searching for the right answer I only got more confused at the end.
Here is my question - I'm making a simple iPhone application which has its own database/consisting of only one simple table and being stored in my projects folder along with all the .m and .h files/ and which uses the managed object model presented by core data.
I think I did everything the way it should be done but my program ends with the following "Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)
Actually here is the whole output:
2011-12-07 18:50:50.009 weatherForecast[5368:207] CoreData: error: (1) I/O error for
database at /Users/eln/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
SQLite error code:1, 'no such table: Z_METADATA'
2011-12-07 18:50:50.012 weatherForecast[5368:207] Unresolved error
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be
completed. (Cocoa error 256.)" UserInfo=0x6a33e40
{NSUnderlyingException=I/O error for database at
/Users/toma.popov/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
SQLite error code:1, 'no such table: Z_METADATA',
NSSQLiteErrorDomain=1},
{
NSSQLiteErrorDomain = 1;
NSUnderlyingException = "I/O error for database at /Users/eln/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
**SQLite error code:1, 'no such table: Z_METADATA'";
my program stops right in this get method:
**
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"weatherForecast.sqlite"];
NSLog(#"DATABASE IS LOCATED AT: %#",(NSString *)[self applicationDocumentsDirectory]);
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
//right here my program aborts
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
**
I will be much grateful if someone gives me advise or at least suggestion on what might the problem be.Thank you in advance!
Core Data needs to manage/create its own database, it can't use one that you've created yourself. Is weatherForecast.sqlite such a file?