3

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()
}
rgov
  • 3,516
  • 1
  • 31
  • 51
Perewillz
  • 73
  • 8

4 Answers4

0

Change your url to use https and it should work.

let url = NSURL(string: "https://weather.weatherbug.com/weather-forecast/now/abuja")!
  • 1
    This didn't work because the website in particular uses http. – Perewillz Aug 08 '17 at 07:39
  • I tested it with the website shown in your code and it worked with https, but perhaps you are using a different url than what is shown. – Mike Killewald Aug 08 '17 at 07:42
  • I just used the https. The problem is that instead on printing the source code for the websites it tells be constraints I used used for my story board when that is not important – Perewillz Aug 08 '17 at 08:11
  • Ok, I don't know why its doing that for you. When I tried your code with https, I got the html source printed in the console. – Mike Killewald Aug 08 '17 at 08:27
0

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()
rgov
  • 3,516
  • 1
  • 31
  • 51
-1

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()
     }
0xInfection
  • 2,676
  • 1
  • 19
  • 34
  • Add more info about your answer and try explaining your code. – 0xInfection Feb 15 '19 at 04:53
  • Never use `NS(Mutable)...` classes in Swift if there is a native counterpart. The request is pointless anyway because `dataTask` can take an URL, too. – vadian Feb 15 '19 at 09:55
-2

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