UIWebView Content is not resizable by user - objective-c

I'm struck over this little issue and just can't figure a way out. My apologies and gratitude.
Description:
an UIView with fullscreen UIWebView targeted for iPhone programmatically loads a web page using viewDidLoad method. When running the app into the simulator UIWebView is displayed, the web page loaded, but no pinching is allowed. Thus, I can move around the page, but I can't resize with multigesture.
Details:
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.someurl.it";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.userInteractionEnabled=YES;
webView.autoresizingMask=YES;
webView.multipleTouchEnabled=YES;
}

Just insert this line of code along with the other properties you set for the webview:
webView.scalesPageToFit = YES;

Because the proper property to set to allow UIWebView's to interpret pinch-to-zoom events is calledscalesPageToFit. Setting an autoresizingMask is a hint to the auto-rotational layout mechanism in UIView of where a view should be positioned in the new coordinate space of the given device.

Related

UIWebView shows black screen without data

iOS 8.1, XCode 6.1, Storyboards and 2 UIViewControllers that look like this:
The view on the left is the main view, the view on the right is a UIWebView that will show help information written in HTML. My problem is that when I tap on the blue circle with an 'i' in it, it is supposed to go to the 2nd view controller (which it does) and display the html for that language (which it doesn't do)... all I get is a black screen! Here is my code to display the html file contents:
-(void) viewWillAppear:(BOOL)animated {
NSURL *indexURL;
NSString *sysLangCode = [[NSLocale preferredLanguages] objectAtIndex:0];
// do we support this language?
if([sysLangCode isEqualToString:#"en"] || [sysLangCode isEqualToString:#"de"] || [sysLangCode isEqualToString:#"it"] ||
[sysLangCode isEqualToString:#"es"] || [sysLangCode isEqualToString:#"fr"] || [sysLangCode isEqualToString:#"ja"] ||
[sysLangCode isEqualToString:#"zh-Hant"] ) {
indexURL = [[NSBundle mainBundle] URLForResource: [NSString stringWithFormat:#"instRST-%#", sysLangCode]
withExtension:#"html"]; // contatenate the language code to the filename
}
else
indexURL = [[NSBundle mainBundle] URLForResource: #"instRST-en" withExtension:#"html"]; // make 'en' the default
NSLog(#"\n\nmainBundle: %#",[NSBundle mainBundle]);
NSURLRequest *request = [NSURLRequest requestWithURL: indexURL];
NSLog(#"\n\nhtmlURL: %#",indexURL);
[self.webView loadRequest:request];
}
- (void)viewDidLoad {
[super viewDidLoad];
[webView setDelegate:self];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Failed to load with error :%#",[error debugDescription]);
}
UPDATE This is the connection for the webView:
I have tried different scenarios for hours now, and have given up doing this on my own. Can someone please tell me what I'm doing wrong?
Since you are using a storyboard you must use a Segue to present your ViewController. Delete your button action implementation and Control+Drag from your button onto your Help view controller. Link it to the Show segue.
i also face the same issue, below webview property helps me to avoid black screen problem
[self.web setOpaque:NO];

WebView in Cocoa not rendering

In my window controller, I have this:
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"webView = %#", webView);
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.apple.com/"]]];
}
My understanding is that should make the webView in my window now download and render Apple's home page. But all I see in a white panel. What am I missing?
(I've checked that webView is not null; NSLog shows something like
"webView = <WebView: 0x10be7c3b0>")
I've also tried the more modern approach, but still had no success:
[webView setMainFrameURL:#"http://www.apple.com/"];
As this project is sandboxed, the solution in this case was to add the relevant entitlement to the project.

Generate a website screen capture in ios

As the question says, on a iOS device if a user inputs a URL in uitextfield I want to generate a thumbnail capture of this URL. How can this be achieved? Unfortunately I don't have a clue where to start from.
Of course one way I can do this is to send this URL to backend & get the image screenshot from there. I know how one can do this kinda stuff using python or php. But I want to avoid the extra round trip to my backend servers.
I want something like - (uiimage *)screenCapture:(nsurl *)url
As per your question i think you'll want to manipulate the generated screenshot afterwards. You'll add a UIWebView programmatically to load the needed URL then take a snapshot of the loaded web page then remove the UIWebView
Here are the steps
a UIImageView to your viewController and connect it to your viewController class
#property (weak, nonatomic) IBOutlet UIImageView *image_Thumbnail;
having the URL in a variable named "webPageUrlString" add the following lines
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
webView.backgroundColor = [UIColor redColor];
NSURL *websiteUrl = [NSURL URLWithString:**webPageUrlString**];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[webView loadRequest: urlRequest];
webView.delegate = self;
[self.view addSubview:webView];
right now you have requested to load the URL but can't render the snapshot unless the web page is fully loaded,so you'll need to wait for the web page loading notification.In order to that you must subscribe to the webView delegate in the header file like that
#interface ViewController : UIViewController<UIWebViewDelegate>
Now you'll implement the "webViewDidFinishLoad" and take the snapshot you want then hide and remove the webView.
-(void)webViewDidFinishLoad:(UIWebView *)webView{
viewImage = [[UIImageView alloc] initWithFrame:self.view.frame];
viewImage.backgroundColor = [UIColor greenColor];
UIGraphicsBeginImageContext(webView.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[webView setAlpha:0];
[webView removeFromSuperview];
[self.view addSubview:viewImage];
}
Now you have the screenshot you want in the property called "viewImage" and you can do what ever you want with it ;)

How do I scroll a WebView of an HTML string to a specific anchor?

I'm wondering how to scroll programmatically to a given anchor in a WebView.
The content I am showing is rendered by
[[webView mainFrame] loadHTMLString:htmlString baseURL:someURL];
and thus I cannot simply navigate to #anchors by pointing them out in the URLs.
I'm looking for a method along the lines of
[[webView mainFrame] scrollToAnchor:#"anchor"]
but obviously it isn't there.
TIA
Using Javascript Bridge works, but you can also do the equivalent from Objective-C if you like:
DOMDocument *doc = [[webView mainFrame] DOMDocument];
DOMElement *element = [doc getElementById:#"anchor"];
[element scrollIntoView:YES];
- (void)viewDidLoad
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"YOUR_STRING_WITHOUT_ANCHOR"]]];
}
- (void)webViewDidStartLoad:(UIWebView *)_webView {
[self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('YOUR_ANCHOR_HERE_WITHOUT_#').scrollIntoView(true);"];
}
Ok, found a workaround, but I don't know if this is the right way to do it. By getting the reference to the javascript context I can call javascript methods in the webFrame.
[[webView windowScriptObject] evaluateWebScript:#"document.getElementById('TheId').scrollIntoView(true);"];

iPhone SDK: UIWebView

I'm working on an app that uses a UIWebView to display its help files. The webView lives in it's own view, DocViewController...when its called the
- (void)viewDidLoad {
method uses
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:docPage ofType:#"html"]isDirectory:NO]]];
to load the proper doc page. However, once the first page is loaded, the view becomes static and new pages aren't loaded when the docPage changes and the view is toggled. Is there a way to clear the webView so new pages load when requested?
Edit:
The first answer is confusing to me. As is the routine below works. It's just that it only works once. After the view is loaded the first time it does not change when this view is toggled on again and the requested html page is different. The view always displays the first html page requested and will not load new requests.
- (void)viewDidLoad {
docPage = [NSString stringWithFormat: #"%d", hexpage];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[NSBundle mainBundle] pathForResource:docPage ofType:#"html"]isDirectory:NO]]];
}
viewDidLoad is only called once, unless the view is released and needs to be reloaded. This occurs usually only when the view controller receives a low memory warning. Instead of viewDidLoad, try putting that code in viewWillAppear:, which gets called every time the view will show on the screen.
Try using
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
passing an NSURL object to the dir that contains your HTML as baseURL and loading the HTML string with something like
NSString *path = [[NSBundle mainBundle] pathForResource:#"myfile"
ofType:#"html"];
NSString *html = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil];
Edit:
Also, make sure that your hyperlinks are not trying to open in a new window with something like target="_blank", your webview will not open those