Loading hidden/offscreen UIWebView - ios

Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.

I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.

Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.

It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.

Related

UIWebView's _UIWebViewScrollView

I created an instance of UIWebView called myWebView. I populated myWebView with the content from loadHTMLString, added several UIImageViews and UIViews. It worked as expected: UIImageViews and UIViews are all on top of the webview's content from html string.
I then archived and un-archived myWebView using NSKeyedArchiver and NSKeyedUnarchiver. After un-archived, only the html string content is visible. All other UIViews and UIImageViews were moved to back. So they are not visible. An NSLog of myWebView.subviews before archiving and after un-archiving showed that there a _UIWebViewScrollView was moved from the first object in the subviews to the last object. Note that only the _UIWebViewScrollView order was changed.
Not sure if it was by design by the SDK but to restore the views order and make them visible as normal, I did this:
[myWebView sendSubviewToBack:[myWebView.subviews lastObject]];
Question: Will doing this violate any of Apple's rule as I am not sure what if the _UIWebViewScrollView is considered as Apple's private thing. But since I did not touch that _UIWebViewScrollView.
Well, as far as rules go, you're fine.
HOWEVER: Since the UIWebViewScrollView is an internal member of the UIWebView, if they change the workings of UIWebView in a different iOS version, your code might get screwed up.
To play it a little on the safer side, you should try to set tags on each of the subviews you add, and then do:
[myWebView bringSubviewToFront:[myWebView viewWithTag:100]];
[myWebView bringSubviewToFront:[myWebView viewWithTag:101]];
[myWebView bringSubviewToFront:[myWebView viewWithTag:102]];
... etc (remember to use the tags that you used for your subviews)
On another note, messing with the UIWebView might result in troubles later on, there might be other ways to accomplish what you're trying to do, ways that are less risky

Web View is not reloading every time in iPad web view?

First thanks in advance to have a look on my issue. My issue is my am developing webview based application. It's same like as browser but i have a issue related to reload web view. When i am getting a value form popover bookmarked and try to reload the current web view from this next bookmark url, my web view do noting. I am stuck in this issue. Although when i enter any url through it works perfectly. May i doing something wrong with web view ?
temp = appDeleg.strURLFromPopover;
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
[self.myWebView setDelegate:self];
[self.view addSubview:myWebView];
Please have a look..
The last line looks like very suspicious. In my experience, [self.view addSubview:myWebView] should be placed in the "initialization block" to avoid some lazy init issues. Are you sure All objects are NOT nil? if so, you can try to set UIWebView's delegate to your class, and debug in each method to test if your object are properly initialized. I tried your code, work fine in my side. Below are delegate method you might consider about
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:

UIWebView won't goBack after loading HTML in iOS

I'm having this problem in my iPhone app: I have a webView which is first loaded with an HTML string. After the user clicks a link and it loads the requested page, the webView won't go back when I call the method [UIWebView goBack]; I suppose webView doesn't cache the HTML string. Is there any way I can make webView cache my HTML string without having to save it in a NSString myself?
You can use the canGoBack property and if you can't go back, reload the UIWebView with the original html. If the user has navigated forward using links then the canGoBack property will return YES and a goBack can be initiated on the UIWebView. The _htmlString is a member variable that is set when the UIWebView is initialized using an HTML string. -rrh
- (void)goBack
{
if (_htmlString && ![_browserWebView canGoBack]) {
[_browserWebView loadHTMLString:_htmlString baseURL:nil];
return;
}
[_browserWebView goBack];
}
Try to create a NSURLRequest from the file URL and use loadRequest instead of loadhtmlString
NSURL *htmlFileUrl = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:htmlFileUrl];
self.webView.delegate = self;
[self.webView loadRequest:localRequest];
This sounds like a your webview variable isn't properly linked up to the webview instance that you are using. Breakpoint at this call and check whether your webView variable is 'nil'.
If it is, make sure your webView in your XIB file is linked to an IBOutlet variable in Interface Builder. This is a common mistake and something I tend to forget when designing a new page for the first time.
This tutorial covers a LOT on how to build interfaces using Interface builder which i'm sure you're familliar with but for those that aren't it's also useful. It has some good screenshots which help illustrate what I mean by 'linking' better than me typing "click the little + icon and drag the little thingy on to the UI element" :)
http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/
EDIT
The only other thing i can think of is that you are overwriting your webView variable by re-initialising it somewhere (it is already initialised by the XIB) and therefore you're calling goBack on a webview that doesn't exist on the screen.

Is is possible to communicate between UINavigationController and web page loaded inside UIWebView?

I am new to iOS development. I will try my best to explain my question, please pardon me if you find it silly.
I was wondering if one can load a subview in a UINavigationController by clicking a link inside a UIWebView.
Explaining it further, let's say there is UINavigationController that has a subview and that subview has a UIWebView called A, and there is another subview(that is not loaded yet) called B. Is is possible to load B (or perform any other native Objective C event) when user taps on a link inside the UIWebView A?
For example in Instagram app for iPhone, as I have observed (I may be wrong), there are UIWebViews in views controlled by UINavigationControllers. Taping a link inside a web view loads a new view. How is it happening? Can one set up the communication between the web page loaded inside the UIWebView and the parent/super UIView?.
Please correct me if I am lost. I am a n00b in iOS development so far. Also reference me the APIs or tutorials to do this if this makes any sense. Thanks in advance.
If you want just to handle a click-on-link inside UIWebView, just implement a delegate for the UIWebView. It has webView:shouldStartLoadWithRequest:navigationType: , which is called any time any content is attempted to be loaded in UIWebView. In the implementation of this method, you can block loading of the HTML content and perform any obj-c code, you want.
Hope that helps!

"loadHTMLString:baseURL:" iPhone SDK

I'm coding up my firs iPhone app..
How can I use "loadHTMLString:baseURL:" to send the user to a view called "pagetwo.m"?
To navigate from one view to another or to insert subview given solution is correct and following is the sample code to let you know, how to use delegate method of UIWebView loadHTMLString:baseURL:...
NSString *embedHTML = [NSString stringWithFormat:#"<HTML><HEAD><TITLE>Page 1</TITLE></HEAD><BODY><H2>HTML Code Editor</H2><H3>p1 - The Basic Page</H3>"
"<P>This is the basic webpage. The HTML tags, BODY, H2, H4, P and A were used, but since no attributes were added to these tags, they are left at their defaults. The links below are shown using their default colors.</P>"
"Google.com<BR>Yahoo<BR></BODY></HTML>"];
[webView loadHTMLString:embedHTML baseURL:nil];
I assume pagetwo.m is a UIViewController subclass, right? You can't send a user to view with loadHTMLString:baseURL: since that's a UIWebView's method to load html page into webview. You will need something like:
[self presentModalViewController:pagetwo animated:YES];
Or if it's a subclass of UIView, you need:
[self.view addSubview:pagetwo];
But before that you need to alloc/init pagetwo (and release it later) which will be hard to do since your class is also named "pagetwo" and you instance variable can't be called the same. you could call it (the instance variable) pagetwoview or something, but preferred way would be to follow Objective-C naming conventions and always name your classes starting with a capital letter.

Resources