So i download an .mp4 file using
urlData * =[NSData dataWithContentsOfUrl:filePath]
then save it using NSFileManager
[urlData writeToFile:filePath atomically:YES];
this saves the file in the NSFileManager directory just fine I can go into iTunes and see it, open and play the video so I know that part is working.
Here is where it breaks for me. I want to open and play these files, so i use like i use
[AVURLAsset URLAssetWithURL:filePath options:nil];
for some reason this always goes to a state of 'AVPlayerStatusFailed'. When I put a break point in and inspect the error I get 'unknown error'
Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSLocalizedDescription=unknown error, NSUnderlyingError=0x1382f9630 {Error Domain=NSOSStatusErrorDomain Code=-12935 "(null)"}}
what am i missing here? or how can i get a better error than 'unknown error'
ok so as it turns out here is how i should have built the NSURL
[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:filePath] options:nil];
Related
I'm running into problems reading jpeg files from the file system and displaying the image in an NSImage. Here's a snippet of code:
NSError *myError;
NSString *path = #"file:/Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url
options:0
error:&myError];
_photoImageView.image = [[NSImage alloc] initWithData:data];
Running this code generates the following error upon calling dataWithContentsOfURL:
Error Domain=NSCocoaErrorDomain Code=260 "The file “1915brsts880804of.jpg” couldn’t be opened because there is no such file."
And, additionally:
{Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}
The file does exist, and if I copy the path and paste it into a web browser, the jpeg image is displayed as expected.
I'm guessing this is some kind of permission problem that exists to prevent applications from accessing the file system directly? I had a similar problem when attempting to open files selected from an Open Panel, which turned out to be a problem with running Open/Save Panels from sandboxes, so I turned off sandboxing to get that aspect of my application working.
Does this ring a bell for anyone? I'm kind of baffled...
If you want to create an NSURL using fileURLWithPath then you need to provide a path, not a URL. Remove the use of file:.
NSString *path = #"/Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL fileURLWithPath:path];
Or you can fix the file URL and use NSURL URLWithString. Use file:// before the absolute file path so you have 3 /:
NSString *fileURLString = #"file:///Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL URLWithString:fileURLString];
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 have this code here which sometimes works. I make sure that I am connected to the internet before attempting this Get from Twitter, and I am pretty sure it isn't the problem.
...
NSString *twitterURLString = [NSString stringWithFormat:#"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2"];NSURL *url = [NSURL URLWithString:twitterURLString];
NSError *error = nil;
NSData *dataFromURL = [NSData dataWithContentsOfURL:url
options:0
error:&error];
NSLog(#"%#", error);
...
This returns the following :
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x4f6520 {NSURL=https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2}
I'm sure it has to do with the url encoding or something since the actual link works well. Also, it actually works sometime, with the same internet connexion (which I repeat is tested and works fine). Any ideas would be greatly appreciated!
I'm trying to copy a file to iCloud using the follow code:
NSError *error;
//My Image Source file
NSURL *sourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"_219" ofType:#"jpg"]isDirectory:NO];
NSLog(#"source: %#", sourceURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
//Discovering iCloud URL
NSURL *iCloudDocumentsURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:#"Documents"];
NSLog(#"iCloudDocumentsURL: %#", iCloudDocumentsURL);
//Setting destination URL
NSURL *destinationURL = [iCloudDocumentsURL URLByAppendingPathComponent:#"image.jpg"];
NSLog(#"destination: %#", destinationURL);
//Copy file to iCloud
BOOL success = [fileManager setUbiquitous:YES itemAtURL:sourceURL destinationURL:destinationURL error:&error];
if (!success) {
#throw [NSException exceptionWithName:[error localizedDescription] reason:[error localizedFailureReason] userInfo:nil];
}
When I run this code I have the following result:
2012-12-31 15:15:30.888 iCloudTest[10614:907] source: file://localhost/var/mobile/Applications/B2202406-BEB8-41B6-A3C4-2327EFB85E54/iCloudTest.app/_219.jpg
2012-12-31 15:15:31.108 iCloudTest[10614:907] iCloudDocumentsURL: file://localhost/private/var/mobile/Library/Mobile%20Documents/xxxxxxxx~com~gazapps~iCloudTest/Documents/
2012-12-31 15:15:31.110 iCloudTest[10614:907] destination: file://localhost/private/var/mobile/Library/Mobile%20Documents/XXXXXXXX~com~gazapps~iCloudTest/Documents/image.jpg
2012-12-31 15:15:31.122 iCloudTest[10614:907] *** Terminating app due to uncaught exception 'The operation couldn’t be completed. (Cocoa error 513.)', reason: '(null)'
*** First throw call stack:
(0x38c312a3 0x32f1897f 0xbb617 0x3a31058d 0x3a350d71 0x3a34cae5 0x3a38e1c9 0xad241 0x3a351ad1 0x3a35165b 0x3a349843 0x3a2f1c39 0x3a2f16cd 0x3a2f111b 0x371295a3 0x371291d3 0x38c06173 0x38c06117 0x38c04f99 0x38b77ebd 0x38b77d49 0x3a34847d 0x3a3452f9 0xacff9 0x3627cb20)
libc++abi.dylib: terminate called throwing an exception
Looking at documentation, Cocoa error 513 means:
NSFileWriteNoPermissionError = 513,
So basically I'm without access to write stuff on iCloud... Looking at https://developer.icloud.com, everything seems to be ok (the documents folder is there).
What I'm doing wrong ?
iCloud has been working intermittently since December 30/31, even though everything may look good on developer.icloud.com. There's a discussion going on at the developer forums here:
https://devforums.apple.com/thread/176739?tstart=0
It's hard to diagnose iCloud issues at this point since it may just be problems on Apple's iCloud server. Some people have noted (on the discussion thread) that iCloud seems to be working better today than prior days so it may be worth testing your app again now.
If I send the message
[[NSFileManager defaultManager] setUbiquitous:NO
itemAtURL:url
destinationURL:iCloudURL
error:&err]
to remove an item from iCloud, it doesn't actually delete the file on the Ubiquitous Container. Is this the expected behaviour?
The method returns NO and the error object contains
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be
completed. (Cocoa error 512.)"
UserInfo=0x20870970
{NSURL=file://localhost/var/mobile/Applications/168EE8CD-4CDF-49BE-AD88-1DC7DD9CF25F/Documents/test.txt,
NSUnderlyingError=0x20863a00 "The operation couldn’t be completed.
(LibrarianErrorDomain error 2 - Cannot disable syncing on a unsynced
item.)"}
The error is pretty clear. You're trying to delete an item from iCloud that's not in iCloud. When you want to delete an item from iCloud using setUbiquitous:..., the item URL (itemAtURL:) should be the iCloud URL. The destination URL can be something local (but is ignored if ubiquitous is set to NO).
When specifying the "setubiquitous" parameter to "no", your destinationURL needs to be the local url, not the iCloud one. You have your URLs switched
To delete an item on iCloud, you can try this code:
NSError *err;
NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateWritingItemAtURL:_url
options:NSFileCoordinatorWritingForDeleting
error:&err
byAccessor:^(NSURL* writingURL) {
NSFileManager* fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtURL:writingURL error:nil];
}];
[fileCoordinator autorelease];
Good luck!