簡體   English   中英

NSURL在webview中返回nil

[英]NSURL returns nil in webview

您好,我正在使用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/目的地http//nl.tommy.com/Tommy-Cares-Tas/C827884386,nl_NL,pd.html#! i = 0&color = 000&size = OS&utm_source = performancehorizo​​n&utm_medium = affiliate&utm_campaign = FW15&utm_term =(UserID)&utm_content = feed&cmpid = ch:affiliate | dp:ecom | so:performancehorizo​​n | cp:fw15 | co:uk | pi:(&ID) = 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/

什么是RFC 2396?

有人知道我如何在我的Webview中加載這種類型的URL或以任何方式繞過或覆蓋NSURL以便我的字符串可以轉換為NSURL的方法嗎? 希望聽到建議,因此無法修改網址,因為它是作為附屬網址提供的。

您的代碼的問題是,URL需要花費很長時間來加載,因此一旦應用程序加載,它就不斷崩潰。

解決方案:等到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)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM