Auto focus text field in UIWebview - ios

I am loading a webview form in my iOS app and I want to autofocus a textfield on the form. I tried
NSString *focus = [NSString stringWithFormat:#"document.getElementsByName('j_captcha')[0].focus()"];
[self.webView stringByEvaluatingJavaScriptFromString: focus];
But that doesnt seem to work. "j_captcha" is the name of the textfield. How can I achieve this?

This did the trick for me
NSString *evalStr = [NSString stringWithFormat:#"setTimeout( function(){document.getElementsByName('j_captcha')[0].focus();},1000);"];
[webView stringByEvaluatingJavaScriptFromString:evalStr];
This post helped - https://stackoverflow.com/a/14268931/653978

Related

Is there a way to vertically fit web page in UIWebView?

I need that my UIWebView shows me an entire web page, from the top to the bottom (it doesn't matter if the page is very long and I don't care if I'm not able to read it, I just want to see ALL the page).
So, how do I vertically fit the page in the web view?
Scales to fill, Aspect fit etc... don't work.
Thanks.
You can do it simply by using the following line of code:
myWebView.scalesPageToFit = YES;
This will scale the page to adjust both horizontally and vertically within your UIWebView.
You can sacle with javascript function
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[myWebView stringByEvaluatingJavaScriptFromString:jsCommand];
[webview setScalesPageToFit:YES];
or
[webview sizeToFit];
setScalesPageToFit: gives output like this
Sometimes myWebView.scalesPageToFit = YES; doesn't work so we have to set it that's why I written [myWebView setscalesPageToFit:YES]; I have faced this type of problem in UIButton that button.text = #"long text"; does not work but when I use [button setTitle:#"long text" forControlState:UIcontrolStateNormal]; it worked. So may be it will work for you

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.

Voice over only reads UILabels and not UIWebview content

I have created UITableViewCell, that contains 2 objects:
UILabel - Used to show title text.
UIWebView- Used to show HTML.
Normally when Voice focus UITableViewCell, it read all added labels without any problem, but in my case, voice over only reads title and not the webview html content, user has to swipe right and left to move to next/previous element to read the content of webview.
My requirement is that when voice focus UITableViewCell, voice should read UILabels and webview content in one go, because as a developer we know its a HTML, but for app user(blind)
doesn't have any idea about it.
Also I want to know that how to disable UIWebview accessibility. I tried by setting isAccessibility to NO, but still Voice Over focus UIWebview.
[self.webview setIsAccessibilityElement:NO];
How to solve this problem?
I have resolved this problem by implementing method "accessibilityLabel" inside table cell view. For webview fetch web view content, convert html into plain text and use it. Don't forget to disable label and webview accessibility.
-(NSString*)accessibilityLabel{
NSString *labelText=nil;
NSMutableString *cellLabelText=[[NSMutableString alloc] init];
//Set label
[cellLabelText appendString:[NSString stringWithFormat:#", %#", self.titleLabel.text]];
//Fetch web view content, convert html into plain text and use it.
NSString *html = [self stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
NSString *plainText=[self convertHTMLIntoPlainText:html];
[cellLabelText appendString:plainText];
labelText=[NSString stringWithString:cellLabelText];
[cellLabelText release];
return labelText;
}
-(NSString *)convertHTMLIntoPlainText:(NSString *)html{
NSScanner *myScanner;
NSString *text = nil;
myScanner = [NSScanner scannerWithString:html];
while ([myScanner isAtEnd] == NO) {
[myScanner scanUpToString:#"<" intoString:NULL] ;
[myScanner scanUpToString:#">" intoString:&text] ;
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%#>", text] withString:#""];
}
//
html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return html;
}
How about:
[self.webview setAccessibilityElementHidden:YES]
Then set whatever accessibility label you like on the cell with the accessibilityLabel property.

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];

Set dynamic height of webview depending on the content on an iPhone

I want set dynamic height for webview depending on the dynamic text. I've tried
[my_webview sizeThatFits:CGSizeZero];
But it didn't work for me. I want to set webview height dynamically.
See my answer to a similar question. The trick is to set the frame of the webView to a minimal height prior to sending -sizeThatFits:.
Set up your web view as follows:
- (void)webViewSettings {
NSString *htmlString = #"Your HTML String";
NSString* descHtmlString = [NSString stringWithFormat: #"<html><meta name=\"viewport\" content=\"width=300, user-scalable=yes\" /><div style='width:300px' id='ContentDiv'><font face=\"Arial\" size=2.5 color='#702f70'>%#</font></div></html>", htmlString];
descHtmlString = [descHtmlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.webView loadHTMLString:descHtmlString baseURL:[NSURL URLWithString:#""]];
}
Then implemented UIWebViewDelegate in your interface file and the following method in the implementation file:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *contentHeight = [self.descriptionWeb stringByEvaluatingJavaScriptFromString:#"document.getElementById('ContentDiv').offsetHeight"];
self.descriptionWeb.frame = CGRectMake(10,webView.frame.origin.y, 300, [contentHeight floatValue]);
}

Resources