【问题标题】:What is the difference between NSURLRequest's mainDocumentURL and URL properties?NSURLRequest 的 mainDocumentURL 和 URL 属性有什么区别?
【发布时间】:2014-01-28 19:27:58
【问题描述】:

我已经为 iOS 构建了一个网络浏览器,我正在尝试提取 URL 以显示在地址栏中。
我查看了webView:shouldStartLoadWithRequest:navigationType: 中的NSURLRequestwebViewDidFinishLoad: UIWebView 委托方法。

我应该使用NSURLRequestURLmainDocumentURL 属性来达到这个目的,有什么区别?

【问题讨论】:

    标签: objective-c uiwebview nsurlrequest uiwebviewdelegate


    【解决方案1】:

    你想使用URL

    mainDocumentURL 将在加载页面的子部分(如图像、脚本等)时使用,以告诉系统在加载这些子部分时可以安全地使用哪些 cookie。这不是您想要显示的任何内容,并且无论如何都记录在当前版本的框架中未使用。

    /*!
        @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.
    */
    

    【讨论】:

    • 文档在哪里说它未使用?在我运行的测试中,似乎没有设置 URL,并且在某些情况下在 webView:shouldStartLoadWithRequest:navigationType: 委托方法中设置了 mainDocumentURL。
    • 我不清楚如果 NSURLRequest 没有要加载的 URL,它会如何加载任何东西,但我不是这个领域的专家。
    • 我建议使用 mainDocumentURL 而不是 URL。事实上,URL 可以引用 UIWebView 当前显示的站点的子部分。
    【解决方案2】:

    文档不是很清楚,但似乎 mainDocumentURL 用于 cookie 策略内部。

    来自NSHTTPCookieStorage Class Reference > setCookies:forURL:mainDocumentURL:

    主文档URL

    顶级框架的主 HTML 文档的 URL(如果已知)。可以为零。如果 cookie 接受策略是 NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain,则此 URL 用于确定是否应接受 cookie。

    如果您使用的 URL 可能是 HTML 文档中嵌入的子框架或某些媒体类型,那么您可能希望知道 mainDocumentURL 是什么;否则,只需使用 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-06
      • 2011-11-14
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多