changing the name of pdf by the title - iphone

In my application I obtain a title of PDF which is saved in my plist with static name which is i given like s.pdf
After that I find the title of that pdf and now I want to save the same pdf with that title name which I got and also replacing the old name which is s.pdf
So how can I do that?

You probably going to need NSFileManager copyItemAtPath:toPath:error:
Copies the directory or file specified in a given path to a different location in the file system identified by another path.
Example This is not a complete code it's for explanation only:
// This will get you the Bundle Path.
NSString*path = [[NSBundle mainBundle] resourcePath];
// This will get your PDF in the Bundle Path.
[[NSBundle mainBundle] pathForResource:#"s" ofType:#"pdf"]
// Copy your file to the same destination but with different name.
[[NSFileManager defaultManager] copyItemAtPath:OldFileNameInBundle
toPath:NewFileNameInBundle
error:&err]
NSFileManager copyItemAtPath

Related

iOS File won't write to directory

I'm using cocoaHTTPServer library that allows you to store files "publicly". For some reason I can't write files to the directory.
I can write to my local file directory, but not the public "Web" folder.
/var/mobile/Applications/42CE308A-F804-47E3-BA07-CB921B909FFB/Documents/song.caf
/var/mobile/Applications/42CE308A-F804-47E3-BA07-CB921B909FFB/MyApp.app/Web/song.caf
The top one works, the bottom one doesn't.
Meaning, when I save the file, then check for the file in the documents directory, it is there, when I save, and then check the "Web" directory, the directory is empty.
And i'm using AVAssetWriter to save the song.
NSString *exportPath = [DataCenter sharedDataCenter].songURLLocal;
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL
fileType:AVFileTypeCoreAudioFormat
error:&assetError];
What does the assetError output through NSLog?
Also, You will not be able to write the the Web directory which is clearly located in your app bundle — which is sandboxed. The only directory you can write to is the Documents directory.
Also, what is the NSString *exportPath = [DataCenter sharedDataCenter].songURLLocal?
If exportPath is the Web directory then your methods will always fail.

PDF File / URL in local filesystem

Hy!
I had generate a PDF File on my iPhone App.
Now I want to send it via EMail - i use the MailViewController and have to add my attachment, but now the question...
Whick URL should I write when I only written the filename.pdf at the PDFContext?
To send a pdf file that is included as a resource in your app:
NSString *path = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"];
To send a pdf file that you've GENERATED in your app:
write to disk.
use the path to attach it to the email.
For a detailed discussion of temp directories, read this:
http://cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html
But in short:
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"filename.pdf"];
if ([fileData writeToFile:filePath atomically:YES]) {
NSLog(#"success!");
} else {
NSLog(#"fail");
}
the path you use to create a PDFContext should be in either the documents directory, or the temp directory, depending on how long you want the file to persist. "filename.pdf" is not a valid path to be creating a pdf context with.

NSBundle and Accessing Folder inside Resources Folder

I have Images folder inside the Resources folder which contains images. I am using the following code to access the images but the resultant array always comes out to be empty:
NSArray *imagesPath = [bundle pathsForResourcesOfType:#"gif" inDirectory:#"Images"];
Am I missing something??
If you do [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil] do you get the correct path for that folder?
If not are you sure that is a folder and not a simple group?

Is possible to read plist from application bundle and documents folder at the same time?

Is it possible?to read from my local bundle and at the same time also read from documents folder into UItableview?
thanks thanks
yes.simultaneously
No — as in the iPhone isn't multicore, you can't have "simultaneous" :p
Yes — as in you can open multiple files in the same period of time. There's no conflicts as long as the files are different (if the files are the same then it depends on how others are using and locking the file etc.)
on viewDidLoad or some similar event when you would be populating your table data, you would simply just aggregate the two files together... that is you are are likely populating an array or dictionary with the contents of the file in question... so use the mutable version of array/dictionary, initialize it empty, then read in the first file from whatever location you choose, populating into your mutable array/dictionary, then do the the same for the next file. after you are done, reloadData as you normally would as if you had read form one file.
As far as simultaneous goes, technically no. However, one could have two different active threads each one reading required files and parsing the data.
Regarding the files you want to access...
Here is a quick and dirty method I use in one project (which I just happen to be working on at the moment):
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError* error;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
self.blahDBPath = [self.documentsDirectory stringByAppendingPathComponent: #"blah.db"];
NSLog(#"Mainbundle Resourcepath: %#", [[NSBundle mainBundle] resourcePath]);
NSString* defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"blah.db"];
NSLog(#"Default DB Path: %#", defaultDBPath);
success = [fileManager copyItemAtPath:defaultDBPath toPath:self.blahDBPath error:&error];
if (!success) {
NSAssert1(0, #"blah blah blah '%#'.", [error localizedDescription]);
}
It is ugly but effective. I'll rewrite it when I refactor the application.
The point here is that I ask the operating system for the path to certain directories. Then I add file names or subdirectories as required. This allows the operating system to manage paths (like in the simulator where each successive build gets a new unique id as part of its path) and I just worry about the final directories and file names in the application.
One I have the paths, I copy the required file from the bundle directory and put them somewhere, the Documents directory in this case. Then I can do whatever I need to with them.
If I just wanted to access them as they are in the bundle directory, then I I just refer to them by using [[NSBundle mainBundle] resourcePath].
I think something along the lines of the above snippet is what you are looking for.
-isdi-

iPhone Referring to Resources in Separate Directories

This question tells how one can create directories in your resources path. Once created, how does one reference these directories?
I have created an html directory with a structure for my internal pages, now I want to load the index.html file from the html directory. Thus, I'll need the file path to it.
I can just use:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"];
But what happens if there are two index.html files contained in the directory structure? Does it just find the first one? Can the referencing be more specific?
Little test project here that is not working if you want to take a look
If you have a path that is copied into your resource bundle, you also should reference the path when looking for the resource like:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"html/index" ofType:#"html"];
EDIT:
You cannot apparently just include the directory in the resource name (I believe you can for other similar calls like "imageNamed:" on UIImage). The working call is:
NSString *helpFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"];