How to create iPhone application can download content - iphone

Now I try to build iPhone application, like magazine online, that can download new magazine form web and store in app (can play offline).
I no idea for implement feature download new magazine form online source and store in the app.
Please help me.

Here's an example how you can download a file to your iPhone and reuse it later (in this case it's stores an HTML document that is then loaded to UIWebView from local store).
// downloading file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *dataPath = [paths objectAtIndex:0];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [dataPath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
NSString *fileString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
[fileString writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
// use local file
NSURL *loadUrl = [[NSURL alloc] initFileURLWithPath: filePath];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: loadUrl];
[webView loadRequest: request];
[request release];
[loadUrl release];

Related

When we open pdf in iPhone then how to save this pdf in iphone

I am very new to iOS. I create PDF and load this PDF on UIWebView
now this time I want to save or download this PDF in iPhone when we tapped download button then all exits PDF supporter show like as open ibook ,open in chrome. This type of option show but when we tap any one then my application closed.
-(void)show_Button
{
NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [docDirectories objectAtIndex:0];
NSString *filePAth = [docDirectory stringByAppendingPathComponent:#"myPDF.pdf"];
NSLog(#"filePath = %#", filePAth);
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSLog(#"url2 = %#", url2);
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
docContr.delegate=self;
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
so how to save or download this pdf in Iphone please solve this problem....
I believe you can simple use the belo two line:
NSData *myFile = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your_url"]];
[myFile writeToFile:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], #"yourfilename.pdf"] atomically:YES];
I hope this it will help you,
Saving the pdf into app
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"pdfname.pdf"];
NSError *writeError = nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&writeError];
if (writeError) {
NSLog(#"Error writing file: %#", writeError); }
Getting the pdf from the NSDocument Directory
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:&error];
for(int i=0;i<[filePathsArray count];i++)
{
NSString *strFilePath = [filePathsArray objectAtIndex:i];
if ([[strFilePath pathExtension] isEqualToString:#"pdf"])
{
NSString *pdfPath = [[stringPath stringByAppendingFormat:#"/"] stringByAppendingFormat:strFilePath];
NSData *data = [NSData dataWithContentsOfFile:pdfPath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[arrayOfImages addObject:image];
}
}
}

How to save filename with space on Iphone

I am trying to save file in document directory using
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename=[NSString stringWithFormat:#"abc 1.pdf",];
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:filename];
I am opening this file using a web view
NSURL *url = [NSURL URLWithString:pdfLocation];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:urlRequest];
[urlRequest release];
This fails if the the file name has a space.How should I save file so that space in the filename is maintained.
UIWebView expects a file URL in this case. Change the line to:
NSURL *url = [NSURL fileURLWithPath:pdfLocation];

Open downloaded files in webview in iphone

I am new to iphone development. I am developing an iphone application in which i am downloading files using web service with following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSData *fileData = [self decodeBase64WithString:soapResults];
[fileData writeToFile:fileLoc atomically:YES];
And my file is in path : /Users/myusername/Library/Application Support/iPhone Simulator/4.0/Applications/7A627E64-DEAB-40FA-BE04-35ED50580B65/filename.pdf
How can i open this file using uiwebview??? I tried this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename"];
[self loadDocument:fileLoc inView:mywebview];
My loadDocument method is:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
But it showing error... I think location is the problem.. How can i find the path in simulator as well as in orignal device???
Please help me...
pathForResource:ofType: can only be used to load resource files, which are shipped as part of your application bundle. You're trying to load a downloaded file from your Documents directory. So you should be able to just do
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
What is the error you are getting? What does your loadDocument method look like? You should load the document into the WebView something like this:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileLoc]]];

Reading local XML file makes NSXMLParser return NULL

I have a local xml file which I know contains UTF-8 well-formed xml. But NSXMLParser will not parse correclty.
Path to file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"example.xml"];
Saving the xml from web:
NSURL *xml = [NSURL URLWithString:#"http://www.example.com/example.xml"];
NSString *data = [[NSString alloc] initWithContentsOfURL:xml encoding:NSUTF8StringEncoding error:nil];
[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
Parsing the xml:
NSURL *xmlFile = [NSURL URLWithString:appFile];
NSXMLParser *addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFile];
If I try parsing the NSURL "xml" directly it works, but the code above will return NULL and no errors. I found another post here on the forum describing the exact same thing, but I cannot find it again, and the post had no answers.
Hope someone can help! Basically it is just a matter of storing a web xml to file, and then feeding it to NSXMLParser.
Change this line:
NSURL *xmlFile = [NSURL URLWithString:appFile];
to this:
NSURL *xmlFile = [NSURL fileURLWithPath:appFile];
Because appFile is a local file system path and not an internet URL.

i can unzip the folder but how to access that folder form iphone

this code allows you to unzip file and create a folder in documents lets say List and this List folder is having all the unzip data now how to access that List content??
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *saveLocation =
[documentsDirectory stringByAppendingString:#"myfile.zip"];
NSFileManager* fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:saveLocation]) {
[fileManager removeItemAtPath:saveLocation error:nil];
NSLog(#" file path %#",fileManager);
}
NSURLRequest *theRequest =
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://localhost/list.zip"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSData *received =
[NSURLConnection sendSynchronousRequest:theRequest
returningResponse:nil error:nil];
if ([received writeToFile:saveLocation atomically:TRUE]) {
NSString *cmd =
[NSString stringWithFormat:#"unzip \"%#\" -d\"%#\"",
saveLocation, documentsDirectory];
// Here comes the magic...
system([cmd UTF8String]);
}
NSLog(#"loca is %#", saveLocation);
NSLog(#"%#", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]); // i can see list folder but how to get the content outside??
to unzip file on iphone read this - http://www.touch-code-magazine.com/update-dynamically-your-iphone-app-with-new-content/
You can access it with NSFileManager's
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error.