Objective C Load PDF file in UIWebView - objective-c

EDIT:
There was something wrong with my Base64 decoding. I searched for a external Base64 decoder and it working just like this:
This is the case:
I have a Base64 encoded byte array I get from a webservice and convert it to NSData:
NSData *data = [Base64 decodeBase64WithString:response];
And in my Webview Controller I declared:
[webview loadData:fileData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
fileData is the decoded data.
When I run this I get a gray screen.
So I assume I'm not giving it a correct NSData object.

I already answered my own question when I was typing it.
So I assume I'm not giving it a correct NSData object.
My Base64 decoding was wrong.
Using this statement works like a charm:
[webview loadData:fileData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I'm just posting so other people can look at it of they have the same problem.

[webView loadata:data MIMEType:#"application/pdf" textEncodingName:#"UTF-8" baseURL:nil];
That should do the trick for you, if it doesn't you can write it to a file as V1ru8 suggests, but that is an extra step in most of the cases.
Hope this will helps

In addition to the already provided answers, I've found that loading NSData* into a UIWebView in the initializer function of a containing UIViewController doesn't work and there'll be no error.
The NSData*needs to be loaded into the UIWebView in the viewDidLoad function.

UIWebView is DEPRECATED, use WKWebView
https://developer.apple.com/documentation/webkit/wkwebview?language=objc
Objective-C Example:
#import <WebKit/WebKit.h>
...
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
//UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; //DEPRECATED
NSURL *targetURL = [[NSBundle mainBundle] URLForResource:#"fileName" withExtension:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
//[webView loadRequest:request]; //DEPRECATED
[wkwebView loadRequest:request];
//[self.view addSubview:webView]; //DEPRECATED
[self.view addSubview:wkwebView];

The easiest way to do that:
UIWebView *webview = [[UIWebView alloc] init];
[self.view addSubview:webview];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdfFileName" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];

Related

Injecting JavaScript into UIWebView

I am trying to use the stringByEvaluatingJavaScriptFromString: to alter a WebView and it's not executing as I expect.
Here is the code, this JS doesn't execute though. Any idea why?
UIWebView *wv = [UIWebView alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"http://www.google.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[wv loadRequest:request];
[wv stringByEvaluatingJavaScriptFromString:#"document.write('This Works')"];
Probably you'll have to wait for the request to finish before you can use stringByEvaluatingJavaScriptFromString. Set wv.delegate = self; and then implement -webViewDidFinishLoad like this
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:#"document.write('This Works')"];
}

UIWebView doesn't load content

I don't understand why if I load the content of a UIWebView in XCode this way:
NSString *string = #"http://www.dummyurl.org/dummy.pdf";
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
everything works fine, but if a build the original string from a php script I wrote:
NSString *strURL = [NSSTring stringWithFormat:#"www.myserver.php"];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
nothing works. I checked my script and it returns EXACTLY the original string (http://www.dummyurl.org/dummy.pdf) that works fine with the first method.
I build from a PHP script the content of a UITextView too, but that works fine.
I don't understand why this method works with the UITextView but not with the UIWebView to load the .pdf.
It may be useful
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *tempString = [string stringByTrimmingCharactersInSet:characterSet];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
Make sure you linked the webview (IBOutlet) and the delegate.
With this lines it should load a url:
webView.delegate = self;
NSURLRequest *urlRequest;
NSString *urlToOpen = [NSString stringWithFormat:#"http://www.stackoverflow.com"];
urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlToOpen]];
[self.webView loadRequest:urlRequest];
Hope this helps...
I had this and it was a cache problem:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];

How can i detect Phone number and Hyperlink from Pdf file?

I am displaying my pdf file in UIWebView now i want to detect links and Phone number which is available in my pdf and by pressing link it should open in browser and touching on number it should be called on that particular number so please anybody have idea about it.Give me some suggestion on it.I have written this code for achieving this task but it do not work:
webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 764, 1004)];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Gita.pdf"];
NSLog(#"FilePath==>%#",filePath);
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSLog(#"url==>%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView setDataDetectorTypes: UIDataDetectorTypeAll];
[webView loadRequest:requestObj];
check this link:
http://bill.dudney.net/roller/objc/entry/tiledlayer_on_the_iphone and http://bill.dudney.net/roller/objc/entry/catiledlayer_example.
Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

Having trouble with this NSURL method

Can anybody tell me excatly how I should execute this method on an NSURL object?
URLByResolvingSymlinksInPath
Compiler says that the method can't be found when I execute it.
Thanks a lot.
This is where I am trying to implement the code:
- (void)data
{
NSURL *url = [[NSURL alloc] initWithString: [[user data] objectAtIndex: 5]];
NSURL * url2 = [[NSURL alloc] init];
url2 = [url URLByResolvingSymlinksInPath];
NSData *newImage = [[NSData alloc] initWithContentsOfURL:url2];
NSImage *image = [[NSImage alloc] initWithData:newImage];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 3.0;
[imageView setImage:image];
[user autorelease];
}
Assuming that you have NSURL named something like myURLWithSymlinks, you would do something like:
NSURL * myURLWithoutSymlinks = [myURLWithSymlinks URLByResolvingSymlinksInPath];
Note that according to the docs, URLByResolvingSymlinksInPath is only available in iOS4.0 or above and MacOSX 10.6. If you are using an older version of iOS (or MacOSX), that could be the cause of your problem.

reading excel sheet with url in Iphone sdk

I am Having a problem here.I am downloading the excel sheet using Url and have to displaying it in the web view here my problem is the Excel sheet is not displaying.
I written the code as:
NSString *str = #"";
str = [str stringByAppendingString:
#"http://databases.about.com/library/samples/address.xls"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[webView loadData:data MIMEType:#"application/xls" textEncodingName:#"UTF-8"
baseURL:[NSURL URLWithString:#"http://google.com"]];
[contentView addSubview:webView];
Thank you,
Monish.
Are you able to view it using Safari on the phone or does it say "unsupported format"?
Try another file that works in Safari and then try in UIWebView.
Also see this: http://developer.apple.com/iphone/library/qa/qa2008/qa1630.html
First make sure you are able to view the xls in Safari then try code like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.yourwebsite.com/good.xls"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}