【问题标题】:NSURL returns nil in webviewNSURL 在 webview 中返回 nil
【发布时间】:2015-12-09 18:15:17
【问题描述】:

您好,我正在使用 WebView 从字符串中加载特定 URL,但在 WebView 中,但它返回为 nil。

var linkobject = "http://prf.hn/click/camref:1011l62a/creativeref:1100l4014/destination:http://nl.tommy.com/Tommy-Cares-Tas/C827884386,nl_NL,pd.html#!i=0&color=000&size=OS&utm_source=performancehorizon&utm_medium=affiliate&utm_campaign=FW15&utm_term=(UserID)&utm_content=feed&cmpid=ch:affiliate|dp:ecom|so:performancehorizon|cp:fw15|co:uk|pi:(UserID)|cr:feed&LGWCODE=8718937448230;88093;5350?LGWCODE=8718937448230;105153;6183"
if let url = NSURL(string:linkobject) {
    var req = NSURLRequest(URL: url)
    if count(url.description) != 0 {
        self.webView.contentMode = UIViewContentMode.ScaleAspectFit
        self.webView.loadRequest(req)
    }
}

这是我的网址:http://prf.hn/click/camref:1011l62a/creativeref:1100l4014/destination:http://nl.tommy.com/Tommy-Cares-Tas/C827884386,nl_NL,pd.html#!i=0&color=000&size=OS&utm_source=performancehorizon&utm_medium=affiliate&utm_campaign=FW15&utm_term=(UserID)&utm_content=feed&cmpid=ch:affiliate|dp:ecom|so:performancehorizon|cp:fw15|co:uk|pi:(UserID)|cr:feed&LGWCODE=8718937448230;88093;5350?LGWCODE=8718937448230;105153;6183

现在我已经阅读了官方文档中的这个 url 不适用于 RFC 2396 标准,这就是为什么它给了我一个 nil 对象。

如果传递的路径格式不正确,则 NSURL 类无法创建新的 NSURL 对象;路径必须符合 RFC 2396

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/

What's RFC 2396?

有人知道我如何在我的 webview 中加载这种类型的 url,或者以任何方式绕过或覆盖 NSURL,以便我的字符串可以转换为 NSURL?喜欢听取建议,修改网址不是一种选择,因为它是作为附属网址提供的。

【问题讨论】:

  • 返回 nil 的那一行

标签: ios swift uiwebview nsurl nsurlrequest


【解决方案1】:

您的代码的问题是,网址需要很长时间才能加载,因此一旦应用加载,它就会一直崩溃。

解决方案:等到url加载完成后再放回主队列

var stringUrl = "http://nl.tommy.com/Tommy-Cares-Tas/C827884386,nl_NL,pd.html?utm_source=performancehorizon&utm_medium=affiliate&utm_campaign=FW15&utm_term=(kledingnl)&utm_content=1100l4014&cmpid=ch:affiliate%7Cdp:ecom%7Cso:performancehorizon%7Ccp:fw15%7Cco:uk%7Cpi:(kledingnl)%7Ccr:1100l4014#!color%3D000%26size%3DOS"
    var url = NSURL(string: stringUrl)
    var request = NSURLRequest(URL: url!)
    dispatch_async(dispatch_get_main_queue()){
      webView.loadRequest(request)
    }

【讨论】:

    猜你喜欢
    • 2015-07-30
    • 2023-03-21
    • 1970-01-01
    • 2015-12-28
    • 2014-10-11
    • 2015-08-03
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多