简体   繁体   中英

NSURL returns nil in webview

Hello I'm using a WebView to load a specific URL from a String but in a WebView but it returns as 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)
    }
}

This is my URL: 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

Now i've read the in official documentation that this url doesn't apply with the RFC 2396 standard, that's why it gives me a nil object.

The NSURL class fails to create a new NSURL object if the path being passed is not well formed; the path must comply with RFC 2396

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

What's RFC 2396?

Does somebody know a way how i could load this type of url in my webview or bypass or override NSURL in any way so my string can be converted to an NSURL? Love to hear suggestions, modifing the url is not a option because it's supplied as an affiliate url.

The problem with your code what's that the url was taking a long time to load, so it kept crashing once the app loads.

Solution: Waiting until the url is finishing to load then put it back into the main queue

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)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM