setZoomScale breaks ability to scroll in UIWebView - ios

One of my apps has a UIWebView which loads a particular pdf from the user's device. After the webView has loaded I would like to set the zoom scale and contentOffset. However, if the pdf takes a while to load, the user can no longer scroll - they can see the whole pdf and can zoom in/out, but it only zooms into the top-left corner and you can't scroll at all. (You can scroll if you zoom in further than zoom scale).
(For completeness) I run this code in viewDidAppear:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *path = [documentsPath stringByAppendingPathComponent:#"filename.pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
The main code:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (!loadFailedBool) {
[self performSelector:#selector(zoomIntoWebView) withObject:nil afterDelay:0.0];
}
}
- (void)zoomIntoWebView {
[webView.scrollView setZoomScale:5.0 animated:NO];
webView.scrollView .contentOffset = CGPointMake(600, 420);
}
I have removed any unnecessary code, but as you can see, it runs the zoom function after a delay of 0.0. In this circumstance, the above bug happens every time. If I set it to something like 0.3 the code works fine, about 90% of the time.
As a temporary fix, I have set the delay to 0.6 but it is far from ideal, especially if a user is on a older device. If this bug happens every time, this feature of the app becomes unusable. If anyone has any suggestions or a similar problem it will be great to find a solution. Knowing when the pdf has finished loading would probably fix it.

Related

WKWebView pinch-to-zoom in iOS10

I encountered a problem that after I upgraded my phone to iOS 10 beta 3, when I zoom a WKWebView in my app, the zooming function not only zoom the content of the web page but also the page itself.
See the image I attached. WKWebView that fails to zoom correctly. I set the background color of the WKWebView to blue, when I pinch to zoom out the view, the page(container?) itself also zoomed out, causing unfavorable result. It does not happen in iOS9. When I do the same thing, the blue background does not show.
The web page is written in AngularJS and the Graph lib is d3js.
The related code are as below.
-(void) reloadWebPage{
[activityIndicator startAnimating];
NSString *indexToPath = [self getIndexHtmlPath];
NSURL *fileUrl = [NSURL fileURLWithPath:indexToPath];
NSURL *absoluteUrl = fileUrl.absoluteURL;
if(currentGroup == nil){
currentGroup = #"0";
}
NSString *absoluteString = [[absoluteUrl absoluteString] stringByAppendingFormat:#"%#%#", #"?currentGroup=", currentGroup];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:absoluteString] ];
[webView loadRequest:request];
}
- (void)createWebView{
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self name:#"interOp"];
webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];
[webView setBackgroundColor:[UIColor blueColor]];
[webView setNavigationDelegate:self];
[webView setUIDelegate:self];
}
I have no clue where is the problem? Is it in Objective C,iOS10 or in Javascript?
Please suggest, How can I resolve this problem?
Thank you
Ok, I resolved this problem by disabling zooming and scrolling in WKWebView.
webView.scrollView.scrollEnabled = false;
- (UIView*) viewForZoomingInScrollView:(UIScrollView *)scrollView{
return nil;
}
After these code are added, the WKWebView itself no longer zooming and moving while the nodes move and zoom normally.
It seems the problem somewhat related to Disable magnification gesture in WKWebView, but
I still don't know what caused the change from iOS9 to iOS10. But it seems to work for me.

Using PanGesture with GoogleVR Panorama

I need to display 360 images on iOS devices.
I chose GoogleVR to do this (https://developers.google.com/vr/) and I'm using the GVRPanoramaView. (https://developers.google.com/vr/ios/vr-view)
_panoView = [[GVRPanoramaView alloc] initWithFrame:self.view.bounds];
[_panoView loadImage:[UIImage imageNamed:#"VRTest.jpg"]];
[self.view addSubview:_panoView];
It works well, the image is displayed and I'm able to see all around using the device gyroscope.
I'd like to be able to move across the image using pan gesture but I can't find a way to achieve this using the given SDK.
Is it even possible ? I did not find anything in the documentation saying it's not ... but didn't find any solution either ...
In the end I used the web version of the same framework ... that handle both gyroscope and pan gesture (but only around y axis) ...
https://developers.google.com/vr/concepts/vrview-web
I embedded the framework in my app to have it local (I want to be able to load offline image)
https://github.com/google/vrview
Then I used a simple webview :
UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.bounds];
// Path depends of your bundle
NSString *indexPath = [[NSBundle mainBundle] pathForResource:#"vrview/index" ofType:#"html"];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"VRTest" ofType:#"jpg"];
// load the local framework with the local image
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#?image=%#&is_stereo=false", indexPath, imagePath]]]];
// Disable scroll because of collision with pan gesture
webview.scrollView.scrollEnabled = NO;
[self.view addSubview:webview];

iOS : jump to a specific slide in ppt

I've one ppt file locally that I'm opening with UIWebView with below code, file having 10 slides, now with the requirement, I want to jump to a specific slide, something like if I want to directly jump to slide#3 then how to do it with this code?
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"ppt"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
//open ppt into UIWebView like,
[self loadDocument:#"newone" inView:self.myWebview];
I searched here, and found this, they are doing
Link to http://www.whatever.com/hello.ppt#4 for slide 4 to be opened initially.
but my ppt file is saved into document directory (locally) within my app.
Any suggestion?
You can set the offset of UIWebview ScrollView.
[[webView scrollView] setContentOffset:CGPointMake(widht, height) animated:YES];
Find height of one slide in current zoom scale. Multiply it with four as forth page to open initially. and put in above code with point CGPointMake(0, heightYouCalculated)

Generating thumbnails of local webpages on iOS

I have a set of local HTML pages that I would like batch generate thumbnails for on the fly (I only want to show the thumbnails, not the full web pages). This is the way I'm accomplishing this:
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:link];
NSURL* url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 725, 1004)];
webView.delegate = cell;
[webView loadRequest:request];
[self.view addSubview:webView]; // doesn't work without this line, but then the UIWebView is onscreen
Then in the delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self performSelector: #selector(render:) withObject: webView afterDelay: 0.01f];
}
- (void) render: (id) obj
{
UIWebView* webView = (UIWebView*) obj;
CGSize thumbsize = CGSizeMake(96,72);
UIGraphicsBeginImageContext(thumbsize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scalingFactor = thumbsize.width/webView.frame.size.width;
CGContextScaleCTM(context, scalingFactor,scalingFactor);
[webView.layer renderInContext: context];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.thumbnail.image = resultImage;
}
Here are my questions:
1) Is my general approach correct? Is this the most efficient way to batch process thumbnails of webpages on the fly?
2) I want to be able to render the thumbnail from an offscreen UIWebView, but the webViewDidFinishLoad: doesn't get called unless I add the UIWebView to the view hierarchy. Is there a way I can avoid this?
3) If I attempt to capture an image of the UIWebView in webViewDidFinishLoad:, I get a blank image. I have to put an artificial delay for the capture to work. Any way around this?
Thanks!
1) This seems to be the only way I know of to do this, without relying on a third party server-based API (if one exists)
2) You can draw the UIWebView off-screen by setting its frame to a position that is off-screen. For example 320,568,725,1008. I haven't tested it, but you should even be able to set the view as a 1 x 1px frame.
3) I imagine this is because when webViewDidFinishLoad: is called, a SetNeedsDisplay() has been called on the web view, but the web view has not yet redrawn itself. I'm not sure about a way around this.

ipad orientation problem with webview

I'm creating ipad application. It has just 2 views.
One view has few buttons. On click of a button it will open another view. This view has a webview which shows PDF file. I'm using the following code to show the PDF
- (void)viewDidLoad {
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:#"mypdf" ofType:#"pdf"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
}
Now the problem is that, when I change the orientation, whole view is rotating, but the pdf is being disturbed and not rendering properly. how can I avoid this?
As the PDF is having 250 pages, reloading the whole PDF on willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation will delay the display.
How to avoid this situation.
There has to be a way a better to avoid/fix this, for now i just read the y scroll position with:
int scrollPosition = [[webView stringByEvaluatingJavaScriptFromString:#"window.pageYOffset"] intValue];
Then reloaded the document in:
(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
and then set the scroll position again with:
[webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:#"scrollTo(0,%d)",scrollPosition];

Resources