1

I am trying to get the html code of the webpage using following code :-

let url = NSURL(string: "http://www.example.com")
var error: NSError?
let html = NSString(contentsOfURL: url!, encoding: NSUTF8StringEncoding, error: &error)

if (error != nil) {
    print("whoops, something went wrong")
} else {
    print(html!)
}

But i am getting following error in line number 3 ( let html = ....) :-

Argument labels '(contentsOfURL:, encoding:, error:)' do not match any available overloads
shubham mishra
  • 971
  • 1
  • 13
  • 32
  • 1
    `NSError` code management has changed in Swift. You have to make `do/catch`: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID10 – Larme Aug 08 '16 at 08:59

2 Answers2

1

The function is NSString(contentsOfURL url: NSURL, encoding enc: UInt) throws

Your code should look like this:

if let url = NSURL(string: "http://www.example.com") {
    do {
        let html = try NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding)
        print(html)
    } catch {
        print("whoops, something went wrong")
    }
}

For more information, take a look at the new way of handling errors in Swift.

UPDATE

Used with your URL:

if let url = NSURL(string: "http://www.police.gov.bd/career_child.php?id=247%20order%20by%201--") {
    do {
        let html = try NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding)
        print(html)
    } catch {
        print("whoops, something went wrong")
    }
}

UPDATE 2:

To autodetect the encoding see this answer

Community
  • 1
  • 1
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • always executing catch, not executing do – shubham mishra Aug 08 '16 at 09:17
  • @shubhammishra If I execute that code in playground, I get the html content printed. Make sure the URL you use is correct (and has the scheme prefix, such as `http://`or `https://`) – Daniel Aug 08 '16 at 09:31
  • http://www.police.gov.bd/career_child.php?id=247%20order%20by%201-- this url works fine in browser but fails to do so using above – shubham mishra Aug 08 '16 at 09:37
  • You have to use `http://www.police.gov.bd/career_child.php?id=247%20order%20by%201--`, without http it is not well formatted – Daniel Aug 08 '16 at 10:10
  • yes that url is working fine, but please check for this url:http://www.corporatebpl.com/blog.php?show=250%20order%20by%201-- – shubham mishra Aug 09 '16 at 05:59
  • here also i have provided http:// and www. before the link but since it is in comment so it is not showing – shubham mishra Aug 09 '16 at 06:01
  • that page has another type of encoding, use `NSISOLatin1StringEncoding` encoding instead and it will work – Daniel Aug 09 '16 at 07:15
  • so, how could i know which website has which kind of encoding ? – shubham mishra Aug 09 '16 at 10:19
  • Cannot convert value of type 'UInt' to expected argument type 'UnsafeMutablePointer' This error is comming in NSISOLatinStringEncoding – shubham mishra Aug 09 '16 at 11:06
  • 1
    Because it is `NSISOLatin1StringEncoding`, not `NSISOLatinStringEncoding`. When I use the code above with that encoding and your url it works fine. PS: I also added a new edit for autodetecting the encoding – Daniel Aug 09 '16 at 12:22
1

Use below code:

if let url = NSURL(string: "http://www.example.com") {
    do {
        let myHTMLString = try String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    } catch let error as NSError {
        print("Error: \(error)")
    } catch {
        print("failure")
    }
}