How to open url which is in PDF file - ios

i triad to click on URL which is in PDF file. and PDF file opened in UIWebView, but i cant able to click on URL.
any one tell me how to open URL which is in PDF file.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
NSURL *targetURL = [NSURL URLWithString:#"http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];

In you webView deleget method, first of detect the url from PDF and then open in webview or etc. I have used following method to do this, may be its helping you :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [[ request URL] retain];
if (([[requestURL scheme] isEqualToString: #"http"] || [[requestURL scheme] isEqualToString: #"https"]) && (navigationType == UIWebViewNavigationTypeLinkClicked))
{
// Your method when tap on url found in PDF
}
}

If you have a PDF file in Local storage
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
If you have Access to file From the Internet
- (void) loadRemotePdf
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSURL *myUrl = [NSURL URLWithString:#"http://www.mysite.com/test.pdf"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[webView loadRequest:myRequest];
[window addSubview: myWebView];
[myWebView release];
}

Related

display ppt files in UIWebview in ios

I want to display MSOffice extensions in webview. Is this possible?
I've tried this:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
// Now create Request for the file that was saved in your documents folder
//UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 300)];
NSURL *targetURL = [NSURL URLWithString:fileurl];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
But I get the message:
"An error occured while reading the document".
Try below if your ppt file is drag dropped in your project
[webView loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"FileName" ofType:#"ppt"]isDirectory:NO]]];
If you are getting your file from server in form of NSData then use below
NSURL *url=[NSURL URLWithString:[#"yourURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData* responseData = [NSData dataWithContentsOfURL: url];
[webView loadData:responseData MIMEType:#"application/ppt"
textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#""]];

Detect when WKWebview cannot handle a request

I have the following code
NSURL *nsurl = [NSURL urlWithString:#"http://url.that.does.not.exist.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
WKWebView *wv = [[WKWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds] configuration:config];
wv.navigationDelegate = self;
And the delegate:
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
...
}
Works fine and I can catch errors when the could not be loaded (404, etc). But how do I catch the error when the nsurl was something WKWebView cannot even handle?
For example:
NSURL *nsurl = [NSURL urlWithString:#"nacho"];
will lead to a blank page with no delegate method being called. How can I handle detect this error? (I have checked at this iPhone SDK: Check validity of URLs with NSURL not working? but I wonder if there is anything better than regex?)
You can try and see if the url can be requested like this:
NSURL *nsurl = [NSURL urlWithString:#"nacho"];
if([NSURLConnection canHandleRequest:[NSURLRequest requestWithURL: nsurl]]){
NSLog(#"URL can be loaded");
}else{
NSLog(#"URL cannot be loaded");
}

web server show pdf in UIWebView but didFailLoadWithError in iOS run with xcode 6

PDF file error with run in xcode 6.0
It's not work ......
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
Its going to encode .. try it. Within url 'record id_2_1_1410848039.pdf' in between record and id space count.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
webView.delegate = self;
NSString *getURL = #"http://google.com/images/item_attachment/282/record id_2_1_1410848039.pdf";
NSString *encodedString=[getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:webURL];
[webView loadRequest:request];
[self.view addSubview:webView];
webView.ScalesPageToFit = true;

Not able to load the file(doc,pdf etc) from url in UIWebview for ios 7

I am loading different file types like PDF, Excel, Doc etc in UIWebview. Some files requires authorization and passed the value in header.
This works fine in ios 6. Not working in ios 7.
Below is the code and error message.
NSURL *url =[NSURL URLWithString:regularURL];
self.webView.scalesPageToFit=YES;
self.request = [NSMutableURLRequest requestWithURL:url];
[self.request setValue:#"multipart/form-data" forHTTPHeaderField:#"Accept"];
NSString *auth = [NSString stringWithFormat:#"Bearer %#",userToken];
[self.request setValue:auth forHTTPHeaderField:#"Authorization"];
Error Message:
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0xd4b5310 {
Is there any additional header field to be passed for ios 7 web view?
I have tried to solve the problem with NSURLCache solution but that didn't work for me.
You have to try next:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSString *strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *targetURL = [NSURL URLWithString:strUrl];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString: strUrl]];
[webView loadData:dataFromUrl MIMEType:#"application/pdf" textEncodingName:nil baseURL:nil];
[self.view addSubview:webView];
It work for the all files that have not worked before (pdf, doc, etc).
I have kind of found a solution. Not perfect but it works!
Set a shared URL cache before loading the request, then intercept the error and load the cached data with the correct MIME type into the webView manually.
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:256 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
And then
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSCachedURLResponse* cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.originalRequest];
if (cachedResponse) {
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)(cachedResponse.response.URL.pathExtension), NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
NSString* MIMETypeString = (__bridge_transfer NSString *)MIMEType;
[self.webView loadData:cachedResponse.data MIMEType:MIMETypeString textEncodingName:nil baseURL:nil];
}
}
Naturally, you must set your WebViews delegate to somewhere you put the above delegate method.
This code works just fine in iOS 7 , screenshot attached. Hope it helps...
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 320, 480)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];

Zoom/Pinch the pdf which is loaded in a UIWebView

I have loaded a PDF file in a UIWebView from the resource bundle.
Now, I am not able to zoom/pinch the PDF file.
I found the below 2 relevant links, but none of the answers are marked as correct -
How to zoom webView with pdf file
Enable zooming/pinch on UIWebView
How to make the PDF zoomable/pinchable which has been loaded in a UIWebView from the resource bundle, Will the below solution work ?
Problem with pdf while opening in UIWebView
Thanks for your help.
In your WebView on Interface Builder add check to Scale Page To Fit and you enable a Pinch ZoomIn ZoomOut ;)
Or if you want to lad better your PDF try to Look this code:
- (void)viewDidLoad
{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:#"http:pdfURL"]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourPDFFile" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
//--------------AND HERE USE SCALE PAGE TO FIT------------------//
[webView setScalesPageToFit:YES];
}
Hope this help you.
//Try like this
NSString *urlstr=#"www.example.com/yy.pdf";
web=nil;
web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 98, 320, 367)];
web.delegate=self;
[self.view addSubview:web];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]]];
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinch:)];
pgr.delegate = self;
[web addGestureRecognizer:pgr];
// Target Action
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
Before this add UIGestureRecognizerDelegate in .h. Hope it will helps you..
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:documentsDirectory])
{
NSURL *url = [NSURL fileURLWithPath:strFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webview loadRequest:request];
}

Resources