I want to make the object of NSUrl in following way but Utilities is undefined.
Please someone tell me If i want to use Utilities class then which framework have to include in our project.
NSString* outputFileName = #"outputFile.mov";
NSString* outputFilePath = [Utilities documentsPath:outputFileName];
NSURL* outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
More easy
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"outputFile" ofType:#"mov"];
Nothing that I know about, but what you are probably looking for is the path to the application documents directory
given by...
NSString *docspath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* outputFilePath = [docspath stringByAppendingPathComponent:outputFileName];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"Documents/movie.m4v"]];
Related
Have a Binary code convert it to nsdata using dataFromBase64String. And load it web view successfully. But Want the pdf file do not store in documentsDirectory.Need to add one button click user click the button, then Binary to pdf convert will happened and display in web view. here my code. How is possible help me. thanks advance.
NSString *binaryString =#"Binary code to change";
myData = [NSData dataFromBase64String: binaryString];
[self savePDF:myData];
- (void)savePDF:(NSData *)pdfContent
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
finalPath = [documentsDirectory stringByAppendingPathComponent:#"myPdf.pdf"];
NSLog(#"%#",finalPath);
NSURL *url = [NSURL fileURLWithPath:finalPath];
[pdfContent writeToURL:url atomically:YES];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
If you don't want to store pdf file to DocumentsDirectory then
try following, hope it will work for you
NSString *binaryString =#"Binary code to change";
myData = [NSData dataFromBase64String: binaryString];
[self savePDF:myData];
- (void)savePDF:(NSData *)pdfContent
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
finalPath = [documentsDirectory stringByAppendingPathComponent:#"myPdf.pdf"];
NSLog(#"%#",finalPath);
NSURL *url = [NSURL fileURLWithPath:finalPath];
//[pdfContent writeToURL:url atomically:YES];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Comment //[pdfContent writeToURL:url atomically:YES]; line
and check
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/audio.mp3",documentsDirectory];
NSURL *audioPathURL = [NSURL fileURLWithPath:filePath];
NSFileManager *filemanager = [[NSFileManager alloc]init];
if ([filemanager fileExistsAtPath:filePath])
{
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:audioPathURL error:&err];
}
This can not read the audio file, It returns audioFile nil. For same code when I pass url from NSBundle like
NSString *path = [[NSBundle mainBundle] pathForResource:#"Audio" ofType:#"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
It works fine, Any sugestions. Thank you
Issue could be the "/" you are using for append filename to path. Try below change
NSString *filePath = [NSString stringWithFormat:#"%#audio.mp3",documentsDirectory];
Or try below alternative:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audio.mp3"];
The NSBundle class is used for finds thing in applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work with Documents directory.
Try as below:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audio.mp3"];
NSURL *audioPathURL = [NSURL fileURLWithPath:filePath];
NSFileManager *filemanager = [[NSFileManager alloc]init];
if ([filemanager fileExistsAtPath:filePath])
{
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:audioPathURL error:&err];
}
I am using the below code to download a ebook from url and store into my device. While this works in emulator, in my ipad I am getting error saying file is not available. I am not able to read the file in the respective path of the device. please help..
NSString *urls = [NSString stringWithFormat: #"http://hungerin.com/?download_file=%#&order=%#&email=%#&key=%#", BookID, orderKey, email, downloadId];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: urls]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#""]];
NSString *filePathAppend = [NSString stringWithFormat: #"/testPubb.app/Documents/%#.epub",BookID];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:filePathAppend];
[pdfData writeToFile:filePath atomically:YES];
The way you are getting the app document directory path is not correct. Try this:
NSString *urls = [NSString stringWithFormat: #"http://hungerin.com/?download_file=%#&order=%#&email=%#&key=%#", BookID, orderKey, email, downloadId];
NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urls]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.epub",BookID];
[pdfData writeToFile:filePath atomically:YES];
Let me know if you have questions.
Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard.
Note: I only need the file URLs, I do not need to access the actual files.
You can get the URL of a file in the main bundle using
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeFile" ofType:#"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];
You can write this URL to, for example, a property list file in the Documents directory:
NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:#"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:#"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];
If you don't know the names of the files and you just want to list all the files in the bundle, use
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// do something with `url`
}
Or in Swift 4:
let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")
Yes, you would get their path:
NSString *path = [NSBundle mainBundle] pathForResource:#"file1" ofType:#"png"];
NSURL *fileURL = [NSURL fileURLWithPath:path]
// save it as any other object or in a dictionary:
[myMutableDictionary setObject:fileURL forKey:#"file1.png"];
EDIT: to get the complete list of files, use the NSFileManager, get the path to the bundle itself, then walk each directory getting the files, making URLs, and saving them somewhere. There is oodles of code on SO how to walk a directory to do this. [You should update your question to be more specific on what you want, this was not made clear at all originally]
There's an API on NSBundle (documentation) available on 10.6 or iOS 4 to get an NSURL directly, rather than passing a path to the NSURL constructor:
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext;
It also has variants that take a subdirectory and localizationName, the same as its -pathForResource: equivalents.
I have this code below:
NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:#"recentDownload"];
NSString *fullPath = [NSBundle pathForResource:fileName ofType:#"txt" inDirectory:[NSHomeDirectory() stringByAppendingString:#"/Documents/"]];
NSError *error = nil;
[textViewerDownload setText:[NSString stringWithContentsOfFile:fullPath encoding: NSUTF8StringEncoding error:&error]];
textviewerdownload is the textview displaying the text from the file. The actual file name is stored in an NSUserDefault called recentDownload.
When I build this, I click the button which this is under, and my application crashes.
Is there anything wrong with the syntax or just simple error?
The NSBundle class is used for finding things within your applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work. Try this instead:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:#"recentDownload.txt"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:#"myfile.txt"];
if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:myPathInfo toPath:myPathDocs error:NULL];
}
//Load from File
NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];
This worked for me
Anyway, thank you all..
For read/write from text file check this url.