UIWebView content loading at wrong position - ios

I am trying to add three UIWebView to my iOS app. I have successfully read the "HTML code" into the UIWebView and it displays the content fine, but one of the three views are displayed at a wrong position.
Here is the code I used to load the data:
-(void) webViewLoading {
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"load" ofType:#"html"];
NSString* embedHTML = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.choice_1_webview loadHTMLString: embedHTML baseURL: nil];
[self.choice_2_webview loadHTMLString: embedHTML baseURL: nil];
[self.choice_3_webview loadHTMLString: embedHTML baseURL: nil];
}
Here is what I got in the simulator:
and here is what I have in Xcode:
The first UIWebView does not load correctly and the grey rectangle is the background color of the UIWebView. Does anyone know what could have caused this? I created the three UIWebViews exactly the same (copied and pasted in the interface builder).
===
It looks like the content of the first UIWebView has a smaller height than the other two. It is weird however because the contents are generated from the same file.

Related

How to get a video to play in UIWebView (video is in a ALAsset group)?

Okay maybe the headline is somehow disturbing, I hope to state this so you can understand my problem.
So I have a scrollview which has paging enabled. I want to build something like iOS Photo App.
I load the photos and videos which are on the device via ALAsset and store them in a group.
Now i want to present each object on a separate subview of the scrollview.
For the pictures it is working fine. But for videos I cant make it work.
Here is what i did and what did not work:
First:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString *embedHTML = [NSString stringWithFormat: #"<html><head><style></style></head><body>\
<video id='video_with_controls' width=\"%0.0f\" height=\"%0.0f\" controls>\
<source src='%#' type='video/MOV'/></video><ul>\
</body></html>", self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height, [assetRepresentation url]];
[webView loadHTMLString:embedHTML baseURL:nil];
then:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString* embedHTML = [NSString stringWithFormat:#"<video controls> <source src=\"%#\"> </video>", [assetRepresentation url];
[webView loadHTMLString:embedHTML baseURL:nil];
Also I tried to store it in NSData first and then load it from there, but that causes memory problems and wont work.
Any help is highly appreciated!
Edit: As I think UIWebView is the thing that matches the iOS App visually at most, I want to use this and not for example the MPMoviePlayer .. if I am wrong there I would be glad to know how / what else looks like the original App
I guess the video url is local thats why its not working in UiWebView
so the solution would be this:
in delegate method webviewdidstartload,set up MPmovieplayer controller to play inside a UiView and yes it will look like original app as you will be playing it in a view
something like this:
Add MPMoviePlayerController's view to a UIView
When you movie file is located locale you must set the baseURL to your MainBundle and put the full path in the "src"-tag.

How to change embed video size while playing in UIWebView?

I have a UIWebView with size 250x160, which should play video from youtube.com.
NSString *videoURL = #"http://www.youtube.com/v/Y4Y_a45Bv20?version=3&f=videos&app=youtube_gdata";
NSString *videoHTML = [NSString stringWithFormat:#"\
<html><body>\
<embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%f\" height=\"%f\">\
</embed>\
</body></html>", videoURL, self.web.frame.size.width-20, self.web.frame.size.height-20];
[self.web loadHTMLString: videoHTML baseURL: nil];
While playing video I want to resize the UIWebView. When I make it, for example 2 times bigger, embed video stays how it was 250x160. How can I resize it without restarting it?
I found anoyingly easy solution :
NSString *videoHTML = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"98%%\" src=\"http://www.youtube.com/embed/Y4Y_a45Bv20?showinfo=0\" frameborder=\"0\"></iframe>"];
The key is in width=\"100%%\" and height=\"98%%\". I didn't set height to 100%%, because while playing video, it's strangely increases and after 10-20 seconds you need to scroll uiwebview to find where video is gone.
And don't forget set UIWebView scalePageToFit = YES;
try affineTransform.
webview.transform = CGAffineTransformMakeScale(2, 2);
or
webView.scalesPageToFit=YES;
I found this approach and it worked for me:
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
Auto-resize iOS WebView Helper

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)

iOS 5 : bug with loadHTMLString with style

I have these lines of code:
NSString *html = [NSString stringWithFormat:
#"<html><body><p \"style=\"font-size:%d; text-align:center; color:red; \
direction:rtl;\">%#</p></body</html>", fontSize, thePage];
mailBody.text = thePage;
[self.page loadHTMLString:html baseURL:nil];
Styling didn't take any effect at all. So, direction isn't set, color is black instead of red and the font-size isn't set to the number I put in the variable. Yet, it's working in pre iOS 5.
There is a problem in your HTML
USE THIS
NSString* html = [NSString stringWithFormat:#"<html><body><p style=\"font-size:%d;text-align:center; color:red; direction:rtl;\">%#</p></body></html>",fontSize,thePage];

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