Webview content not resizing - iphone

I have a webview.. with the following attributes:
1.scalesPageToFit
2.Autoresizing masks
3.contentmode = aspectFit
After loading it fits to the page as obvious. But as soon as i zoom in and then zoom out to normal; rotating the device doesn't fit completely.
As a tweak, i have checked if the orientation mode is landscape reload the web page. I read various posts in this regard, but couldn't find any solution.

You have 2 folowing options:
Add this into head section of your html file:
<meta name="viewport" content="width=device-width" />
Or call [myWebView reload] when orientation changes

You can use this Code
NSString *website =#"http://www.google.com";
NSURL *url = [NSURL URLWithString:website];
NSURLRequest *requestURL =[NSURLRequest requestWithURL:url];
mywebview.scalesPageToFit = YES;
mywebview.autoresizesSubviews = YES;
[mywebview loadRequest:requestURL];

It took more than 2 days to find out the reason causing this issue. Actually webview was working correctly except the fact that if you try to use PinchGesture to zoom out further.. webview will baheve in the same way.
To overcome this issue, we need to set the zoom scale of scroll view in Webview under willRotate method. However, it's animating weirdly but one can make a guess where the issue lies.

Related

How to Load a WebView pre-zoomed in? (In Xcode / Objective C)

I am loading a UIWebview of a hosted .jpg. It's actually a schedule, so it is a rather large image. Instead of having users have to zoom in right away, I would like to load the web view already zoomed in. Although I still need the user to be able to zoom in and out, and scroll. Basically I am just looking for an "initial" zoom. How would I accomplish this? Just FYI I put the method below I am using to load the image...Thanks!
// Webview code
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webview loadRequest:requestObj];
In iOS 5 you can access the UIWebView's scrollView property and set the zoom level on it after the page has loaded. That would likely work as you wanted. To get the same thing on pre-iOS 5 you'd probably want to go through the UIWebView's subviews until you find a UIScrollView and do the same thing on that.
I don't think there's any other way to programatically zoom it. Unless you can do it with Javascript and execute some using UIWebView's stringByEvaluatingJavaScriptFromString: method.

How to show TXT or ASCII file into UIWebView correctly

I'm trying to show TXT file (ASCII) into UIVewView. For example, using the site www.partisani.net/35.txt in Safari on MacBook works fine, Safari on iPhone doesn't. Safari's iPhone shows the file with another layout. Could someone help me, please?
As far as I can tell, the layout of the file on the Mac vs iOS is exactly the same. Are you talking about text wrapping? You can see that on the Mac by resizing the browser.
If you want to handle the line length differently you'll need to do so by setting up scrolling.
UPDATE with more detail:
This "sort of" changes the original content :). Basically, you need to tweak both the contentSize of the webview and embed the text file in some boilerplate HTML to reflow the text rather than have the default viewport width assigned to the text document. The latter I accomplish with something like:
NSURL* url = [NSURL URLWithString:#"http://www.partisani.net/35.txt"];
UIWebView* vw = (UIWebView*)self.view;
vw.delegate = self;
NSData* Data = [NSData dataWithContentsOfURL:url];
NSString* aStr = [[NSString alloc] initWithData:Data encoding:NSASCIIStringEncoding];
NSString* responseStr = [NSString stringWithFormat:
#"<HTML>"
"<head>"
"<title>Text View</title>"
"</head>"
"<BODY>"
"<pre>"
"%#"
"/pre>"
"</BODY>"
"</HTML>",
aStr];
[vw loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[aStr release];
The vw.delegate = self is important, as you also need to have your controller conform to the UIWebViewDelegate protocol and implement the webViewDidFinishLoad: method. There you can set the scroll width and height of your webview as needed:
- (void) webViewDidFinishLoad:(UIWebView *)webview {
UIScrollView* sview = (UIScrollView*)[[webview subviews] objectAtIndex:0];
sview.contentSize = CGSizeMake(1000, 800);
}
This is an extremely barebones implementation--presumably you would also want logic to calculate the necessary width and height based upon the loaded text rather than use constants as shown here; you'll need some parsing logic associated with the original data for that, but this should get you started.
Setting the scalesPageToFit property to yes might fix your problem.
I'm not sure this is actually something to do with TXT or ASCII, but rather the UIWebview resizing the content.
The file you mentioned loaded into a landscape ipad screen has exactly the same layout as on Mac Safari:
You can change whether or not the UIWebView scales its content with the scalesPageToFit property.

UIWebView keeps zooming on iOS 5

I have a older app where in a UIWebView I loaded a movie from a URL. It worked just fine. But now on iOS 5 ... when I enter the ViewController containing the UIWebView ... the thumbnail of the video starts "growing" like someone would zoom on it and it doesn't stop.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.link_go_here"]];
[infoWebView loadRequest:request];
(it's a universal app ... the same happens on both devices)
Have you set the infoWebView.scalesPageToFit = NO; property of the webview? You could also try to set
<meta name='viewport' content='initial-scale=1.0,maximum-scale=1.0'/>
of the html page your loading.

uiwebview youtube starting in landscape thumbnail does resize on rotation

I have navigation controller with a tableview. When you click on one of the cells it pushes on a view with a uiwebview on it. You are taken to a YouTube page.
When you are on the table view in portrait and click on a cell you see the youtube page in portrait. Changing your orientation the video thumbnail does not refresh. So the thumbnail is smaller. This is fine. I actually prefer it smaller. All the content that would consume 2 lines will then consume 1 line. So in other words everything else adjusts for the new dimensions.
The problem comes in when you start off in landscape. Since the thumbnail doesn't resize on orientation change, changing to portrait mode, the image now goes off the screen, while the rest of the content adjusts correctly.
[webVIew refresh];
does work but it obviously loads the entire page again. So depending on the connection there will be a flicker or possibly the site will go white until its finished loading (on slower connections).
I also tried load the website in an iframe. I asked a similar question yesterday, this was for local pages i was creating. The answer to that question was to put <meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'> in the head. So I tried that with an iframe going and getting the page. That seemed like a dumb hack to begin with, but i was willing to go there. It wouldn't even load the page at all. I guess because the youtube page i'm loading redirects to yet other page. Upon further research it seemed like there were other issues with the iframe such as scrolling.
So my question is how can i:
A. Get just the thumbnail to resize/reload on orientation change
OR
B. Get the thumbnail to load in the dimensions it would load in portrait mode all the time, even if it was started in landscape.
You should override this method in your UIViewController class and do the resizing there:
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
I found the answer, maybe someone could explain it or offer a better solution. There was also an addition bug that I had to work out.
I made sure the uiwebview had the delegate set to files owner.
I changed the parent view when it pushed the view onto the form from:
[self.navigationController pushViewController:controller animated:YES];
to (the important part being the 320, i'm restricting the view to portrait):
[self.navigationController pushViewController:controller animated:YES];
controller.webView.hidden = YES;
controller.webView.frame=CGRectMake(0, 0, 320,367);
3.On the webview i load the url as i always did:
NSURL *url = [NSURL URLWithString:sUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
4.In the webviewdidfinishload i now have:
if (self.interfaceOrientation ==
UIInterfaceOrientationLandscapeLeft ||
self.interfaceOrientation ==
UIInterfaceOrientationLandscapeRight) {
self.webView.frame=CGRectMake(0, 0, 480,227);
}
self.webView.hidden = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
All this would normally have solved my issue, but then i found out that the gdata url i'm getting from youtube service actually gets resolved to a 2nd url. Then i guess youtube changed their url format so it is forwarded to a 3rd url. This means that my didfinishload code was being called before the final url had loaded. To solve this i added:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[NSString stringWithFormat:#"%#",request.URL] substringWithRange:NSMakeRange(0, 26)] isEqualToString:#"http://m.youtube.com/watch"]) {
NSString *sUrl=[NSString stringWithFormat:#"%#",request.URL];
sUrl = [sUrl stringByReplacingOccurrencesOfString:#"http://m.youtube.com/watch?" withString:#"http://m.youtube.com/#/watch?"];
NSURL *url = [NSURL URLWithString:sUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
return NO;
}
//NSLog(#"request:%#",request.URL);
return YES;
}
Which im sure could be better but basically i tell it to ignore the 2nd url, and make my own change to go to the 3rd url.

UIWebview won't zoom even after setting scalesPageToFit to YES

I have a UIWebview that is loaded as a subview when a user selects a tab on a UISegmentedControl. For some reason, I can not get it to allow pinch/zooming.
I set the following code in the viewDidLoad: method, so it should work.
self.myWebView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: myWebView];
I tried loading a UIWebView from a NIB and creating it programmatically with no avail. Is there something I'm missing? What could be causing the webview to ignore pinching and zooming?
Thanks!
[webView stringByEvaluatingJavaScriptFromString:#"document.body.style.zoom = 5.0;"];
seem to be the suitable solution
I solved this with setting a view port:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>
Good luck
I see you are setting the autoresizingMask. Does that mean you have created the UIWebView with an initial size of CGRectZero ? Can you interact with the document at all? I mean, does scrolling/tapping work?
In my experience, you need to set scalesPageToFit before the UIWebView loads. This means setting before viewDidLoad etc. What I do is set it in "shouldStartLoadWithRequest"
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//make sure that the page scales when it is loaded :-)
theWebView.scalesPageToFit = YES;
return YES;
}
My interpretation of the documentation is that the scalesPageToFit property dictates how the page WILL be loaded. It does not alter things after the fact.
Hope this helps.
FYI:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>
works but
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=YES"/>
does not on the iPhone. The documentation says to use yes/no. I guess case matters in this case. In obj-c the values are YES/NO and 1/0
You have to enable multi-touch. Pinch involves more than one finger on the screen:
[myWebView setMultipleTouchEnabled:YES]
This is what Apple suggests in the webview class reference.
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.