iOS - Zoom into pdf in webview after it has loaded - ios

I have the following code, which loads a pdf from the apps documents into a UIWebView.
-(IBAction)viewMyPdf:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *path = [documentsPath stringByAppendingPathComponent:#"myfilename.pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];
[self performSelector:#selector(theTest) withObject:nil afterDelay:2.0];
}
-(void)theTest {
NSLog(#"Hello");
[webView.scrollView setZoomScale:5.0 animated:NO];
webView.scrollView.contentOffset = CGPointMake(480, 360);
}
As you can see, at the end I am calling theTest to zoom into and set an appropriate position to start with (As I don't want to begin with the pdf fitting to the page). This has a delay of 0.2 which is undesirable because the time delay can vary and so the zoom effect may not happen (if load takes longer than 0.2s) or it feels very buggy and jumpy (depending on how much sooner it loads, than the 0.2 delay).
Is there a robust way to zoom in and position my pdf once it is loaded into the UIWebView?
EDIT: -(void)webViewDidFinishLoad:(UIWebView *)webView { does not seem to help either. It prints the NSLog but the zoom does not work. The webview does have its delegate set accordingly.

Why don't you try the QuickLook framework, if I understood well your question could do exactly what you are looking for. See that tutorial

Related

How to load, scroll and pinch and zoom *.svg image in iOS device using Objective -C

In my ios application, I am displaying a *.svg file as below:
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"svg"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:path];
NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
[_webView setScalesPageToFit:YES];
[_webView loadRequest:req];
This works fine as I can load pinch and zoom the file on device. But now I need to highlight and area of image (say a room of a building or well of a garden). How can I achieve this? I have tried SVG Kit, but I was not able to find the touch-highlight area property. I've also used FSInteractiveMapView. But is does not allow me to pinch and zoom or scrol through the svg image. Please help me.

iOS 7 UIWebView inconsistent Loading

In my project I have 2 UIWebViews that are IBOutlets and created in the main.storyboard.
One I load by calling a method on ViewDidAppear. It appears perfectly and hits all it's delegate methods. We will call this ViewA
The other is supposed to be Hidden and appear and load when the user taps a button. We will call this ViewB. But once I submit the request, nothing happens, none of the delegate methods get called.
ViewA loads perfectly. If I try to load ViewB on ViewDidAppear, it also loads.
EDIT: Moving the ViewA and ViewB load method calls to the ButtonPress method calls both successfully.
EDIT: Code
-(void)loadCarouselWebView //ViewA
{
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLPublicKey],[[JSONReader sharedInstance] URLToLoad:kCarouselURLkey]];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.carouselWebView.delegate = self;
[self.carouselWebView loadRequest: requestObj];
}
-(void)loadFullWebView:(int)index //ViewB
{
NSString *gameURL;
switch (index) {
case 1:
gameURL = game1Constant;
break;
case 2:
gameURL = game2Constant;
break;
case 3:
gameURL = game3Constant;
default:
break;
}
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLKey],[[JSONReader sharedInstance] URLToLoad:gameURL]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.fullWebView.delegate = self;
[self.fullWebView loadRequest: requestObj];
}
- (IBAction)gameButtonPushed:(id)sender {
[[self fullWebView] setHidden:NO];
[[self backNavButton] setHidden:NO];
[self loadFullWebView:[sender tag]];
[self loadCarouselWebView];//Added for debugging
}
EDIT: ONLY Loading ViewB on button tap works. If I add Load ViewA on ViewDidAppear then ViewB will not load.
In a surprise twist, there is nothing wrong with the UIWebView.
It was my fault for not reading the code carefully enough.
Someone coded a one time counter into the shouldStartLoadingRequest delegate method that I missed. So only one request was allowed to load.
Thats like 2 days burned there, great stuff! >:E

Loading a UIWebview when pressing a UIButton

I am developing a simple testing app for iPhone. I want to click a button to change the URL of webview, then load the new URL. Here is my code:
NSString *nurl = [NSString stringWithFormat: #"http://www.abc.com/abc.aspx?SN=%#&fdate=%#&tdate=%#", SNstr, fDate, tDate];
NSLog(#"################: %#", nurl);
NSURL *url = [NSURL URLWithString:nurl];
NSLog(#"################: %#", url);
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Logs loadRequest:req];
It is working well inside viewDidLoad.
I move it into a button:
-(IBAction)CheckLog:(id)sender
{
// codes here
}
When I press the button, it gives nurl's value correct, but url's value is null. So nothing is changed on the UIWebView Logs.
Why could this be, when the code runs correctly in viewDidLoad?
your button action will be like this...
-(IBAction)CheckLog:(id)sender
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *nurl = [NSString stringWithFormat: #"http://www.abc.com/abc.aspx?SN=%#&fdate=%#&tdate=%#", SNstr, fDate, tDate];
NSURL *url = [NSURL URLWithString: [nurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
// NSURL *theURL = [NSURL URLWithString:#"https://www.google.co.in/"]; // Your new URL
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
The first thing to check is that the method is definitely being called. Are your NSLog statements being printed? If not, check that you have connected the button to the IBAction correctly in Interface Builder.
I can't see anything in your code that is immediately jumping out to me as incorrect, so your best bet is to set a breakpoint at the start of the method and step through it line-by-line, checking the values of each variable as you go.

ASIHTTPRequest Asynchronous Download not canceling

I am trying to cancel an Asynchronous Download using ASIHTTPRequest with no joy. I am downloading a movie to disk on the appearance of a ViewController. What I want is when the user click close to dismiss the view controller I want to cancel the download. Here is the code I have so far:
Making the request:
-(void)retrieveLatestFile{
NSURL *url = [NSURL URLWithString:[appDelegate.urlToLoad stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ashiRequest = [[ASIHTTPRequest requestWithURL:url] retain];
[ashiRequest setUsername:#"Appidae"];
[ashiRequest setPassword:#"Dubstance1"];
[ashiRequest setDelegate:self];
[ashiRequest startAsynchronous];
[ashiRequest setDownloadProgressDelegate:progressView];
NSLog(#"Value: %f", [progressView progress]);
if ([progressView progress]==1) {
[self hideInfoPane];
}
//NSLog(#"Max: %f, Value: %f", [myProgressIndicator maxValue],[myProgressIndicator doubleValue]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [NSString stringWithFormat:#"%#/download/%#/%#", documentsDirectory, appDelegate.languageFolder,appDelegate.filename];
NSLog(#"Path: %#", tempPath);
[ashiRequest setDownloadDestinationPath:tempPath];
}
Cancel the the downloading:
- (IBAction)closeDocDisplay:(id)sender {
// Cancels an asynchronous request
[ashiRequest clearDelegatesAndCancel];
[self dismissModalViewControllerAnimated:YES];
}
Everything is working, the download is fine and the movie plays properly but when I dismiss the View Controller the request is not canceled and my file is still downloading (network activity indicator spins on the status bar. Even the ASIHTTP delegate error doesn't get called
Try using
ashiRequest.delegate = nil;
[ashiRequest cancel];
This should work.
I figured out what was wrong. I was using Reachability to call my retrieveLatestFileMethod. That's fine if Reachability is being used just once in the app, maybe from the delegate. But I had a few instances of it so it keept calling the same functions and downloads.

Add TapRecognizer and different color to word inside a string in Xcode

I am pulling a text from a plist and draw it into a UIView.
The text contains terms that I want to be highlighted and user interaction enabled so I could pop a dictionary view with the teem explanation.
I know how to find their location on the string and that works fine.
But I did not find how to make them a appear in different color.
And even more important to me - how to add a tap recognizer to them.
I would appreciate any help.
Thanks
Shani
Add a transparent button with the same font and text in front of where the word appears in the UIView. I have a class that will do this with a UILabel if you'd like it? It creates the same effect as with the Facebook application.
O.k
i found a solution that works great for me.
i am using UIWebView as The view for the text and then adds the text formatted as HTML.
in this way i can use CSS to format the text better and even tag some of the text as link ,in the example i want that the word consonant will appear as a link.
(an other great benefit is that I can add support to RTL languishes).
UIWebView *webView =[[UIWebView alloc] initWithFrame:imgViewRect];
webView.backgroundColor = [UIColor clearColor];
webView.opaque=NO;
webView.delegate=self;
NSString *localizedPath = [NSString stringWithFormat:#"rt%#",pcard.card_number];
NSString *localizedString = NSLocalizedString(localizedPath,#"");
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
//do base url for css
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *html =[NSString stringWithFormat:#"<dfn>consonant</dfn>",
currentLanguage,dir,cssPath,langClass,localizedString];
NSLog(#"%#",html);
[webView loadHTMLString:html baseURL:baseURL];
[self addSubview:webView];
then i use this function to recognize what word clicks and lunch a UIView with the dictionary text:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSLog(#"%#",[URL lastPathComponent]); //thats gives me the name of the clicked word.
//here you can set any function that you like.
//using the data from the url path.
}
return YES;
}
hope it will help someone.
shani

Resources