Downloading web content with Swift 3

1.7k views Asked by At

I am trying to download webcontent for a weather app that I am making. When I run the app the source code on the website does not appear on my Xcode. I also updated my info.plist to accept web content.

Do you have an idea on what the problem is and how I can solve it?

I have a copied my code below:

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://weather.weatherbug.com/weather-forecast/now/abuja")!
    let request = NSMutableURLRequest(url:url as URL)
    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in

        if error != nil{
            print(error.debugDescription)
        }
        else {
            if let unwrappedData = data{

                let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

                print(dataString as Any)
            }
        }
    }

    task.resume()
}
4

There are 4 answers

4
Mike Killewald On

Change your url to use https and it should work.

let url = NSURL(string: "https://weather.weatherbug.com/weather-forecast/now/abuja")!
3
Saranjith PK On

Use

let myURLString = "http://weather.weatherbug.com/weather-forecast/now/abuja"
guard let myURL = URL(string: myURLString) else {
    print("Error: \(myURLString) doesn't seem to be a valid URL")
    return
}

do {
    let myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
    print("HTML : \(myHTMLString)")
} catch let error {
    print("Error: \(error)")
}

From Link

0
rgov On

Here's an example in Swift 4 for downloading a document and parsing as JSON:

// If you're doing this in an Xcode Playground, uncomment these lines:
// import XCPlayground
// XCPSetExecutionShouldContinueIndefinitely()

let url = URL(string: "http://json-schema.org/example/geo.json")!
let task = URLSession.shared.dataTask(with: url) {
    data, response, error in

    guard error == nil else { return }
    guard data != nil else { return }
    guard (response as? HTTPURLResponse)?.statusCode == 200 else { return }

    do {
        if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] {
            print(json)
        }
    } catch { return }
}

task.resume()
2
MICHAEL ADU DARKO On

Use "if let" instead of only "let" and it should work.

 if  let url = URL(string:"http://weather.weatherbug.com/weather-forecast/now/abuja"){

        let request = NSMutableURLRequest(url: url)

        let task = URLSession.shared.dataTask(with: request as URLRequest){

            data, responds, error in
            if error != nil{
                print(error!)

            } else {
                if let unwrappedData = data {

                    let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

                    print(dataString!)

                    DispatchQueue.main.sync(execute: {

                    })
                }
            }

        }



        task.resume()
     }