3

I've built a web browser for iOS and I'm trying to pull the URL to display in an address bar.
I look at the NSURLRequest in the webView:shouldStartLoadWithRequest:navigationType: and the webViewDidFinishLoad: UIWebView delegate methods.

Should I use the URL or the mainDocumentURL property of the NSURLRequest for that purpose, and what is the difference?

cacau
  • 3,606
  • 3
  • 21
  • 42
Austin
  • 4,638
  • 7
  • 41
  • 60

2 Answers2

3

You want to use URL.

mainDocumentURL would be used when loading sub-parts of a page (like images, scripts, whatever) to tell the system which cookies it can safely use when loading those sub-parts. It’s nothing you’d want to display, and is documented to be unused in the current version of the framework anyhow.

/*!
    @method mainDocumentURL
    @abstract The main document URL associated with this load.
    @discussion This URL is used for the cookie "same domain as main
    document" policy. There may also be other future uses.
    See setMainDocumentURL:
    NOTE: In the current implementation, this value is unused by the
    framework. A fully functional version of this method will be available 
    in the future. 
    @result The main document URL.
*/
Wil Shipley
  • 9,343
  • 35
  • 59
  • Where does the documentation say that it is unused? It looks like in the tests I've run that URL is not set and mainDocumentURL is set in the webView:shouldStartLoadWithRequest:navigationType: delegate method in some instances. – Austin Jan 28 '14 at 19:54
  • I’m not clear how an NSURLRequest would load anything if it doesn’t have a URL to load, but I’m not an expert in this realm. – Wil Shipley Jan 28 '14 at 21:16
  • I would advise using mainDocumentURL instead of URL. As a matter of fact URL can refer to a sub-part of the site the UIWebView is currently displaying. – Joss Sep 17 '14 at 09:53
1

The documentation is not very clear, but it appears that mainDocumentURL used for cookie policy internals.

From NSHTTPCookieStorage Class Reference > setCookies:forURL:mainDocumentURL:

mainDocumentURL

The URL of the main HTML document for the top-level frame, if known. Can be nil. This URL is used to determine if the cookie should be accepted if the cookie accept policy is NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain.

If it's possible that the URL you are using is a sub-frame or some media type embedded in an HTML document, then you may wish to know what the mainDocumentURL is; otherwise, just use URL.

Community
  • 1
  • 1
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117