NSData contentsOfURL constructor returns nil

前端 未结 1 1440
一向
一向 2020-12-20 09:03

I\'m trying to access an API I made in PHP. It works fine in browser, but I can\'t get this code to fetch any sort of data from any given webpage.

var jsonDa         


        
相关标签:
1条回答
  • 2020-12-20 09:57

    the code is correct but the data isn't in UTF it seems so it fails to make the NSString.

    fails in swift & objC

    for that server data is in NSISOLatin2StringEncoding

    it differs based on the HTTP endpoint and normally UTF8 is fine


        var jsonData: NSData = NSData(contentsOfURL: NSURL(string: "https://www.google.com"))
        let string1 = NSString(data: jsonData, encoding: NSISOLatin1StringEncoding)
        println(string1)
    

    shows the page's sourcecode

    0 讨论(0)
提交回复
热议问题