Incorrect URL for AVURLAsset - ios

I am preparing the URL for one method of AVURLAsset:
+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options
My URL belongs to a file which is written to the document path of my app. The result of NSLog the path is:
/var/mobile/Containers/Data/Application/E0E64E72-9A47-4E62-B9C6-818AB8BF3F4C/Documents/audio.wav
After got the NSString *path, I tried to convert NSString to NSURL by [NSURL URLWithString:path] and [NSURL fileURLWithPath:path], but both of them are incorrect for URLAssetWithURL.
Error of URLWithString is:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could
not be completed" UserInfo=0x170269f80 {NSUnderlyingError=0x17405f440
"The operation couldn’t be completed. (OSStatus error -12780.)",
NSLocalizedFailureReason=An unknown error occurred (-12780),
NSLocalizedDescription=The operation could not be completed}
And fileURLWithPath crashed the app.
I know url = [[NSBundle mainBundle] URLForResource:fileURL withExtension:#"wav"] will fit the situation. But I want to get the url of a local file to fit URLAssetWithURL as well. Any idea? Thanks.

This way of getting a file from the documents should work:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths.firstObject stringByAppendingPathComponent:#"file"] stringByAppendingPathExtension:#"wav"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL *url = [NSURL fileURLWithPath:path];
}

Related

Load rtf and pdf using UIWebview from local

I knew this question is already asked here. But I am facing some issue which is not getting resolved.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *fileCacheDirPath = [docsPath stringByAppendingPathComponent:kATTACHMENTS_FOLDER];
NSString *filePath = [fileCacheDirPath stringByAppendingPathComponent:_fileItemDataModel.name];
NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO];
Error:
url :
file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B990D3-4311-B580-D2E00E75/Documents/AttachmentsFolder/Scrum_Methodology.pdf
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo={NSUnderlyingError=0x60800084d950 {Error
Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed out."
UserInfo={NSErrorFailingURLStringKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
NSErrorFailingURLKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4, NSLocalizedDescription=The request timed out.}},
NSErrorFailingURLStringKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
NSErrorFailingURLKey=file:///Users/Sanoj/Library/Developer/CoreSimulator/Devices/722CC1FA-6542-4E14-9CC5-24F96EE305D9/data/Containers/Data/Application/B992A9C0-A0D3-4311-B580-D2E0049CFE75/Documents/AttachmentsFolder/Scrum_Methodology.pdf,
_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
it worked with following code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *fileCacheDirPath = [docsPath stringByAppendingPathComponent:kATTACHMENTS_FOLDER];
NSString *filePath = [fileCacheDirPath stringByAppendingPathComponent:_fileItemDataModel.name];
[self.webView loadData:_fileItemDataModel.data MIMEType:#"application/pdf" textEncodingName:#"" baseURL:[NSURL URLWithString:filePath]];
Please, check

Can't write to document directory

I'm trying to copy a json file from the application bundle to the Document directory, but surprisingly i'm not able to do that. Here's the code:
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array lastObject];
NSString *path = [docDir stringByAppendingPathExtension:#"themes.json"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *themesPath = [[NSBundle mainBundle] pathForResource:#"themes" ofType:#"json"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:themesPath toPath:path error:&error];
if (error)
NSLog(#"Error %#", error);
}
It produces the following error:
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x194a0e90 {NSSourceFilePathErrorKey=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json, NSUserStringVariant=(
Copy
), NSFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json, NSDestinationFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json, NSUnderlyingError=0x194a0400 "The operation couldn’t be completed. Operation not permitted"}
After a search i've found this question and tried to modify my code as follows:
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *themesPath = [[NSBundle mainBundle] pathForResource:#"themes" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:themesPath];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
if (!success)
NSLog(#"Fail");
}
but it doesn't work either. success variable is NO. The last thing i tried was:
[data writeToFile:path atomically:YES];
but still in vain. it returns NO.
I need to note that the issue arises on a device only. On a simulator it works all right.
Can anybody give me a clue?
The first example is correct on one thing:
[docDir stringByAppendingPathExtension:#"themes.json"];
should be:
[docDir stringByAppendingPathComponent:#"themes.json"];
This becomes clear we you read the error message, you see that it tries to write the file to /var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json. Notice that there a . where there should be a /.
The stringByAppendingPathExtension: is used for adding a extension to a file, jpg, txt, html,.....
stringByAppendingPathComponent will add a file to a path, by adding the correct directory separator, you case /.
You second example will definitely fail since the app bundle is readonly.

Path to file in app bundle

I did this many times before but now it fails.
I want to access a file shipped within my app.
NSString* path = [[NSBundle mainBundle] pathForResource:#"information" ofType:#"xml"];
returns
/Users/eldude/Library/Application Support/iPhone Simulator/7.0.3/Applications/5969FF96-7023-4859-90C0-D4D03D25998D/App.app/information.xml
which is correct - checked in terminal and all that. However, trying to parse the path fails
NSURL* fileURL = [NSURL URLWithString:path];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
[nsXmlParser setDelegate:self];
if(![nsXmlParser parse]){
NSError* e = nsXmlParser.parserError;
NSLog(#"ERROR parsing XML file: \n %#",e);
self = NULL;
}
with
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" UserInfo=0xb9256f0 {NSXMLParserErrorMessage=Could not open data stream}
Ideas anyone?
File URLs and network URLs are different.
From the Apple documentation:
Important: To create NSURL objects for file system paths, use
fileURLWithPath:isDirectory:
or just
fileURLWithPath:
Example:
NSString *filePath = #"path/file.txt";
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(#"fileURL: %#", [fileURL absoluteURL]);
NSURL *netURL = [NSURL URLWithString:filePath];
NSLog(#"netURL: %#", [netURL absoluteURL]);
NSLog Output:
fileURL: file:///path/file.txt
netURL: path/file.txt

Error using zlib on ios

This is the code I use to read from a compressed resource file and write the decompressed data to a file in the documents directory:
- (void)setupPFile
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* passwordsFile = [documentsPath stringByAppendingPathComponent:#"p.txt"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:passwordsFile];
if (!fileExists) {
NSString *resourceDictPass = DICTIONARY_FILE;
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:resourceDictPass ofType:#"gz"];
NSData *fileCompressedData= [NSData dataWithContentsOfFile:resourcePath];
NSError *error = nil;
[fileCompressedData writeInflatedToFile:passwordsFile error:&error];
if (error) {
NSLog(#"error uncompressing passwords file");
return;
}
}
});
}
This is the error I get:
Printing description of error:
Error Domain=se.bitba.ZlibErrorDomain Code=2 "The operation couldn’t be completed. (se.bitba.ZlibErrorDomain error 2.)"
I am using a NSData+zlib category I found recommended around here.
How to debug this?

iOS - iPhone application documents "No such directory"?

I'm trying to use the method seen in the solution here to delete all the files in the iPhone documents directory for the application I'm writing. I've made some minor changes to the code in the solution in order to pass in the string location of the documents directory. My version of the code is the following:
NSString *directory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
NSLog(#"%#", directory);
NSError *error = nil;
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [directory stringByAppendingPathComponent:path];
BOOL removeSuccess = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
}
}
} else {
// Error handling
NSLog(#"%#", error);
}
When I attempt to run this however, the setting of directoryContents fails due to what's being passed being interpreted as a non-existent directory. Specifically, the two NSLog() statements I have put in the code returns the following:
2013-04-22 11:48:22.628 iphone-ipcamera[389:907] file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/
2013-04-22 11:48:22.650 iphone-ipcamera[389:907] Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x1d5232c0 {NSUnderlyingError=0x1d54c420 "The operation couldn’t be completed. No such file or directory", NSFilePath=file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/, NSUserStringVariant=(
Folder
)}
As far as I can see the path being printed to the NSLog looks correct so I'm not sure what I'm doing wrong. Can anyone point out to me where my mistake is? Thank you much!
Your code to get the value of directory isn't quite right. You want:
NSURL *directoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *directory = [directoryURL path];
Calling absoluteString on NSURL gives you a file URL. You don't want a file URL, you want the file URL converted to a file path. This is what the path method does.
Another way is:
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

Resources