Allowing user to pinch zoom in/out of a webview - iphone

I have a WebView into which I'm loading a local HTML page with some embedded images. I want to allow the user to zoom in on the text and image in a similar fashion as they would with the web browser.
Any ideas how to enable this?
Thanks

Try to set UIWebView property scalesPageToFit to YES

For completeness. You must enable multitouch, scalesPagesToFit to get it to work (there are some conditions if its within certain other views).
But then to prevent it scaling your webpage (especially if using local files). You will need to add the following snippet to your HTML
For more info so: http://iphoneincubator.com/blog/windows-views/right-scale-for-a-uiwebview

This is the code that works for me:
// declare and allocate UIWebView before
// UIWebView *webView;
// webView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, w, h)];
webView.scalesPageToFit = YES;

I used scalesPageToFit to YES.... it allows it to zoom in some... but not a true zoom like safari has.

Related

How to Zoom UIWebView in iPhone

I am new to this technology,
I am loading webpage in my webview,
I didn't set scalePageToFit property. without using this property i want to do Zooming In and Out on my webview.
is it Possible ?
Here is my Code snippet,
NSURL *url2=[NSURL URLWithString:str1];
NSURLRequest *req2=[NSURLRequest requestWithURL:url2];
[webView1 loadRequest:req2];
[webView1.scrollView setZoomScale:2.0 animated:TRUE];
[webView1 release];
str1 is my url i tried this but, still i am unable to zoom my web view in simulator.
Any help will be appreciated.
Thanks In Advance !
UIWebView is having scrollView as a property. Use ScrollView's delegate method
– scrollViewDidEndZooming:withView:atScale:
This might help you.
Follow this link : This might give you some clue. how to zoom the scrollview.
You need to set scalePageToFit property to YES.

Preloading a UIWebView, avoiding white screen flash

I'm working on an app that has table navigation to eventually drill down to UIWebView that displays various information. However, the transition from the slickness of UITableView to the slow wonkiness of UIWebView is jarring for the user so I want to improve that experience however I can.
Specifically, the background of both the tableViews and UIWebView pages have black backgrounds, but when I open the UIWebView it flashes empty white for about a second (this is the case for both local and remote HTML files.) How can I (ideally) preload this process, or (at least) make the "flash" be all black rather than all white? I tried making the view / webView's background black but that didn't seem to help.
In my app right now, when a user selects a cell, the app just loads up the UIWebView subclass and pushes it on the navigation stack. The UIWebView subclass has an activity indicator that starts & stops animating on WebViewDidStartLoad and WebViewDidFinishLoad, which works fine, but it doesn't do anything to help the "white flash."
I have tested it... I'm sending the two method that I have used...
- (void)viewDidLoad {
[super viewDidLoad]; //objWebView is the outlet of UIWebView
[objWebView loadHTMLString:#"<html><body style=\"background-color:black;\"></body></html>" baseURL:nil];
//the more the delay the errors will be less so within 0.1-0.3 would be fine
[self performSelector:#selector(loadURL:) withObject:nil afterDelay:0.1];
}
-(void)loadURL:(id)sender{
[objWebView stopLoading]; //added this line to stop the previous request
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[objWebView loadRequest:req];
}
here I'm performing the request after 0.1 sec, other wise it will look white as in your case. Or you can give your delay time depending upon the time.. Cheers :)
Try in your
-(void)viewDidLoad{
myWebView.hidden = YES;
Then in
-(void)loadURL:(id)sender{
myWebView.hidden = NO;
I used:
[webView setHidden:YES];
[webView setDelegate:self];
when creating my webView and making the request and then added this delegate method to handle completed requests:
- (void) webViewDidFinishLoad:(UIWebView*)webView{
[webView setHidden:NO];
}
Successfully done it.
You have to create a view in Interface Builder first.
Then load the html to the webview using a initWithFrame in the init of your ViewController that contains the webview(this is where the magic happens):
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webView = [[UIWebView alloc] initWithFrame:webFrame];
Then simply load the webView into the view in viewWillAppear:
[viewWeb addSubview:webView];
This is really a question of interface designing, which is faster paint directly on the view or paint in a subview and then paint that subview in the view?
I had solved this problem years ago, using the common method of hiding the UIWebView behind a UIImageView, then removing the UIImageView after a delay.
But it suddenly came back, I think on iOS 7.0.4. It was occurring on a brand new iPad Air, as well as an older iPad mini non-retina. After two days of hair-pulling, I finally found a work-around.
Let's say you have webview which is restricted to landscape orientation, initialized like this:
WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024.0f, 768.0f)];
And then you make it visible after pre-loading, with eg bringSubviewToFront or setHidden:NO on the webview (or alternatively with setHidden:YES or removeFromSuperview on the UIImageView). But instead of seamlessly switching the views, there's a flash and the background color blinks for about half a second.
The fix is to change the size of your webview, ever so slightly:
WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024.01f, 768.0f)];
The problem and fix is highly reproducible. It works with a change as slight as the fourth decimal place (1024.0001f). At the fifth decimal place (1024.00001f), the flash returns. The value of the height (768.0f) didn't seem to matter.
Or, instead of working around the problem, you could just set the background color of your webview to whatever background color you're using. Unless you're using an image of course.

How do you create and manage a UIWebView object entirely in code?

I'm adding a UIWebView to my iOS App and I'd like it to open in response to a button getting clicked (so this code is going to be written in one of the button's event handler).
How can I create this UIWebView dynamically in code, position it to cover the entire screen and respond to events (e.g. to UIWEbView's shouldStartLoadWithRequest function so that the UIWebView can ask the native code to close the UIWebView).
I'd specifically like to avoid having to create stuff in Interface-Builder and it would be great if this could be reduced to several lines of code that could be later copy-pasted into other projects easily.
Simple:
UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];
webView.delegate = self;
[self.view addSubview:webView];
You just need to implement the WebViewDelegate's method in the controller, that you are making the delegate of the UIWebView.

iPhone dev: Increase scroll speed in UIWebView?

I have an app in which I render local HTML files in a UIWebView. The files, however, are sometimes large, and getting to where you want takes a long time with the default scroll speed. Is there any way to boost up the vertical scroll speed of a UIWebView?
In iOS 5 we can access the scrollView property of the UIWebView.
If you are targeting iOS 5+, you can simply call:
webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
Find a subview of UIWebView which is a UIScrollView, then set decelerationRate to UIScrollViewDecelerationRateNormal. This makes the UIWebView as fast as an ordinary UIScrollView.
In iOS 4/5, we can simply use the last subview of UIWebView.
UIScrollView *scroll = [webView.subviews lastObject];
if ([scroll isKindOfClass:[UIScrollView class]]) {
scroll.decelerationRate = UIScrollViewDecelerationRateNormal;
}
The default decelerationRate of UIWebView's UIScrollView is 0.989324, while UIScrollViewDecelerationRateFast is 0.99, and UIScrollViewDecelerationRateNormal is 0.998.
This method doesn't use any private API.
Search for a subview of UIWebView that responds to -setScrollDecelerationFactor: (it's UIScroller - a private class that's the only subview of UIScrollView). You'll find that it takes the same deceleration factors defined for the public UIScrollView class:
- (void)webViewDidFinishLoad:(UIWebView *)aView {
id decelerator = [aView viewWithSelector:#selector(setScrollDecelerationFactor:)];
[decelerator setScrollDecelerationFactor:UIScrollViewDecelerationRateNormal];
}
Note that the method I'm using viewWithSelector: is a method I defined in a category of UIView. Presumably, if UIWebView changes in future, my search will return nil and this method will become a no-op.
Have considered adding # tags into your html on significant boundaries?
You could actually use native UI to implement bookmarks or a ToC for easier navigation, or simply embed links to the appropriate targets right in your html.
If you 'speed up scrolling' your app is at risk of rejection for being non-standard, since it may confuse users who are used to webviews scrolling with a standard 'feel'.
Old question but these setting or trick really helped me even in 2018.
Follow these simple coding tricks to improve Android WebView Performance:
WebView mWebView = new WebView(this);
WebSettings settings = mWebView.getSettings();
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(false);
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

MPMoviePlayer Audio Album art

I am streaming an MP3 file using MPMoviePlayer, and I would like to have an image displayed instead of the default Quicktime logo in the background.
I found out a way to have an image overlay the player, but the problem with that, is you have to tap outside the image to get the player controls to appear. And when they do appear, they are underneath the image.
Does someone know how to do this?
Thanks,
John
backgroundColor is deprecated, and diving into private View structures is dangerous. This worked fine for me:
UIImageView *coverImageView = [[UIImageView alloc] initWithImage:coverImage];
coverImageView.contentMode = UIViewContentModeScaleAspectFit;
coverImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
coverImageView.frame = moviePlayerController.view.bounds;
[moviePlayerController.view addSubview:coverImageView];
Most every app on the iPhone is made of a hierarchy of views. You could go up to the top root node of the movie player and walk down the child views recursively and set the hidden value to YES until you find the right item, then insert your UIImageView below that item. That way, the controls stay on top and still respond to user inputs.
The risk you run is if Apple changes the MPMoviePlayer UI and shuffles the view hierarchy, but you'll probably have lots of advance notice and can patch your app to allow for the new layout.
There is a question as to whether this is kosher for appstore apps, but many current apps (especially camera/picture-taking ones) are doing it to get rid of standard window elements.
Use AVAudioPlayer and implement your own player UI.
it will work check this
MPMoviePlayerController *moviePlayerController=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
moviePlayerController.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Default.png"]];