2

Actually I have two related questions here, about different use cases of loading requests in a UIWebView.

  1. 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?
  2. 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.

san
  • 3,350
  • 1
  • 28
  • 40
DevGansta
  • 5,564
  • 2
  • 16
  • 16

3 Answers3

3

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.

mhw
  • 31
  • 1
  • Any view is expected to be displayed at some point, the question is rather whether developers should load a request in UIWebView instance and then show it or show and then load. – DevGansta Feb 18 '14 at 16:08
  • It is safe to call load request if the UIWebView is not in the view hierarchy and the delegate methods will be called as usual. You could add the UIWebView to the view hierarchy when webViewDidFinishLoad is called. As to whether it is good practise then it depends what you are trying to achieve with your UI but I don't believe there is any reason why you should expect this to cause problems. – mhw Feb 18 '14 at 16:18
0
  1. 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];
    
  2. 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.

user3182143
  • 9,459
  • 3
  • 32
  • 39
  • You sad "it is safe" in both cases without referencing any documentation or arguing it somehow. I know that it works in practice but I'm more concerned about reliability of those approaches. Any undocumented behavior may change at some point, without any notice from Apple. It would be nice, if you could provide any proof for your answer. – DevGansta Feb 18 '14 at 16:18
  • what proof do you want? – user3182143 Feb 18 '14 at 16:27
  • A reference to a doc, reasoning why it is safe, comments from Apple engineers, anything. – DevGansta Feb 19 '14 at 10:38
  • Go through this one-https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/DisplayWebContent/DisplayWebContent.html – user3182143 Feb 19 '14 at 13:03
  • hello dev ,anyway it does not make any problem if u do the above things. – user3182143 Feb 19 '14 at 13:33
  • At the link you provided, I have not fond anything confirming that the above approaches are safe. Anyway thank you for your input. – DevGansta Feb 21 '14 at 20:27
0

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.

san
  • 3,350
  • 1
  • 28
  • 40
  • The fact that a web view to be visible needs to be inserted in the view hierarchy is too obvious, so I just want to double that indeed it doesn't require window to load content. So far it works, but may stop at any point, without any notification from Apple, as those specifics are not documented. But I agree that the solution #1 seems to be quite reliable. – DevGansta Feb 21 '14 at 20:33
  • As you said yourself #2 works, so I would like to tell you can use #2 if it suits your requirement better than #1. If Apple is going to stop #2 in future, then I think they will definitely notify us like they do in terms of deprecated behavior. Also, if they do it without notification you can always test in the available beta XCode. Then, in case you find it stopped, so raise bug to get clarification and fix your app with the #1. – san Feb 22 '14 at 06:37
  • Good suggestion, although requires monitoring all beta releases and rebuilding the app if it stops working. Thank you! – DevGansta Feb 23 '14 at 09:35
  • I've got a utility class that keeps an instance of UIWebView as I want to use it to render the HTML content to an image then cache that. I'm having definite problems when making subsequent loadHTMLString: calls. The content isn't complete. – horseshoe7 Apr 15 '16 at 14:20