I have a webView and when the content initially loads it looks perfect and fits nicely but as soon as it completes loading it automatically zooms way in. Is there a way to stop this? Ive tried more combinations than I can possibly post here.
Many Thanks.
The Code I'm using is pretty straight forward:
NSString *urlString = #"some_url";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[cell.webView loadRequest:request];
Here's what you do:
In your UI controller that owns the web view, make it a UIWebViewDelegate. Then where you set the URL to load, set the delegate as the controller:
NSString *urlAddress = #"http://dl.dropbox.com/u/50941418/2-build.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
And finally implement the webViewDidFinishLoad: to correctly set the zoom level:
This option will applicable from iOS 5.0 and >
- (void)webViewDidFinishLoad:(UIWebView *)theWebView{
CGSize contentSize = theWebView.scrollView.contentSize;
CGSize viewSize = theWebView.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
}
I hope this helps...
Option B, you can try to alter the HTML (this example does the job but is less than perfect from an HTML parsing standpoint. I just wanted to illustrate my point. It does work for your example, and probably most cases. The inset of 40 can probably be detected programmatically, I didn't try to research that.
NSString *urlAddress = #"http://dl.dropbox.com/u/50941418/2-build.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSString *html = [NSString stringWithContentsOfURL:url encoding:[NSString defaultCStringEncoding] error:nil];
NSRange range = [html rangeOfString:#"<body"];
if(range.location != NSNotFound) {
// Adjust style for mobile
float inset = 40;
NSString *style = [NSString stringWithFormat:#"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
html = [NSString stringWithFormat:#"%#%#%#", [html substringToIndex:range.location], style, [html substringFromIndex:range.location]];
}
[webView loadHTMLString:html baseURL:url];
Related
Am displaying a Image on a web view and it doesn't fit the web view completely and shows blank white space around the border.How can i fit or scale the image completely to the size of web view?
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.backgroundColor=[UIColor blackColor];
self.webView.delegate = self;
[self.webView setScalesPageToFit:YES];
Try this html code snippet
<html><body><img src='%#' width='100%' height='100%'></body></html>
It will fill the web view completely without any white space.
try to load like this
- (void) showImageWithLoadHTMLString {
// Create URL string for image file location
NSURL *imageURL = [NSURL fileURLWithPath: path];
// Create HTML string from image URL
NSString *htmlString = #"<html><body><img src='%#' width='900'></body></html>";
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL];
// Load image in UIWebView
self.webView.backgroundColor=[UIColor blackColor];
self.webView.scalesPageToFit = YES;
[self.webView loadHTMLString:imageHTML baseURL:nil];
}
I have an application at the moment that basically opens a url depending on what the user has typed into a textbox. The logic for this is as such:
Predetermined beginning + User Input + Predetermined End
So basically my URL is 3 concatenated strings. Now I know the link is being formed properly (I've put the same snip of code into a label) but nothing happens when I press the button to load the webview.
It works perfectly fine when I use the below, where I explicitly type https://google.com
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = #"https://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
However nothing happens when I use this code which includes my concatenated url:
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
Any help would be much appreciated.
UPDATE: I'm fairly sure having the stringWithFormat is causing this problem just unsure how to get around it.:
Check if fullURL starts with http:// or https://, if not prefix that.
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
if ( ! ([fullURL hasPrefix:#"http://"] || [ fullURL hasPrefix:#"https://"]) ) {
fullURL = [NSString stringWithFormat:#"http://%#", fullURL ];
}
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
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];
}
In my app, I have a view controller that contains a UIWebView. This View Controller has the UIWebView as an IBOutlet. When loading the view I just get a white screen with my navigation bar at the top. Here is the code for loading the UIWebView. Originally my completeURL wasn't being formatted correctly and was just null. No information I can scour through online seems to be able to help me with this issue.
-(void) viewWillAppear:(BOOL)animated
{
NSString *stringURL = [NSString stringWithFormat:#"www.ncbi.nlm.nih.gov/pubmed/?term=%#", _URL];
NSURL *completeURL = [NSURL URLWithString: [stringURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:completeURL];
[_webView loadRequest:request];
NSLog(#"%#", request);
}
once check this line may be problem is here replace this once and check it,
NSString *stringURL = [NSString stringWithFormat:#"http://www.ncbi.nlm.nih.gov/pubmed?term=%#", _URL];
I think the problem could be in:
NSURL *completeURL = [NSURL URLWithString: [stringURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
check intermediate strings
I'm trying to run a web form on an ipad that has no web connection. So far i've successfully been able to load the form into safari through the use of an app that takes a html file and displays it in a browser.
However, the data store sql component of html 5 just doesn't seem to work. I copied the code from html5rocks.com
http://playground.html5rocks.com/#async_transactions
Seems to work everywhere but in my application.
So my app is very simple, it just loads a web browser... here's the relevant code
- (void)viewDidLoad {
// Example 1, loading the content from a URLNSURL
//NSURL *url = [NSURL URLWithString:#"http://dblog.com.au"];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[webView loadRequest:request];
NSString *webPage = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *webData = [NSData dataWithContentsOfFile:webPage];
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
// NSString *HTMLData = #"
// <h1>Hello this is a test</h1>
// <img src="sample.jpg" alt="" width="100" height="100" />";
// [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadHTMLString:webPage baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:webPage isDirectory:NO]]];
NSString *baseStr = [NSString stringWithFormat:#"file:/%#//",imagePath];
NSURL *baseURL = [NSURL URLWithString: baseStr];
NSLog(#"%#",baseStr);
//webView.scalesPageToFit = YES;
//webView.multipleTouchEnabled = YES;
/*for (id subview in webView.subviews){
if([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
}*/
[webView loadData:webData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseURL];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.cyberdesignworks.com.au/clients/ideatoaction"]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Test" ofType:#"svg"]isDirectory:NO]]];
[super viewDidLoad];
The problem was that Xcode tries to compile my js files.
DAMN YOU APPLE!
anyway look on the right in the list of items in xcode when you have a project open
look for targets, it has a picture of a bow and arrow target, or like a red and white dart board. open that up, look at compiled resources. Move any js files from there to the copy bundled resources.
Bam! it should all work as intended now.