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?
Related
I have an HTML file save in a temporary directory like that :
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentDirectory = NSTemporaryDirectory();
NSString *documentPath = [documentDirectory stringByAppendingPathComponent:#"mydocument.html"];
[fileManager createFileAtPath:documentPath contents:myHTMLDocumentData attributes:nil];
The document is created in my temporary file. After it, I want to open this document in Safari but it doesn't work :
NSURL *url = [NSURL fileURLWithPath:documentPath];
[[UIApplication sharedApplication] openURL:url];
Nothing happen at screen, no error...
However, if I replace "url" by #"http://google.fr", Safari is launched with google.fr and I can access to my temporary file by typing the url "file://localhost..../myHtmlDocument.html" in Safari.
Hope you can help me
You cannot open a document with resides in your app bundle through Safari (please, see iOS Security Model).
What you need is using an UIWebView to display your document content inside your app using – loadHTMLString:baseURL:; e.g.:
UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];
i dont think you can achieve that, since both UIApplication openURL: and UIDocumentInteractionController will not open local files inside your bundle
Do the following instead, in your viewDidLoad
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath: documentPath]];
[webView loadRequest:request];
Currently, I detect if the UIWebView load a pdf file by doing a check on the current URL. Next I download the pdf file with ASIHTTPRequest library. The problem is that if the file is display in the UIWebView, is that it is already downloaded somewhere, so I download this file twice. How can I get this file load in my UIWebView ?
The purpose is to store this file loaded in my UIWebView in my Document directory.
Here's how you can download, read and store your pdf locally in iphone application, so that you don't have to download regularly:
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]){ // if file not present
// 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];
All I can suggest is either to download it a second time, or put it in the temporal storage and then put it it the UIWebView and when the user asks, then put it where you want from the temporal storage.
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]];
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];
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];
}