PDF File / URL in local filesystem - iphone

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.

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.

How to download file and save in local iphone application?

I have locally saved one xml file and showing the details by using NSXmlParser(Which is stored in Resource folder). But, now i want to download new xml from url(from client side) and need to store that new xml file in local and also need to delete existing one from the application. How can i do this? Any suggestions? There is any sample code/project for reference? Please help me. Thanks for reading my poor english. Thanks is advance.
although u can delete file from yr resource directory and save new one but the my way is to perform file operation mostly on app diretories like document directory or cache directory.
first u will to save yr xml file from app resource to cache directory then access file from there.And u can also remove file from resource directory ,its no longer needed.
any new file available in internet then first remove old one from cache and add new one to cache directory.
whole thing is as follows:-
//get your cachedirectory path
NSArray *imagepaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachdirectory = [[NSString alloc] initWithString:[imagepaths objectAtIndex:0]];
//first save your file from resource directory to app's cache directory
NSString * path = [[NSBundle mainBundle] pathForResource:#"fileinresource" ofType:#"xml"];
NSData *filedata = [NSData dataWithContentsOfFile:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *savename = [cachdirectory stringByAppendingPathComponent:#"mynewfile.xml"];
[fileManager createFileAtPath:savename contents:filedata attributes:nil];
//remove file from resource directory
[fileManager removeItemAtPath:path error:nil];
//new file from internet is avilable then do following:first remove old file from cache
[fileManager removeItemAtPath:cachdirectory error:nil];
//save new file from internet
NSData *newfiledata=[NSData dataWithContentsOfURL:[NSURL URLWithString:myxmlurlstringpath]];
[fileManager createFileAtPath:savename contents:newfiledata attributes:nil];

changing the name of pdf by the title

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

How to read a local xml file

I want to create a NSURL object out of a NSString, where I use -fileURLWithPath:(NSString )
I put my xml file as my source file, and name it event.xml.
But
NSString URL = #"event.xml";
NSURL a = [NSURL fileURLWithPath:URL];
but then my xmlparser return a connection failed error.
so what's the correct way to specify the path of a xml in the source file?
sorry about the formatting but I'm really in a rush
That depends on the directory your XML file is in. If we are talking about a file in your app bundle, it is:
NSString *path = [[NSBundle mainBundle] pathForResource:#"event" ofType:#"xml"];

can i update my local text file on iphone

i have a text file so can i update this text file from web ???
lets say i have
1. NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
2. NSData *myData = [NSData dataWithContentsOfFile:filePath];
3. if (myData) {
4. // do something useful
5. }
right now Myfile.text is having 10 data so how to insert 10 more to this data??
plz help
Files stored inside your app bundle will not be writable.
You will have to make a copy of the file and store it in the Documents or Library folder before you will be able to edit it.