Objective-c: Downloading a PDF file - iphone

I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.mydomain.com/mypdffile.pdf"]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"<Application_Home>/Documents/"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];

You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.
Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

Related

How to use HTML file in iPhone

I want to use an HTML file in my code currently I am using following line of code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:nil];
But the problem which I am facing is: the HTML Page contains some images, and all that images are in a folder in my project but I am not able to see that images in the HTML page at run time.
You can set the baseURL to your resource folder, where your images are located:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:nil];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
NSURL *baseUrl = [[NSURL alloc]initFileURLWithPath:resourcePath];
[self.myWebView loadHTMLString:htlmString baseURL:baseUrl];
Here the possible ways to use HTML into the project,
Following code for the load HTML file into xcode project
Example 1, loading the content from a URLNSURL
NSURL *url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
Example 2, loading the content from a string
NSString *HTMLData = #"<h1>Hello this is a test</h1>";
[webView loadHTMLString:HTMLData baseURL:nil];
Example 3, loading the content from a local file
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
Hope this code helping to develop a application

feed://... in my UIWebView

I want to open RSS Feed in my UIWebView, but it isn't working and opening Safari.
My code:
NSString *link = [[NSString alloc] initWithFormat:#"feed://smartfiction.disqus.com/%#/latest.rss", slug];
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *requestWeb = [NSURLRequest requestWithURL:url];
[webViewComment loadRequest:requestWeb];
I found right way. I have created rss parser and showed feed in UITableView. It's more beautiful and easy.
feed:// is a URL Scheme which is handled by Safari and so will only be opened by Safari... you could try :
NSString *link = [[NSString alloc] initWithFormat:#"feed://smartfiction.disqus.com/%#/latest.rss", slug];
NSURL *url = [NSURL URLWithString:link];
NSData *rssData = [NSData dataWithContentsOfURL:url];
[webViewComment loadData:rssData MIMEtype:#"application/rss+xml" textEncodingName:#"utf-8" baseURL:nil];

How to fetch pdf file in Controller file in iphone

I have to read pdf file from my controller. How to load it ?
Thanks
You could use UIWebView to view the pdf file.
Check the below SO post.
Display pdf from the weburl iphone sdk
Tutorial link1
From apple
Using UIWebView to display select document types
Here's how you can read and store your pdf locally:
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you simply want to read/load pdf from your Resource folder then simply do this :
NSString* filePath= [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"]];
/// or you can even read docs file as : pathForResource:#"sample" ofType:#"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];

iPhone SDK: WebView how to specify a local URL?

I have HTML files that I want to load locally. I have included the files in the resources folder in XCode. I am not sure what the syntax is to load them.
This is the code I use to connect to say google.
NSString *urlAddress=#"http\\someurl";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webHelpView loadRequest:requestObj];
Could someone provide an example where a HTML file is loaded locally.
Thanks in advance.
This is the only code that worked for me.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SettingsHelp.html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Thanks all for the help.
Can't you just do
NSURL *url = [NSURL URLWithString:#"file:///path/to/file"];
?
Sure, here is some code from an app that loads a locallly stored html page into a web view:
- (void) viewDidLoad
{
// Load the content into the view
NSString* path = [NSString stringWithFormat: #"%#/index.html",
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]];
[webView_ loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: path]]];
}

Creating a PDF programmatically on the iPhone?

Hey all, was wondering if anyone could point me to the right direction. I'm curious about how I would go about creating a PDF programmatically on the iPhone. The documentation mentions CGPDFDocumentCreateWithProvider but i'm not sure how to go about using it. Thanks!
Likewise I do not believe it is possible to view a PDF on the iPhone without an external service to render the output
This is incorrect. You can render a PDF document within a UIWebView instance, for example:
NSString *urlPathStr = [[NSBundle mainBundle] pathForResource:#"myPDFFile" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPathStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:urlRequest];
You can also render a PDF within a UIView object for better performance.
The PDFKit library is not yet available for the iPhone.
You might look at the QuartzDemo sample application for some Quartz2D methods for rendering and manipulating a PDF document.
CGPDFDocumentCreateWithProvider or CGPDFDocumentCreateWithURL are the methods you have to use.
i tried this methode CGPDFDocumentCreateWithURL.it will create a pdf in the url that you have provided to the methode as first parameter.
Once created ,,load the url in the web view to view the pdf.
you can enter content in pdf using cgcontextRef class.
Here's how you can read and store your pdf locally in iphone application:
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you simply want to read/load pdf from your Resource folder then simply do this :
NSString* filePath= [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"]];
/// or you can even read docs file as : pathForResource:#"sample" ofType:#"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you want to create your own pdf from bunch of text and strings,here's how i did it:
http://iosameer.blogspot.com/2012/09/creating-pdf-file-from-simple.html