iPhone SDK: UIWebView - objective-c

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

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

UIWebView Content is not resizable by user

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.

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.

Load an URL into a webview from outside the application does not work

I'm trying to load an URL into a webview from outside the application. I've set my info.plist properly to support http, https. My app appear in the handler list (safari preference pan).
Here is my code
I have this in my awakefromnib
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
[em
setEventHandler:self
andSelector:#selector(getUrl:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
And then
//get the URL from outside the application
- (void)getUrl:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
// Get the URL
NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
[self initWithUrl:urlStr];
}
//Can be used as intializer to init the webview with a page
-(void)initWithUrl:(NSString *)url
{
//load the lading home page
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
If i nslog the url it show it (so it properly get it). My next method is also called. This method work fine from inside my app.
The problem is that when I click in a link outside of my app (once the app is selected as default browser). I popup the window but it not load the URL. It just does nothing.
Any idea ?
What is the "mainFrame" method of webview? Is that a custom class? Did you add the web view's frame to the window's frame?

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