EXCEPTION CPMessageException: (null) in uiwebview - iphone

I'm new in iPhone, I'm trying to open .doc file in the UIWebView, I wrote the following code
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#.doc", docName] ofType:nil];
NSLog(#"in search the document url address: %#", urlAddress);
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
the application doesn't crash, but it gives me the following:
EXCEPTION CPMessageException: (null)
and loads a blank page
Any Help in this issue?

Check out these two SO posts:
Reading Open-XML documents in IOS
any objective-c library to parse/read word documents?

Related

UIWebView and Safari content not looking the same

I have a UIWebView which is adapted to a mobile style of a forum I have. In Safari, it looks and works great.
However, inside my WebView there is a disastrous problem.
When quoting or editing a post, HTML is displayed inside of BBcode. Furthermore, posting code results in ignored line breaks.
The webview is loaded like this:
//display the webview
NSString *fullURL = #"http://www.mysite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[_webTest loadRequest:requestObj];
Change your this line:
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
With this line & check:
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
Do you want to load a html-string in your webView?
If it is the thing than following may help you:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath];
NSString *htmlString = [bundlePath pathForResource:#"index" ofType:#"html"] ;
[webView loadHTMLString:htmlString baseURL:baseURL];

How to use the HTML Template In Xcode?

can some one tell me how i can use html template on the View Control?
if you want to use existing html page you have then you should use ---
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyWebPage" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
OR
If you want to use your own code for html then you can use
[myWebView loadHTMLString:#"<html><head></head><body style=\"font-family: sans-serif;\"> .... </body></html>" baseURL:nil];
Hope you were helped....

Loading PDF file from Resource directory in iPhone crashes

NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:#"sampleLayout.pdf" withExtension:nil];
This above line warns NSBundle may not respond to -URLForResource:withExtension:
and the app crashes while loading the PDF file from this URL path.
Why dont you try like this..?
NSString *urlPath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webViewOutlet loadRequest:urlRequest];
NSURL *pdfPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"]];
i got working from this above line, i think this is alternative for ipad 3.2 from iphone 4.0 os

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