stringByEvaluatingJavaScriptFromString recognize webview content height - objective-c

i placed a Uiwebview in a UIscrollview because after the uiwebview i want to display some images with the PageController.
i'm using this line of code.
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (webView!=WebView)return;
float h;
NSLog(#"web view is %f high", WebView.frame.size.height);
NSString *heightString = [WebView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"body\").offsetHeight;"];
NSLog(#"web content is %# high",heightString);
h = [heightString floatValue] +12.0f;
WebView.frame = CGRectMake(WebView.frame.origin.x, WebView.frame.origin.y, WebView.frame.size.width, h);
h = WebView.frame.origin.y + h + 20;
[ScrollView setContentSize:CGSizeMake(320, h)];
}
But Web view is %f high doesn't work, it does not reconize the height of the WebView does anyone know how i can fix this? Is this because i'm using [WebView loadHTMLString:html baseURL:nil];
And: NSString *heightString = [WebView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"body\").offsetHeight;"];
NSString *html = [NSString stringWithFormat:#"<html><head><!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><style>body { font: normal 13px helvetica; color: #565656; } </style></head><body><p>%#</p></body></html> ",text];
[WebView loadHTMLString:html baseURL:nil];

Try
NSString *heightString = [Bericht stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
Good luck,
Nathan

UIWebView already contains a scrollView, why are you adding it as a subview again. Check Apple's documentation here

Related

UIWebView loads HTML from local file but fails occasionally

I've come across many ways to load and reload the content of a UIWebView from a local html file but it seems like the issue I have keeps coming back.
On my project I have a webView that dynamically loads HTML content pulled from one of the 50 different HTML files I have in my XCode project.
Depending on which button the user presses on the viewController before, a different HTML file is used to reload the content of my webView. Everything works fine most of the time but every now and then, the webView will simply display "(null)", after the webViewDidFinishLoad delegate method is called.
I don't understand what is going on, and when I go back and forth on this viewController and the webView is reloaded the HTML content ends up showing up properly eventually for the same HTML file.
Here is the code to load the HTML in the webView (called from the viewWillAppear method) :
- (void) reloadWebViewFromLocalFile{
// Reset the UIWebView
[self.contentWebView removeFromSuperview];
self.contentWebView = nil;
self.contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
[self.contentWebView setContentMode:UIViewContentModeScaleAspectFit];
[self.contentWebView setDelegate:self];
[self.contentScrollView addSubview:self.contentWebView];
[self.contentWebView release];
NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%d.%d",self.currentIndexPath.section+1,self.currentIndexPath.row+1] ofType:#"html"];
NSString *CSSContent = [NSString stringWithFormat:#"<style type=\"text/css\">body{padding-left:8px;padding-right:8px;font-family:\"%#\";}p{text-align:justify;}img{max-width:300px;display:block; margin:auto;}strong{font-family:\"%#\";}h1{font-size:20;font-family:\"%#\"}h2{font-size:18;font-family:\"%#\"}h3{font-size:17;font-family:\"%#\"}</style><script type=\"text/javascript\">touchMove = function(event) {event.preventDefault();}</script>", FONT_STAG_BOOK, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD];
self.HTMLString = [NSString stringWithFormat:#"<html><head>%#</head>%#</html>",CSSContent,[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]]];
[self.contentWebView loadHTMLString:self.HTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}
and this is the webViewDidFinishLoad delegate method :
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *stringContent = [self.contentWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
float height = [[self.contentWebView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"] floatValue] + 20;
[self.contentWebView setFrame:CGRectMake(0, 0, 320, height)];
[self.contentScrollView setContentSize:CGSizeMake(0, height+self.nextButton.frame.size.height)];
[self.nextButton setFrame:CGRectMake(0, height, 320, self.nextButton.frame.size.height)];
if ([self.contentWebView respondsToSelector:#selector(scrollView)]) {
self.contentWebView.scrollView.scrollEnabled = NO; // available starting in iOS 5
} else {
for (id subview in self.contentWebView .subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).scrollEnabled = NO;
}
}
Edit : Forgot to copy-paste the line that actually loads the HTML string in the webView
The problem seems to be using the bytes function, which doesn't work too well if the content encoding isn't exact...
[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]];
should be
[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileName] encoding:NSUTF8StringEncoding];

add UIWebView programmatically to UIImageView

I would like to be able to add zoom functionality to UIImage and believe the best way is to embed within UIWebview. Would someone be able to help me achieve this?
- (void)viewDidLoad
{
[super viewDidLoad];
// image
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 70, IMGSIZE.width, IMGSIZE.height)];
[self.imageView setClipsToBounds:YES];
self.imageView.layer.borderColor = kITBarTint.CGColor;
self.imageView.layer.borderWidth = 1.0;
self.imageView.layer.cornerRadius = 15.0;
self.imageView.layer.shadowColor = [UIColor blackColor].CGColor;
self.imageView.layer.shadowOffset = CGSizeMake(5, 5);
self.imageView.layer.shadowRadius = 10;
[self.controller.view addSubview:self.imageView];
// activity indicator
CGRect frame = CGRectMake(roundf(IMGSIZE.width / 2) + self.imageView.frame.origin.x, roundf(IMGSIZE.height /2) + self.imageView.frame.origin.y, 0, 0);
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_activityIndicator setFrame:frame];
[_activityIndicator startAnimating];
[self.controller.view addSubview:_activityIndicator];
}
Try adding this to your viewDidLoad method,
scrollView.minimumZoomScale = 0.4;
scrollView.maximumZoomScale = 4.0;
scrollView.delegate = self;
[scrollView setZoomScale:scroll.minimumZoomScale];
Hope this helps!!
And try using a UIScrollView
You don't need to Use a UIImageview additionally.
For Local files use file:// as url. you can get a path of a file width:
NSString *path = [[NSBundle mainBundle] pathForResource:#"myimage" ofType:#"jpg"];
For the Webview simple create a Html Code as String.
[webView loadHTMLString: [NSString stringWithFormat:#"<html><head></head><body><img src=\"file://%#\"></body></html>",path] baseURL:nil];
Inside the HTML String you can also use CSS for sizing or positioning!

Objective-C – UIWebView bug found?

I'm setting my UIWebView's size to be 276 pixels wide.
Then I load some html in it and the width of UIWebView is no longer 276 pixels.
I setup the UIWebView as following:
// Setup Career about WebView
r.origin.x = 22;
r.origin.y = kWebViewYPos;
r.size.width = 276;
r.size.height = 1;
UIWebView *tmpAboutWebView = [[UIWebView alloc] initWithFrame:r];
[self setSeminarAboutWebView:tmpAboutWebView];
[tmpAboutWebView release];
[[self seminarAboutWebView] setAutoresizesSubviews:YES];
[[self seminarAboutWebView] setUserInteractionEnabled:YES];
[[self seminarAboutWebView] setTag:999];
[[self seminarAboutWebView] setDelegate:self];
self.seminarAboutWebView.layer.borderColor = [UIColor redColor].CGColor; //Debugging
self.seminarAboutWebView.layer.borderWidth = 1.0; //Debugging
// Load the description for the selected seminar
NSString *htmlString = [[NSString alloc] initWithString:[[self seminar] about]];
DLog(#"%#", htmlString);
NSString *myDescriptionHTML = [NSString stringWithFormat:#"<html> \n"
"<head> \n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"career.css\" /> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", htmlString];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.seminarAboutWebView loadHTMLString:myDescriptionHTML baseURL:baseURL];
[htmlString release];
What could be causing this behaviour? Could the HTML contain some tags that make the UIWebView go bananas?
I'm building with iOS 4.3 SDK.
Link to html code
EDIT: I actually found the bug, I believe it was a bug in my own code not in UIWebView. I am dynamically setting the height of the UIWebView depending on how much HTML is loaded in the UIWebView. For this purpose I was using this code:
// Change the height dynamically of the UIWebView to match the html content
CGRect webViewFrame = webView.frame;
webViewFrame.size.height = 1;
webView.frame = webViewFrame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
// Making sure that the webView doesn't get wider than 276 px I needed to add the below line and now it works
webViewFrame.size.width = 276;
webView.frame = webViewFrame;

How to jump pdf page in UIWebView?

I'm trying to scroll the pdf file by page
here is my code
-(void)viewDidLoad{
NSString *path = [[NSBundle mainBundle] pathForResource:#"myPdffile" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[webview loadRequest:urlrequest];
webview.scalesPageToFit = YES;
[super viewDidLoad];
}
//goto next page
-(IBAction)nextpage{
[webview stringByEvaluatingJavaScriptFromString: ?????? ];
}
//goto previous page
-(IBAction)prevpage {
?????
}
what should i put after stringByEvaluatingJavaScriptFromString?
do i put same code in "prevpage"?
thanks!
In iOS 5 you can use this statement for page jumping
[[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];
and in below iOS 5 you can use this statement
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, %f)", y]];
Where y is the height where you're going to jump.
Here i am writing some sample code for next and previous page
-(IBAction) nextPage: (id) sender
{
if (currentPage < numberOfPages)
{
float y = pageHeight * currentPage++;
if (isIOSAbove4)
{
[[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];
}
else
{
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, %f)", y]];
}
}
}
For previous page:
-(IBAction) prevPage: (id) sender
{
if (currentPage - 1)
{
float y = --currentPage * pageHeight - pageHeight;
if (isIOSAbove4)
{
[[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];
}
else
{
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, %f)", y]];
}
}
}
in above sample code isIOSAbove4 is defined in pch file
#define isIOSAbove4 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
Hope above code will solve your problem
I dont believe there is a way to do this, in other web browsers/PDF viewers you could use the querystring parameter to ?page=6 but that doesnt appear to be supported by the WebKit PDF engine
I have faced a similar problem, in which I had to navigate to particular page in PDF (which is displayed using UIWebView) on selection of a cell in tableView (thumbnail tableview).
So I wrote a piece of code which allows you to scroll to particular page in PDF. This works even if the PDF is in zoomed state. You can find the code here.

How to show HTML5 enabled videos in uiwebview?

I have to show HTML5 enabled videos in my iphone application using the uiwebview. How to resolve this problem? Any one having idea please give with example code.
My code to show the video:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Movie-1" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movie_obj = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[movie_obj.view setFrame:CGRectMake(0,0,320,460)];
movie_obj.scalingMode=MPMovieScalingModeAspectFit;
[self.view addSubview:movie_obj.view];
[movie_obj play];
I'm quite sure it should support this by default. The main problem would be the HTML part.
But what doesn't work? Because seeing your tags, i think you're not the one making the site, but the UIWebView simply won't show the video?
Edit: check out the post UIWebView to Play Local and downloaded Video or, more specifically, this answer:
Solution is here, you can play video in Embedded UIWebView.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4\" type=\"application/x-shockwave-mp4\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 412.0)];
[webView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}